From b0ed3ffe960caed0de773d5d9c3f08cdef3a8d7a Mon Sep 17 00:00:00 2001 From: Andrzej Stepien Date: Sat, 9 Sep 2023 16:04:53 +0200 Subject: [PATCH] add and test 'create' endpoints --- objects/Endpoints.mjs | 45 ++++++++++++-- objects/Publication.mjs | 3 + objects/Story.mjs | 4 +- objects/Submission.mjs | 3 + test-db | 0 test.db | Bin 40960 -> 40960 bytes test/endpoints.test.mjs | 128 +++++++++++++++++++++++++++++++++++++++- 7 files changed, 176 insertions(+), 7 deletions(-) create mode 100644 test-db diff --git a/objects/Endpoints.mjs b/objects/Endpoints.mjs index 4571d73..0d79f28 100644 --- a/objects/Endpoints.mjs +++ b/objects/Endpoints.mjs @@ -1,31 +1,66 @@ import express from "express"; -import { Data } from "./Data.mjs"; +import logger from "../logger.mjs"; +import Story from "./Story.mjs" +import Publication from "./Publication.mjs" +import Submission from "./Submission.mjs"; -export const getEndpoints = (data) => { +export const getEndpoints = (dbObject) => { const router = express.Router() router.get('/stories', (req,res)=>{ - res.send(data.stories) + res.statusCode=200 + res.send(dbObject.stories) }) router.get('/publications', (req,res)=>{ res.statusCode=200 - res.send(data.publications) + res.send(dbObject.publications) }) router.get('/submissions', (req,res)=>{ res.statusCode=200 - res.send(data.submissions) + res.send(dbObject.submissions) }) return router } +export const endpoints = (db) => { + 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) + return router +} + + + + const endpoint = (router,Entity,path,method,db) =>{ + router.post(`/${Entity.name.toLowerCase()}/${path}`, async (req,res) => { + try { + const entity = new Entity(req.body) + await entity[method](db) + res.sendStatus(200) + } catch (error) { + if(error instanceof TypeError){ + res.sendStatus(400) + } + res.sendStatus(500) + } + }) +} + diff --git a/objects/Publication.mjs b/objects/Publication.mjs index 1a4f8b1..8a0eded 100644 --- a/objects/Publication.mjs +++ b/objects/Publication.mjs @@ -7,6 +7,9 @@ export default class Publication extends Title{ this.link=prop } } + get table(){ + return 'pubs' + } constructor(data){ super(data) this._link=data?.link diff --git a/objects/Story.mjs b/objects/Story.mjs index 3316a3a..49df95b 100644 --- a/objects/Story.mjs +++ b/objects/Story.mjs @@ -6,7 +6,9 @@ export default class Story extends Title{ this.word_count=prop } } - + get table(){ + return 'stories' + } constructor(data){ super(data) this._word_count=data?.word_count diff --git a/objects/Submission.mjs b/objects/Submission.mjs index 7578f7d..e081f4f 100644 --- a/objects/Submission.mjs +++ b/objects/Submission.mjs @@ -32,6 +32,9 @@ export default class Submission extends Entity{ this.date_responded=prop } } + get table(){ + return 'subs' + } constructor(data){ super(data) this._story_id=data?.story_id diff --git a/test-db b/test-db new file mode 100644 index 0000000..e69de29 diff --git a/test.db b/test.db index 7fd8bb212b8f510614cf4cbcafc0c6a6616012ac..b8216d98898086246558ef5c745010502284cf98 100644 GIT binary patch delta 200 zcmZoTz|?SnX@WFk$3z)t)(!@}w3i!Gp71lVKiSwgmt8_yTvxdywYa3byj(9cuOzi7 zFSSH3IX`#ve{op}CVr01iVAP}IC&Vw^TEnCi`Yj9vTE`L)*yzHxd0UtqtDKB6 sI|C=DBEO=cfti`EfgunXBAAmuMbEU6F~z5ZSDir)w*i}v$5=W80IO^?{Qv*} delta 66 zcmV-I0KNZ!zyg540+1U4LXjLp1wsHWS4y#D