2023-09-06 16:26:15 +00:00
|
|
|
import express from "express";
|
2023-09-09 14:04:53 +00:00
|
|
|
import logger from "../logger.mjs";
|
|
|
|
import Story from "./Story.mjs"
|
|
|
|
import Publication from "./Publication.mjs"
|
|
|
|
import Submission from "./Submission.mjs";
|
2023-09-06 16:26:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-09-09 14:04:53 +00:00
|
|
|
export const getEndpoints = (dbObject) => {
|
2023-09-06 16:26:15 +00:00
|
|
|
const router = express.Router()
|
|
|
|
|
|
|
|
router.get('/stories', (req,res)=>{
|
2023-09-09 14:04:53 +00:00
|
|
|
res.statusCode=200
|
|
|
|
res.send(dbObject.stories)
|
2023-09-11 10:04:09 +00:00
|
|
|
return
|
2023-09-06 16:26:15 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
router.get('/publications', (req,res)=>{
|
|
|
|
res.statusCode=200
|
2023-09-09 14:04:53 +00:00
|
|
|
res.send(dbObject.publications)
|
2023-09-11 10:04:09 +00:00
|
|
|
return
|
2023-09-06 16:26:15 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
router.get('/submissions', (req,res)=>{
|
|
|
|
res.statusCode=200
|
2023-09-09 14:04:53 +00:00
|
|
|
res.send(dbObject.submissions)
|
2023-09-11 10:04:09 +00:00
|
|
|
return
|
2023-09-06 16:26:15 +00:00
|
|
|
})
|
2023-09-11 22:00:25 +00:00
|
|
|
router.get('/responses', (req,res)=>{
|
|
|
|
res.statusCode=200
|
|
|
|
res.send(dbObject.responses)
|
|
|
|
return
|
|
|
|
})
|
2023-09-06 16:26:15 +00:00
|
|
|
|
|
|
|
return router
|
|
|
|
}
|
|
|
|
|
2023-09-11 10:04:09 +00:00
|
|
|
export const postEndpoints = (db,data) => {
|
2023-09-09 14:04:53 +00:00
|
|
|
const router = express.Router()
|
2023-09-11 10:04:09 +00:00
|
|
|
endpoint(router,Story,'create','insert',db,data)
|
|
|
|
endpoint(router,Story,'edit','update',db,data)
|
2023-09-14 09:40:19 +00:00
|
|
|
endpoint(router,Story,'delete','update',db,data)
|
2023-09-11 10:04:09 +00:00
|
|
|
endpoint(router,Submission,'create','insert',db,data)
|
|
|
|
endpoint(router,Submission,'edit','update',db,data)
|
2023-09-14 09:40:19 +00:00
|
|
|
endpoint(router,Submission,'delete','update',db,data)
|
2023-09-11 10:04:09 +00:00
|
|
|
endpoint(router,Publication,'create','insert',db,data)
|
|
|
|
endpoint(router,Publication,'edit','update',db,data)
|
|
|
|
endpoint(router,Publication,'delete','del',db,data)
|
2023-09-09 14:04:53 +00:00
|
|
|
return router
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-09-11 10:04:09 +00:00
|
|
|
const endpoint = (router,Entity,path,method,db,data) =>{
|
2023-09-09 14:04:53 +00:00
|
|
|
router.post(`/${Entity.name.toLowerCase()}/${path}`, async (req,res) => {
|
|
|
|
try {
|
2023-09-10 15:01:21 +00:00
|
|
|
logger.trace({data:req.body},"POST request received")
|
2023-09-09 14:04:53 +00:00
|
|
|
const entity = new Entity(req.body)
|
|
|
|
await entity[method](db)
|
|
|
|
res.sendStatus(200)
|
2023-09-11 10:04:09 +00:00
|
|
|
data.init()
|
|
|
|
return
|
2023-09-09 14:04:53 +00:00
|
|
|
} catch (error) {
|
2023-09-10 15:01:21 +00:00
|
|
|
logger.error(error)
|
2023-09-09 14:04:53 +00:00
|
|
|
if(error instanceof TypeError){
|
|
|
|
res.sendStatus(400)
|
2023-09-11 10:04:09 +00:00
|
|
|
return
|
2023-09-09 14:04:53 +00:00
|
|
|
}
|
|
|
|
res.sendStatus(500)
|
2023-09-11 10:04:09 +00:00
|
|
|
return
|
2023-09-09 14:04:53 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-09-06 16:26:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
|