From cefd6770de6bad0159a39461e79a8e53698dc254 Mon Sep 17 00:00:00 2001 From: Andrzej Stepien Date: Thu, 14 Sep 2023 16:06:17 +0200 Subject: [PATCH] added genre getting --- logger.mjs | 2 +- objects/Data.mjs | 36 +++++++++++++++- objects/Entity.mjs | 6 ++- objects/Story.mjs | 4 +- objects/Submission.mjs | 12 +++--- submissions | Bin 40960 -> 40960 bytes test.db | Bin 40960 -> 40960 bytes test/Data.test.mjs | 13 ++++++ test/Entity.test.mjs | 1 - test/Story.test.mjs | 2 +- test/endpoints.test.mjs | 91 +++++----------------------------------- 11 files changed, 72 insertions(+), 95 deletions(-) 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 93108aed2164ba2609957103f9f5e90a521b4f06..09783b4477fd4b5b8ddbf2fe3108e5168029fbc9 100644 GIT binary patch delta 186 zcmZoTz|?SnX@az%4FdxM7Z7s*F%u9QPt-A%wPDZ;_u}OdWZ>ZV!@!ry_nIe^=QH;v zE?15}{2rSX6&g4;|KV_D^8#rEf<{wbc5!88#wNp(#H5_mg3_eo`1I7gqSRs}CcATx zt7C|(LWrZ2kE;Tbga#LoGue?>d-4}fjma~4rP+%$GE+3UG#3d3uvd&$cq$iU9=hk-AZ?=?>-&u8vU oT&^5{HY+MLacus>;mXE%Xkz2OMFIhv6${t}7@0P66#UQ!0BC9#TmS$7 diff --git a/test.db b/test.db index 187bde052fb270455193cf7e92fc57b937f29ccc..165ce42c0e64b244eb73c9bd7b453304e8dd8d73 100644 GIT binary patch delta 2864 zcmb_eU2G#)6~1>IkLSlT*V&ENyK9d#v)L`lZYG%@+li4NO=;MzNcd?|s%C{Y&U%s< zc|F0NY&Hnw^}ZA#Ds^QL>P!0s1+^0DMm*7qs!~}Afked?A@v18AAkywSo#23shm4= zXR-nw8QJpn`R@6?@0|N%-+Yt1`6l=L>0|hx2q6jH0=zkRySJyEGDK&#{-k6_ksv%k zs;7RCeJXojz9TIO56GoML%%5;J`hf(@Z+}!k4l4LCR51g@vT$C*T`o-J*|Ib&+8ss zX>0{v_e!&I(9>I;KyP|mjlFiD*R5$7x4pn?(zv(nb@h%P=$*ZGJDFrn4ew{~j*w5H zS8fkpmIkR*rcf;6!Iy>?$wukF31V`{`#@b?VKu?WpsHjF6BGEB9W*xE-tOLJ@5;8< z>3Y3~hM7-2xwN>pq^~VLw!EZ21WZcsWw@wZ82&??9KN6wjoxIdIVDXg%Foa_{vJ|( zL0%_cCy%Qisy|Y%tCRe_;pyD0{Ay-DGp~*+ACcd`Hn_*pfn-v zKFufJE_hI)fsC}6BZG>)}TifyU zw*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|HidSX6sBK4l*- zPdHG+*B>hQNBlkh7{B1&cdxkbyW`G{cr4dc|K9F>rH_tacAk= zd;N!{-Hd+P$S5O`LIbzl;Ze1;%yeP3gG%Z@>__a)&BphS)Z)(EYtN6S(h7>4r(a!NURhZ> z_pX0_#a~}M<3G2sT0{U%;#Xc;^Iu#v~>*_qh?jb)wu2 zbwyR{MjaJ`RpUWp63!<{T8G2c0}O`x z86(j|w9sL0egYOy^URzm4&FFQ#-+L&L^4UES#Ev5fKz#B26mQiYZ6tS6xyguy>Sk# z5(~)_f}I&k{Oo}rm#_zo3r-Nqi0qTK3(%{C+cF}LN!Sb9&91;<3qXsyBz1vj3*b#8 z;nXDT%i<_035~-K#;yq%nTg@>u!O5s2CH?=;+xf_qzo*DMj?zUL{|(tkNG5*M5H3r zH7`lhGv(xR8OZd=_JD^ZSQ!(_UIb^%39YazMmZv*vad0iRDKa6s>Aw+ zcFsdm5b{PooR!HWB6$I)3lX%$Iq;{|?y_AcLz3?8!C^An46f!x^^^wVF9rdDdx>L# z8)+B=kxT_(g!73j%jX~?k}!M_(esapBXE;-H^Yk3l{_N0*qbs{=jPLNn*?l0bxe;& zu%S8jibTW|Fv?QLqIVEya5o}`q>oUXlR_vY=vnEFc^B%iki7^I(S{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