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 84adcbc..0c57463 100644 Binary files a/submissions and b/submissions differ diff --git a/test.db b/test.db index 245ded8..187bde0 100644 Binary files a/test.db and b/test.db differ 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(){