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 7fd8bb2..b8216d9 100644 Binary files a/test.db and b/test.db differ diff --git a/test/endpoints.test.mjs b/test/endpoints.test.mjs index 760b5f8..a1db716 100644 --- a/test/endpoints.test.mjs +++ b/test/endpoints.test.mjs @@ -1,16 +1,21 @@ import {describe} from "mocha"; import chai, { expect } from "chai"; +import bodyParser from "body-parser"; import express from 'express' import chaiHttp from "chai-http"; import { testDb as db } from "../db.mjs"; import { Data } from "../objects/Data.mjs"; -import { getEndpoints } from "../objects/Endpoints.mjs"; +import { beforeEach, afterEach } from "mocha"; +import { endpoints, getEndpoints } from "../objects/Endpoints.mjs"; +import Submission from "../objects/Submission.mjs" chai.use(chaiHttp) const app = express() const data = new Data(db) await data.init() +app.use(bodyParser.json()) app.use('/api',getEndpoints(data)) +app.use('/api', endpoints(db)) @@ -36,4 +41,125 @@ describe("Testing GET endpoints", async function(){ expect(res.body).to.be.a('array') }) }) +}) +describe("testing /create endpoints", async function(){ + + describe("/story/create",async function(){ + const goodData = { + title:"#test", + word_count:111 + } + const badData = { + title:1, + word_count:"not a number" + } + afterEach(async function(){ + await db('stories') + .where('title',goodData.title) + .del() + }) + it("should return 200 if a valid request is made",async function(){ + const res = await chai.request(app) + .post('/api/story/create') + .send(goodData) + expect(res).to.have.status(200) + }) + it("should return 400 if an invalid request is made",async function(){ + const res = await chai.request(app) + .post('/api/story/create') + .send(badData) + expect(res).to.have.status(400) + }) + it("the new entry should exist in the database",async function(){ + await chai.request(app) + .post('/api/story/create') + .send(goodData) + const res = await db('stories') + .select('*') + .where('title',goodData.title) + expect(res[0].title).to.eql(goodData.title) + }) + + }) + describe("/publication/create",async function(){ + const goodData = { + title:"#test", + link:"www.internet.com" + } + const badData = { + title:1, + link:1 + } + afterEach(async function(){ + await db('pubs') + .where('title',goodData.title) + .del() + }) + it("should return 200 if a valid request is made",async function(){ + const res = await chai.request(app) + .post('/api/publication/create') + .send(goodData) + expect(res).to.have.status(200) + }) + it("should return 400 if an invalid request is made",async function(){ + const res = await chai.request(app) + .post('/api/publication/create') + .send(badData) + expect(res).to.have.status(400) + }) + it("the new entry should exist in the database",async function(){ + await chai.request(app) + .post('/api/publication/create') + .send(goodData) + const res = await db('pubs') + .select('*') + .where('title',goodData.title) + expect(res[0].title).to.eql(goodData.title) + }) + + }) + describe("/submission/create",async function(){ + const goodData = { + story_id:1, + pub_id:1, + response_id:1, + date_submitted:"1066-01-01", + date_responded:"1066-01-01" + } + const badData = { + story_id:"string", + pub_id:1, + response_id:1, + date_submitted:"1066-01-01", + date_responded:"1066-01-01" + } + afterEach(async function(){ + await db('subs') + .where('date_submitted',goodData.date_submitted) + .del() + }) + it("should return 200 if a valid request is made",async function(){ + const res = await chai.request(app) + .post('/api/submission/create') + .send(goodData) + expect(res).to.have.status(200) + }) + it("should return 400 if an invalid request is made",async function(){ + const res = await chai.request(app) + .post('/api/submission/create') + .send(badData) + expect(res).to.have.status(400) + }) + it("the new entry should exist in the database",async function(){ + await chai.request(app) + .post('/api/submission/create') + .send(goodData) + const res = await db('subs') + .select('*') + .where('date_submitted',goodData.date_submitted) + expect(res[0].date_responded).to.eql(goodData.date_responded) + }) + + }) + }) \ No newline at end of file