added genre getting

This commit is contained in:
Andrzej Stepien 2023-09-14 16:06:17 +02:00
parent 051b8d92c6
commit cefd6770de
11 changed files with 72 additions and 95 deletions

View File

@ -16,7 +16,7 @@ const __dirname = path.dirname(__filename);
// })
export default pino(
{
level: 'trace',
level: 'fatal',
formatters: {
level: (label) => {
return { level: label.toUpperCase() };

View File

@ -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
}
}

View File

@ -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
}
}

View File

@ -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
}
}

View File

@ -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
}
}

Binary file not shown.

BIN
test.db

Binary file not shown.

View File

@ -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')
})
})

View File

@ -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)
})

View File

@ -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})

View File

@ -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()
})
})
})