data object now inits after db update
This commit is contained in:
parent
0d66151fae
commit
71baf2bcb4
|
@ -15,50 +15,57 @@ export const getEndpoints = (dbObject) => {
|
||||||
router.get('/stories', (req,res)=>{
|
router.get('/stories', (req,res)=>{
|
||||||
res.statusCode=200
|
res.statusCode=200
|
||||||
res.send(dbObject.stories)
|
res.send(dbObject.stories)
|
||||||
|
return
|
||||||
})
|
})
|
||||||
|
|
||||||
router.get('/publications', (req,res)=>{
|
router.get('/publications', (req,res)=>{
|
||||||
res.statusCode=200
|
res.statusCode=200
|
||||||
res.send(dbObject.publications)
|
res.send(dbObject.publications)
|
||||||
|
return
|
||||||
})
|
})
|
||||||
|
|
||||||
router.get('/submissions', (req,res)=>{
|
router.get('/submissions', (req,res)=>{
|
||||||
res.statusCode=200
|
res.statusCode=200
|
||||||
res.send(dbObject.submissions)
|
res.send(dbObject.submissions)
|
||||||
|
return
|
||||||
})
|
})
|
||||||
|
|
||||||
return router
|
return router
|
||||||
}
|
}
|
||||||
|
|
||||||
export const endpoints = (db) => {
|
export const postEndpoints = (db,data) => {
|
||||||
const router = express.Router()
|
const router = express.Router()
|
||||||
endpoint(router,Story,'create','insert',db)
|
endpoint(router,Story,'create','insert',db,data)
|
||||||
endpoint(router,Story,'edit','update',db)
|
endpoint(router,Story,'edit','update',db,data)
|
||||||
endpoint(router,Story,'delete','del',db)
|
endpoint(router,Story,'delete','del',db,data)
|
||||||
endpoint(router,Submission,'create','insert',db)
|
endpoint(router,Submission,'create','insert',db,data)
|
||||||
endpoint(router,Submission,'edit','update',db)
|
endpoint(router,Submission,'edit','update',db,data)
|
||||||
endpoint(router,Submission,'delete','del',db)
|
endpoint(router,Submission,'delete','del',db,data)
|
||||||
endpoint(router,Publication,'create','insert',db)
|
endpoint(router,Publication,'create','insert',db,data)
|
||||||
endpoint(router,Publication,'edit','update',db)
|
endpoint(router,Publication,'edit','update',db,data)
|
||||||
endpoint(router,Publication,'delete','del',db)
|
endpoint(router,Publication,'delete','del',db,data)
|
||||||
return router
|
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) => {
|
router.post(`/${Entity.name.toLowerCase()}/${path}`, async (req,res) => {
|
||||||
try {
|
try {
|
||||||
logger.trace({data:req.body},"POST request received")
|
logger.trace({data:req.body},"POST request received")
|
||||||
const entity = new Entity(req.body)
|
const entity = new Entity(req.body)
|
||||||
await entity[method](db)
|
await entity[method](db)
|
||||||
res.sendStatus(200)
|
res.sendStatus(200)
|
||||||
|
data.init()
|
||||||
|
return
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error(error)
|
logger.error(error)
|
||||||
if(error instanceof TypeError){
|
if(error instanceof TypeError){
|
||||||
res.sendStatus(400)
|
res.sendStatus(400)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
res.sendStatus(500)
|
res.sendStatus(500)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
import dv from "./dataValidation.mjs"
|
|
||||||
export default class Entity{
|
export default class Entity{
|
||||||
set _id(prop){
|
set _id(prop){
|
||||||
if(prop){
|
if(prop){
|
||||||
|
|
|
@ -2,7 +2,7 @@ import Title from "./Title.mjs"
|
||||||
export default class Story extends Title{
|
export default class Story extends Title{
|
||||||
set _word_count(prop){
|
set _word_count(prop){
|
||||||
if(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
|
this.word_count=prop
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@ import logger from "./logger.mjs";
|
||||||
import bodyParser from "body-parser";
|
import bodyParser from "body-parser";
|
||||||
import { Data } from "./objects/Data.mjs";
|
import { Data } from "./objects/Data.mjs";
|
||||||
import { db } from "./db.mjs";
|
import { db } from "./db.mjs";
|
||||||
import { getEndpoints } from "./objects/Endpoints.mjs";
|
import { getEndpoints, postEndpoints } from "./objects/Endpoints.mjs";
|
||||||
import cors from 'cors'
|
import cors from 'cors'
|
||||||
|
|
||||||
const app = express()
|
const app = express()
|
||||||
|
@ -22,7 +22,9 @@ app.use(bodyParser.json())
|
||||||
const data = new Data(db)
|
const data = new Data(db)
|
||||||
await data.init()
|
await data.init()
|
||||||
|
|
||||||
|
|
||||||
app.use('/api',getEndpoints(data))
|
app.use('/api',getEndpoints(data))
|
||||||
|
app.use('/api',postEndpoints(db,data) )
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
BIN
submissions
BIN
submissions
Binary file not shown.
|
@ -6,8 +6,7 @@ import chaiHttp from "chai-http";
|
||||||
import { testDb as db } from "../db.mjs";
|
import { testDb as db } from "../db.mjs";
|
||||||
import { Data } from "../objects/Data.mjs";
|
import { Data } from "../objects/Data.mjs";
|
||||||
import { beforeEach, afterEach } from "mocha";
|
import { beforeEach, afterEach } from "mocha";
|
||||||
import { endpoints, getEndpoints } from "../objects/Endpoints.mjs";
|
import { postEndpoints, getEndpoints } from "../objects/Endpoints.mjs";
|
||||||
import Submission from "../objects/Submission.mjs"
|
|
||||||
|
|
||||||
chai.use(chaiHttp)
|
chai.use(chaiHttp)
|
||||||
const app = express()
|
const app = express()
|
||||||
|
@ -15,7 +14,7 @@ const data = new Data(db)
|
||||||
await data.init()
|
await data.init()
|
||||||
app.use(bodyParser.json())
|
app.use(bodyParser.json())
|
||||||
app.use('/api',getEndpoints(data))
|
app.use('/api',getEndpoints(data))
|
||||||
app.use('/api', endpoints(db))
|
app.use('/api', postEndpoints(db))
|
||||||
|
|
||||||
|
|
||||||
describe("testing endpoints...",async function(){
|
describe("testing endpoints...",async function(){
|
||||||
|
|
Loading…
Reference in New Issue