sub-manager-backend/objects/Title.mjs

71 lines
1.8 KiB
JavaScript
Raw Normal View History

2023-09-07 09:39:09 +00:00
import Entity from "./Entity.mjs";
import dataValidation from "./dataValidation.mjs";
2023-09-07 09:39:09 +00:00
//THIS CLASS WILL HANDLE JUNCTION TABLE STUFF
export default class Title extends Entity{
2023-09-27 09:44:44 +00:00
#genres
2023-09-07 10:07:34 +00:00
set _title(prop){
if(prop){
if(!dataValidation.isString(prop)){throw new TypeError("title must be a string")}
2023-09-07 10:07:34 +00:00
this.title=prop
2023-09-07 09:39:09 +00:00
}
2023-09-07 10:07:34 +00:00
}
set _genres(prop){
if(prop){
2023-09-27 09:44:44 +00:00
if(!dataValidation.isObject(prop)){throw new TypeError(`genres must be an object; this is a ${typeof prop}`)}
this.#genres=prop
2023-09-07 09:39:09 +00:00
}
}
2023-09-14 09:40:19 +00:00
set _deleted(prop){
if(prop){
if(prop===1 || prop===0){
this.deleted=prop
}
}
}
2023-09-27 09:44:44 +00:00
get genres(){
return this.#genres
}
2023-09-07 10:07:34 +00:00
constructor(data){
super(data)
this._title = data?.title
this._genres = data?.genres
2023-09-14 09:40:19 +00:00
this._deleted = data?.deleted
2023-09-07 10:07:34 +00:00
}
2023-11-13 11:09:47 +00:00
async updateGenres(db,genres){
const table = this.table+'_genres'
const relevantEntries = await db(table)
.select('*')
.where(this.idName,this.id)
//DELETE FALSES
const truesWithEntry= []
for (const entry of relevantEntries) {
const genreName = genres[entry.genre_id]
if(this.#genres[genreName]===false){
await db(table)
.where('id',entry.id)
.del()
continue
}
truesWithEntry.push(entry.genre_id)
}
//INSERT TRUES
for (const genre in this.#genres){
if(truesWithEntry.includes(genre)){continue}
await db(table)
.insert({
[this.idName]:this.id,
genre_id://GET GENRE ID???!!!
})
}
}
2023-09-07 09:39:09 +00:00
}