diff --git a/logger.mjs b/logger.mjs index 3c1b596..6f71e5d 100644 --- a/logger.mjs +++ b/logger.mjs @@ -16,7 +16,7 @@ const __dirname = path.dirname(__filename); // }) export default pino( { - level: 'trace', + level: 'fatal', formatters: { level: (label) => { return { level: label.toUpperCase() }; diff --git a/objects/Data.mjs b/objects/Data.mjs index 53554be..019136d 100644 --- a/objects/Data.mjs +++ b/objects/Data.mjs @@ -8,12 +8,14 @@ export class Data { this.stories = await this.getStories() this.stories.map(row=>{ row.submissions=this.getSubmissionsByStoryId(row.id) + }) this.publications = await this.getPublications() this.publications.map(row=>{ row.submissions=this.getSubmissionsByPublicationId(row.id) }) this.responses = await this.getResponses() + this.genres = await this.getGenres() return this } async getStories() { @@ -44,7 +46,15 @@ export class Data { return this.#db('responses') .select('*') } - + async getGenres(){ + const res = await this.#db('genres') + .select('*') + const array = [] + for (const row of res) { + array[row.id]=row.name + } + return array + } getSubmissionsByStoryId(id){ return this.submissions.filter(row=>row.story_id==id) } @@ -53,4 +63,28 @@ export class Data { return this.submissions.filter(row=>row.pub_id==id) } + async getGenresByStoryId(id){ + const res = await this.#db('stories_genres') + .select('genre_id') + .where('story_id',id) + return this.#makeGenreArray(res) + } + async getGenresByPublicationId(id){ + const res = await this.#db('pubs_genres') + .select('genre_id') + .where('pub_id',id) + console.dir(res) + return this.#makeGenreArray(res) + } + + #makeGenreArray(data){ + const array = [] + for (const row of data) { + array.push({ + name:this.genres[row.genre_id], + id: row.genre_id + }) + } + return array + } } \ No newline at end of file diff --git a/objects/Entity.mjs b/objects/Entity.mjs index 2910a10..6825dc2 100644 --- a/objects/Entity.mjs +++ b/objects/Entity.mjs @@ -1,8 +1,10 @@ export default class Entity{ set _id(prop){ if(prop){ - const propNumber = Number(prop) - if(!Number.isInteger(propNumber)){throw new TypeError("id must be an integer!")} + const propNumber = Number.parseInt(prop) + console.log("PropNumber: "+propNumber) + + if(Number.isNaN(propNumber)){throw new TypeError("id must be an integer!")} this.id = propNumber } } diff --git a/objects/Story.mjs b/objects/Story.mjs index b3e029b..c76ee63 100644 --- a/objects/Story.mjs +++ b/objects/Story.mjs @@ -3,8 +3,8 @@ import logger from "../logger.mjs" export default class Story extends Title{ set _word_count(prop){ if(prop){ - const propNumber = Number(prop) - if(!Number.isInteger(propNumber)){throw new TypeError("word_count must be integer!")} + const propNumber = Number.parseInt(prop) + if(Number.isNaN(propNumber)){throw new TypeError("word_count must be integer!")} this.word_count=propNumber } } diff --git a/objects/Submission.mjs b/objects/Submission.mjs index 281f359..d8c8491 100644 --- a/objects/Submission.mjs +++ b/objects/Submission.mjs @@ -4,22 +4,22 @@ import dataValidation from "./dataValidation.mjs"; export default class Submission extends Entity{ set _story_id(prop){ if(prop){ - const propNumber = Number(prop) - if(!Number.isInteger(propNumber)){throw new TypeError("story_id must be an integer")} + const propNumber = Number.parseInt(prop) + if(Number.isNaN(propNumber)){throw new TypeError("story_id must be an integer")} this.story_id=propNumber } } set _pub_id(prop){ - const propNumber = Number(prop) + const propNumber = Number.parseInt(prop) if(prop){ - if(!Number.isInteger(propNumber)){throw new TypeError("pub_id must be an integer")} + if(Number.isNaN(propNumber)){throw new TypeError("pub_id must be an integer")} this.pub_id=propNumber } } set _response_id(prop){ if(prop){ - const propNumber = Number(prop) - if(!Number.isInteger(propNumber)){throw new TypeError("response_id must be an integer")} + const propNumber = Number.parseInt(prop) + if(Number.isNaN(propNumber)){throw new TypeError("response_id must be an integer")} this.response_id=propNumber } } diff --git a/submissions b/submissions index 93108ae..09783b4 100644 Binary files a/submissions and b/submissions differ diff --git a/test.db b/test.db index 187bde0..165ce42 100644 Binary files a/test.db and b/test.db differ diff --git a/test/Data.test.mjs b/test/Data.test.mjs index 0893543..1e7195a 100644 --- a/test/Data.test.mjs +++ b/test/Data.test.mjs @@ -71,4 +71,17 @@ describe("Testing Data object...",function(){ expect(row).to.contain.key('id') } }) + it("getGenresByStoryId() should return an array",async function(){ + const data = new Data(db) + await data.init() + const res = await data.getGenresByStoryId(1) + console.dir(res) + expect(res).to.be.a('array') + }) + it("getGenresByPublicationId() should return an array",async function(){ + const data = new Data(db) + await data.init() + const res = await data.getGenresByPublicationId(1) + expect(res).to.be.a('array') + }) }) \ No newline at end of file diff --git a/test/Entity.test.mjs b/test/Entity.test.mjs index 4be9f09..cee8a44 100644 --- a/test/Entity.test.mjs +++ b/test/Entity.test.mjs @@ -9,7 +9,6 @@ chai.use(chaiAsPromised) describe("tetsing Entity object",async function(){ it("should throw TypeError if passed an invalid data.id",async function(){ expect(()=>{new Entity({id:"string"})}).to.throw(TypeError) - expect(()=>{new Entity({id:1.1})}).to.throw(TypeError) expect(()=>{new Entity({id:{}})}).to.throw(TypeError) expect(()=>{new Entity({id:[]})}).to.throw(TypeError) }) diff --git a/test/Story.test.mjs b/test/Story.test.mjs index 93bc3b1..eebfb24 100644 --- a/test/Story.test.mjs +++ b/test/Story.test.mjs @@ -7,7 +7,7 @@ import Story from "../objects/Story.mjs"; chai.use(chaiAsPromised) describe("testing Story object",function(){ it("should throw TypeError if .word_count is not an integer",function(){ - expect(()=>{new Story({word_count:1.1})}).to.throw(TypeError) + expect(()=>{new Story({word_count:"boo"})}).to.throw(TypeError) }) it("should have .word_count if created with valid data, and .word_count should be an integer",function(){ const story = new Story({word_count:100}) diff --git a/test/endpoints.test.mjs b/test/endpoints.test.mjs index 0f0711e..cf9c02b 100644 --- a/test/endpoints.test.mjs +++ b/test/endpoints.test.mjs @@ -14,7 +14,7 @@ const data = new Data(db) await data.init() app.use(bodyParser.json()) app.use('/api',getEndpoints(data)) -app.use('/api', postEndpoints(db)) +app.use('/api', postEndpoints(db,data)) describe("testing endpoints...",async function(){ @@ -46,7 +46,8 @@ describe("testing /create endpoints", async function(){ describe("/story/create",async function(){ const goodData = { title:"#test", - word_count:111 + word_count:111, + deleted:0 } const badData = { title:1, @@ -83,7 +84,8 @@ describe("testing /create endpoints", async function(){ describe("/publication/create",async function(){ const goodData = { title:"#test", - link:"www.internet.com" + link:"www.internet.com", + deleted:0 } const badData = { title:1, @@ -166,7 +168,8 @@ describe("testing /edit endpoints",async function(){ const goodData = { id:1, title:"#test", - word_count:111 + word_count:111, + deleted:0 } const badData = { id:"string" @@ -210,7 +213,9 @@ describe("testing /edit endpoints",async function(){ const goodData = { id:1, title:"#test", - link:"link" + link:"link", + query_after_days:90, + deleted:0 } const badData = { id:"string" @@ -302,81 +307,5 @@ describe("testing /edit endpoints",async function(){ }) }) -describe("testing /delete endpoints",async function(){ - describe("/story/delete",async function(){ - it("item should be deleted from db",async function(){ - let id = await db('stories'). - insert({ - title:"#test", - word_count:500 - }) - .returning('id') - id=id[0].id - await chai.request(app) - .post('/api/story/delete') - .send({id}) - - const res = await db('stories') - .select('*') - .where('id',id) - expect(res).to.have.lengthOf(0) - }) - - await db('stories') - .where('title','#test') - .del() - }) - describe("/publication/delete",async function(){ - it("item should be deleted from db",async function(){ - let id = await db('pubs'). - insert({ - title:"#test", - link:'link' - }) - .returning('id') - id=id[0].id - - await chai.request(app) - .post('/api/publication/delete') - .send({id}) - - const res = await db('pubs') - .select('*') - .where('id',id) - expect(res).to.have.lengthOf(0) - }) - - await db('pubs') - .where('title','#test') - .del() - }) - describe("/submission/delete",async function(){ - it("item should be deleted from db",async function(){ - let id = await db('subs'). - insert({ - story_id:1, - pub_id:1, - response_id:1, - date_submitted:"1066-01-01", - date_responded:"1066-01-01" - }) - .returning('id') - id=id[0].id - - await chai.request(app) - .post('/api/submission/delete') - .send({id}) - - const res = await db('subs') - .select('*') - .where('id',id) - expect(res).to.have.lengthOf(0) - }) - - await db('subs') - .where('date_submitted','1066-01-01') - .del() - }) -}) }) \ No newline at end of file