sub-manager-backend/objects/Data.mjs

90 lines
2.5 KiB
JavaScript
Raw Normal View History

2023-09-06 16:26:15 +00:00
export class Data {
#db
constructor(db) {
this.#db = db
}
async init() {
this.submissions = await this.getSubmissions()
2023-09-06 16:26:15 +00:00
this.stories = await this.getStories()
this.stories.map(row=>{
row.submissions=this.getSubmissionsByStoryId(row.id)
2023-09-14 14:06:17 +00:00
})
2023-09-06 16:26:15 +00:00
this.publications = await this.getPublications()
this.publications.map(row=>{
row.submissions=this.getSubmissionsByPublicationId(row.id)
})
2023-09-11 22:00:09 +00:00
this.responses = await this.getResponses()
2023-09-14 14:06:17 +00:00
this.genres = await this.getGenres()
return this
2023-09-06 16:26:15 +00:00
}
async getStories() {
return this.#db('stories')
2023-09-14 09:40:19 +00:00
.select('id','title','word_count','deleted')
2023-09-06 16:26:15 +00:00
}
async getPublications() {
return this.#db('pubs')
.select('*')
}
async getSubmissions() {
return this.#db('subs')
.join('stories', 'subs.story_id', 'stories.id')
.join('pubs', 'subs.pub_id', 'pubs.id')
.join('responses', 'subs.response_id', 'responses.id')
.select('subs.id',
'subs.story_id',
'stories.title as story',
'subs.pub_id',
'pubs.title as publication',
'subs.date_submitted',
'subs.date_responded',
'subs.response_id',
'responses.response'
)
}
2023-09-11 22:00:09 +00:00
async getResponses() {
return this.#db('responses')
.select('*')
}
2023-09-14 14:06:17 +00:00
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)
}
getSubmissionsByPublicationId(id){
return this.submissions.filter(row=>row.pub_id==id)
}
2023-09-14 14:06:17 +00:00
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
}
2023-09-06 16:26:15 +00:00
}