sub-manager-backend/objects/Title.mjs

68 lines
1.9 KiB
JavaScript
Raw Normal View History

2023-09-07 09:39:09 +00:00
import Entity from "./Entity.mjs";
import dataValidation from "./dataValidation.mjs";
2024-02-16 10:24:45 +00:00
import logger from "../logger.mjs";
2023-09-07 09:39:09 +00:00
//THIS CLASS WILL HANDLE JUNCTION TABLE STUFF
2024-02-16 10:24:45 +00:00
export default class Title extends Entity {
2023-09-27 09:44:44 +00:00
#genres
2024-02-16 10:24:45 +00:00
set _title(prop) {
if (prop) {
if (!dataValidation.isString(prop)) { throw new TypeError("title must be a string") }
this.title = prop
2023-09-07 09:39:09 +00:00
}
2023-09-07 10:07:34 +00:00
}
2024-02-16 10:24:45 +00:00
set _genres(prop) {
if (prop) {
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
}
}
2024-02-16 10:24:45 +00:00
set _deleted(prop) {
if (prop) {
if (prop === 1 || prop === 0) {
this.deleted = prop
2023-09-14 09:40:19 +00:00
}
}
}
2024-02-16 10:24:45 +00:00
get genres() {
2023-09-27 09:44:44 +00:00
return this.#genres
}
2023-09-07 10:07:34 +00:00
2024-02-16 10:24:45 +00:00
constructor(data) {
2023-09-07 10:07:34 +00:00
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
2024-02-16 10:24:45 +00:00
async updateGenres(db, genres) {
const table = this.table + '_genres'
await genres.forEach(async (e, i) => {
//INSERT TRUE
const genreName = genres[i]
if (this.#genres[genreName]) {
await db(table).whereNotExists(
db.select('*')
.from(table)
.where(this.idName, this.id)
.andWhere('genre_id', i)
)
.insert({
[this.idName]: this.id,
genre_id: i
})
} else {
//DELETE FALSE
const res = await db(table)
.where(this.idName, this.id)
.andWhere('genre_id', i)
2023-11-13 11:09:47 +00:00
.del()
2024-02-16 10:24:45 +00:00
console.log("RES: "+res)
2023-11-13 11:09:47 +00:00
}
2024-02-16 10:24:45 +00:00
})
2023-11-13 11:09:47 +00:00
}
2023-09-07 09:39:09 +00:00
}