From 71baf2bcb4b4703417dea9cb084d95967e1b6380 Mon Sep 17 00:00:00 2001 From: Andrzej Stepien Date: Mon, 11 Sep 2023 12:04:09 +0200 Subject: [PATCH] data object now inits after db update --- objects/Endpoints.mjs | 29 ++++++++++++++++++----------- objects/Entity.mjs | 1 - objects/Story.mjs | 2 +- server.mjs | 4 +++- submissions | Bin 40960 -> 40960 bytes test.db | Bin 40960 -> 40960 bytes test/endpoints.test.mjs | 5 ++--- 7 files changed, 24 insertions(+), 17 deletions(-) diff --git a/objects/Endpoints.mjs b/objects/Endpoints.mjs index 7f408fc..ddcb821 100644 --- a/objects/Endpoints.mjs +++ b/objects/Endpoints.mjs @@ -15,50 +15,57 @@ export const getEndpoints = (dbObject) => { router.get('/stories', (req,res)=>{ res.statusCode=200 res.send(dbObject.stories) + return }) router.get('/publications', (req,res)=>{ res.statusCode=200 res.send(dbObject.publications) + return }) router.get('/submissions', (req,res)=>{ res.statusCode=200 res.send(dbObject.submissions) + return }) return router } -export const endpoints = (db) => { +export const postEndpoints = (db,data) => { const router = express.Router() - endpoint(router,Story,'create','insert',db) - endpoint(router,Story,'edit','update',db) - endpoint(router,Story,'delete','del',db) - endpoint(router,Submission,'create','insert',db) - endpoint(router,Submission,'edit','update',db) - endpoint(router,Submission,'delete','del',db) - endpoint(router,Publication,'create','insert',db) - endpoint(router,Publication,'edit','update',db) - endpoint(router,Publication,'delete','del',db) + endpoint(router,Story,'create','insert',db,data) + endpoint(router,Story,'edit','update',db,data) + endpoint(router,Story,'delete','del',db,data) + endpoint(router,Submission,'create','insert',db,data) + endpoint(router,Submission,'edit','update',db,data) + endpoint(router,Submission,'delete','del',db,data) + endpoint(router,Publication,'create','insert',db,data) + endpoint(router,Publication,'edit','update',db,data) + endpoint(router,Publication,'delete','del',db,data) return router } - const endpoint = (router,Entity,path,method,db) =>{ + const endpoint = (router,Entity,path,method,db,data) =>{ router.post(`/${Entity.name.toLowerCase()}/${path}`, async (req,res) => { try { logger.trace({data:req.body},"POST request received") const entity = new Entity(req.body) await entity[method](db) res.sendStatus(200) + data.init() + return } catch (error) { logger.error(error) if(error instanceof TypeError){ res.sendStatus(400) + return } res.sendStatus(500) + return } }) } diff --git a/objects/Entity.mjs b/objects/Entity.mjs index 8ec5ed5..b062a16 100644 --- a/objects/Entity.mjs +++ b/objects/Entity.mjs @@ -1,4 +1,3 @@ -import dv from "./dataValidation.mjs" export default class Entity{ set _id(prop){ if(prop){ diff --git a/objects/Story.mjs b/objects/Story.mjs index 49df95b..88bf1ef 100644 --- a/objects/Story.mjs +++ b/objects/Story.mjs @@ -2,7 +2,7 @@ import Title from "./Title.mjs" export default class Story extends Title{ set _word_count(prop){ if(prop){ - if(!Number.isInteger(prop)){throw new TypeError("word_count must be integer!")} + if(!Number.isInteger(Number(prop))){throw new TypeError("word_count must be integer!")} this.word_count=prop } } diff --git a/server.mjs b/server.mjs index 8b08b4c..7cd5093 100644 --- a/server.mjs +++ b/server.mjs @@ -4,7 +4,7 @@ import logger from "./logger.mjs"; import bodyParser from "body-parser"; import { Data } from "./objects/Data.mjs"; import { db } from "./db.mjs"; -import { getEndpoints } from "./objects/Endpoints.mjs"; +import { getEndpoints, postEndpoints } from "./objects/Endpoints.mjs"; import cors from 'cors' const app = express() @@ -22,7 +22,9 @@ app.use(bodyParser.json()) const data = new Data(db) await data.init() + app.use('/api',getEndpoints(data)) +app.use('/api',postEndpoints(db,data) ) diff --git a/submissions b/submissions index 84adcbc8f99a4e600b8f8fde99c0e3e014df812e..0c5746390341b1898ba947873c047c9538d02ff9 100644 GIT binary patch delta 169 zcmZoTz|?SnX@WH4$%!(~tS1@t(q3*%nX50&@5{i&_mzqNC4VsgUjBLf9sD)?IecIF zb@)X#D=O^glL9J`VA5qi8kCw>qTrHPsiRN?6jEdIWSZ=2FF#qt-cpwT1_PTolPD7l z1Cuttw`Z|JNJeI{LZU)&a$>IiVe7#O(tMHu+s^WWg#%Ri65gTID9hu@c9 zhhJo~qQVcp$s+calfT-@PtLK=(GX=~VPMkc_x3DS2+7DSR!CF`PEO3V=Vt^8%k%g- Q=Hw{&=9iXaEOJNy0Q^QDA^-pY diff --git a/test.db b/test.db index 245ded8d263311bc3b8ef687864aaca3f9d67c77..187bde052fb270455193cf7e92fc57b937f29ccc 100644 GIT binary patch delta 19 bcmZoTz|?SnX@WH4#)&e{j2ky5ESL`fOC|@@ delta 19 bcmZoTz|?SnX@WH4go!fFj1x8{ESL`fNjC?Q diff --git a/test/endpoints.test.mjs b/test/endpoints.test.mjs index 87de5b8..0f0711e 100644 --- a/test/endpoints.test.mjs +++ b/test/endpoints.test.mjs @@ -6,8 +6,7 @@ import chaiHttp from "chai-http"; import { testDb as db } from "../db.mjs"; import { Data } from "../objects/Data.mjs"; import { beforeEach, afterEach } from "mocha"; -import { endpoints, getEndpoints } from "../objects/Endpoints.mjs"; -import Submission from "../objects/Submission.mjs" +import { postEndpoints, getEndpoints } from "../objects/Endpoints.mjs"; chai.use(chaiHttp) const app = express() @@ -15,7 +14,7 @@ const data = new Data(db) await data.init() app.use(bodyParser.json()) app.use('/api',getEndpoints(data)) -app.use('/api', endpoints(db)) +app.use('/api', postEndpoints(db)) describe("testing endpoints...",async function(){