From e9b55f3e640b04a494dac56f8e2b5c35be5b9bc3 Mon Sep 17 00:00:00 2001 From: Raffone17 Date: Wed, 21 Jul 2021 12:29:49 +0200 Subject: [PATCH 1/7] Integration front-end and back-end --- client/src/routes/api/authors/[slug].js | 34 ---- client/src/routes/api/authors/index.js | 29 --- client/src/routes/api/books/[slug].js | 35 ---- client/src/routes/api/books/index.js | 10 -- client/src/routes/authors.svelte | 226 ++++++++++++++---------- client/src/routes/books.svelte | 2 +- package-lock.json | 71 ++++++++ package.json | 5 +- server/loaders/koa.ts | 6 +- 9 files changed, 212 insertions(+), 206 deletions(-) delete mode 100644 client/src/routes/api/authors/[slug].js delete mode 100644 client/src/routes/api/authors/index.js delete mode 100644 client/src/routes/api/books/[slug].js delete mode 100644 client/src/routes/api/books/index.js diff --git a/client/src/routes/api/authors/[slug].js b/client/src/routes/api/authors/[slug].js deleted file mode 100644 index 0373bdb..0000000 --- a/client/src/routes/api/authors/[slug].js +++ /dev/null @@ -1,34 +0,0 @@ -import db from '../../../json/db.js' - -const authors = db.authors - -export async function get (request) { - try { - const bid = request.params.slug - const book = await authors.find(({ id }) => id === bid) - return { - status: 200, - body: book - } - } catch (err) { - return { - status: 500, - body: { - error: 'A server error occured: ', - type: '' + err - } - } - } -} - -export async function del (request) { - const bid = parseInt(request.params.slug) - const ret = authors.findIndex(({ id }) => id === bid) - if (ret >= 0) { - authors.splice(ret, 1) - } - return { - status: 200, - body: { i: ret } - } -} diff --git a/client/src/routes/api/authors/index.js b/client/src/routes/api/authors/index.js deleted file mode 100644 index 630d8c8..0000000 --- a/client/src/routes/api/authors/index.js +++ /dev/null @@ -1,29 +0,0 @@ -import db from '../../../json/db.js' - -const authors = db.authors -const books = db.books - -function newId () { - const newId = books.reduce((a, c) => { return (c.id > a) ? c.id : a }, 0) - - return 1 + newId -} - -export async function get (request) { - return { - status: 200, - body: authors - } -} - -export async function put (request) { - const author = JSON.parse(request.body) - author.id = newId() - authors.push(author) - return { - status: 200, - body: { - status: 'Success' - } - } -} diff --git a/client/src/routes/api/books/[slug].js b/client/src/routes/api/books/[slug].js deleted file mode 100644 index 06a5954..0000000 --- a/client/src/routes/api/books/[slug].js +++ /dev/null @@ -1,35 +0,0 @@ - -import db from '../../../json/db.js' - -const books = db.books - -export async function get (request) { - try { - const bid = request.params.slug - const book = await books.find(({ id }) => id === bid) - return { - status: 200, - body: book - } - } catch (err) { - return { - status: 500, - body: { - error: 'A server error occured: ', - type: '' + err - } - } - } -} - -export async function del (request) { - const bid = parseInt(request.params.slug) - const ret = books.findIndex(({ id }) => id === bid) - if (ret >= 0) { - books.splice(ret, 1) - } - return { - status: 200, - body: { i: ret } - } -} diff --git a/client/src/routes/api/books/index.js b/client/src/routes/api/books/index.js deleted file mode 100644 index f2c804b..0000000 --- a/client/src/routes/api/books/index.js +++ /dev/null @@ -1,10 +0,0 @@ -import db from '../../../json/db.js' - -const books = db.books - -export async function get (request) { - return { - status: 200, - body: books - } -} diff --git a/client/src/routes/authors.svelte b/client/src/routes/authors.svelte index 6332ff4..7a8c275 100644 --- a/client/src/routes/authors.svelte +++ b/client/src/routes/authors.svelte @@ -1,100 +1,115 @@ @@ -106,34 +121,52 @@
-
+
- - - + {#each authors as author} - - - - - + + + + + {/each}
+ Name + Bio -
- {author.name} - - {author.bio} - - - -
+ {author.name} + + {author.bio} + + + +
@@ -142,27 +175,30 @@
- - {#if updateopen}Edit Author{:else}Add a new Author{/if} + {#if updateopen}Edit Author{:else}Add a new Author{/if} - -

+ +

- + {#if updateopen} - + {:else} - + {/if} - + diff --git a/client/src/routes/books.svelte b/client/src/routes/books.svelte index e333e1c..81a3d53 100644 --- a/client/src/routes/books.svelte +++ b/client/src/routes/books.svelte @@ -2,7 +2,7 @@ import axios from 'axios' import { onMount } from 'svelte'; - const apiPath = "/api/books" + const apiPath = "/api/book" var books = [] diff --git a/package-lock.json b/package-lock.json index 29d15f6..ad33e51 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2730,6 +2730,40 @@ } } }, + "koa-send": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/koa-send/-/koa-send-5.0.1.tgz", + "integrity": "sha512-tmcyQ/wXXuxpDxyNXv5yNNkdAMdFRqwtegBXUaowiQzUKqJehttS0x2j0eOZDQAyloAth5w6wwBImnFzkUz3pQ==", + "requires": { + "debug": "^4.1.1", + "http-errors": "^1.7.3", + "resolve-path": "^1.4.0" + }, + "dependencies": { + "debug": { + "version": "4.3.2", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.2.tgz", + "integrity": "sha512-mOp8wKcvj7XxC78zLgw/ZA+6TSgkoE2C/ienthhRD298T7UNwAg9diBpLRxC0mOezLl4B0xV7M0cCO6P/O0Xhw==", + "requires": { + "ms": "2.1.2" + } + }, + "ms": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" + } + } + }, + "koa-static": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/koa-static/-/koa-static-5.0.0.tgz", + "integrity": "sha512-UqyYyH5YEXaJrf9S8E23GoJFQZXkBVJ9zYYMPGz919MSX1KuvAcycIuS0ci150HCoPf4XQVhQ84Qf8xRPWxFaQ==", + "requires": { + "debug": "^3.1.0", + "koa-send": "^5.0.0" + } + }, "koa-swagger-decorator": { "version": "1.7.5", "resolved": "https://registry.npmjs.org/koa-swagger-decorator/-/koa-swagger-decorator-1.7.5.tgz", @@ -3614,6 +3648,43 @@ "path-parse": "^1.0.6" } }, + "resolve-path": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/resolve-path/-/resolve-path-1.4.0.tgz", + "integrity": "sha1-xL2p9e+y/OZSR4c6s2u02DT+Fvc=", + "requires": { + "http-errors": "~1.6.2", + "path-is-absolute": "1.0.1" + }, + "dependencies": { + "depd": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/depd/-/depd-1.1.2.tgz", + "integrity": "sha1-m81S4UwJd2PnSbJ0xDRu0uVgtak=" + }, + "http-errors": { + "version": "1.6.3", + "resolved": "https://registry.npmjs.org/http-errors/-/http-errors-1.6.3.tgz", + "integrity": "sha1-i1VoC7S+KDoLW/TqLjhYC+HZMg0=", + "requires": { + "depd": "~1.1.2", + "inherits": "2.0.3", + "setprototypeof": "1.1.0", + "statuses": ">= 1.4.0 < 2" + } + }, + "inherits": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz", + "integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=" + }, + "setprototypeof": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.1.0.tgz", + "integrity": "sha512-BvE/TwpZX4FXExxOxZyRGQQv651MSwmWKZGqvmPcRIjDqWub67kTKuIMx43cZZrS/cBBzwBcNDWoFxt2XEFIpQ==" + } + } + }, "resolve-url": { "version": "0.2.1", "resolved": "https://registry.npmjs.org/resolve-url/-/resolve-url-0.2.1.tgz", diff --git a/package.json b/package.json index 4775bbb..bf11370 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,9 @@ "serve:server": "npm run build:server && nodemon dist/app.js", "serve:client": "cd client && npm run dev", "dev": "concurrently \"npm run serve:server\" \"npm run serve:client\"", - "build:server": "rm -rf ./dist/ && tsc" + "build:server": "rm -rf ./dist/ && tsc", + "build:client": "cd client && npm run build", + "serve:prod": "npm run build:client && npm run serve:server" }, "repository": { "type": "git", @@ -38,6 +40,7 @@ "koa-jwt": "^4.0.1", "koa-logger": "^3.2.1", "koa-router": "^10.0.0", + "koa-static": "^5.0.0", "koa-swagger-decorator": "^1.7.5", "migrate-mongo": "^8.2.2", "mongodb": "^3.6.10", diff --git a/server/loaders/koa.ts b/server/loaders/koa.ts index 2c36fb0..f08f663 100644 --- a/server/loaders/koa.ts +++ b/server/loaders/koa.ts @@ -2,6 +2,7 @@ import Koa from "koa"; import koaBody from "koa-body"; import jwt from "koa-jwt"; import cors from "@koa/cors"; +import serve from "koa-static"; import route from '../api'; import swagger from "../api/swagger"; @@ -27,7 +28,10 @@ export default ({ app }: { app: Koa }) => { }).unless({ path: unlessPaths })); */ - + // Load client + console.log(__dirname + '/../../client/build'); + app.use(serve(__dirname + '/../../client/build')); + // Load API routes console.log(route.stack.map(i => i.path)); app.use(route.routes()).use(route.allowedMethods()); From bc5a62699c6791a8dfe31e31216a13ba307f3885 Mon Sep 17 00:00:00 2001 From: alv67 Date: Wed, 21 Jul 2021 15:36:38 +0000 Subject: [PATCH 2/7] [WIP] Books code integration --- client/src/routes/authors.svelte | 14 ++-- client/src/routes/books.svelte | 135 ++++++++++++++++++++++++++++++- 2 files changed, 137 insertions(+), 12 deletions(-) diff --git a/client/src/routes/authors.svelte b/client/src/routes/authors.svelte index 7a8c275..977db6c 100644 --- a/client/src/routes/authors.svelte +++ b/client/src/routes/authors.svelte @@ -19,16 +19,11 @@ var authors = []; var addAuthorForm = { - id: "", + _id: "", name: "", bio: "", }; - var editForm = { - _id: "", - title: "", - author: "", - description: "", - }; + let addopen = false; let updateopen = false; @@ -72,9 +67,10 @@ }) .catch((error) => { console.error(error); + alert(error); getAuthors(); - }); - closeDialog(); + }) + .finally(() => closeDialog()); } function updateAuthor() { diff --git a/client/src/routes/books.svelte b/client/src/routes/books.svelte index 81a3d53..d6000ec 100644 --- a/client/src/routes/books.svelte +++ b/client/src/routes/books.svelte @@ -1,10 +1,33 @@