From 00aaf29a97095b700f04e6131e22fa06f5b6a7e6 Mon Sep 17 00:00:00 2001 From: Andrzej Stepien Date: Fri, 16 Feb 2024 11:24:45 +0100 Subject: [PATCH] update junctions --- logger.mjs | 2 +- objects/Endpoints.mjs | 2 +- objects/Entity.mjs | 25 ++++++++----- objects/Title.mjs | 81 ++++++++++++++++++++---------------------- submissions | Bin 40960 -> 40960 bytes subs | 0 6 files changed, 58 insertions(+), 52 deletions(-) delete mode 100644 subs diff --git a/logger.mjs b/logger.mjs index 274931c..6f71e5d 100644 --- a/logger.mjs +++ b/logger.mjs @@ -16,7 +16,7 @@ const __dirname = path.dirname(__filename); // }) export default pino( { - level: 'error', + level: 'fatal', formatters: { level: (label) => { return { level: label.toUpperCase() }; diff --git a/objects/Endpoints.mjs b/objects/Endpoints.mjs index 9d12c3d..eea59c5 100644 --- a/objects/Endpoints.mjs +++ b/objects/Endpoints.mjs @@ -64,7 +64,7 @@ export const postEndpoints = (db,data) => { try { logger.trace({data:req.body},"POST request received") const entity = new Entity(req.body) - await entity[method](db) + await entity[method](db,data) res.sendStatus(200) data.init() return diff --git a/objects/Entity.mjs b/objects/Entity.mjs index 6825dc2..85a9757 100644 --- a/objects/Entity.mjs +++ b/objects/Entity.mjs @@ -13,21 +13,30 @@ export default class Entity{ this._id = data?.id } - - async insert(db){ - return db(this.table) - .insert(this) + async updateAppropriateJunctions(db,data){ + if(typeof this.updateGenres === "function" ){ + await this.updateGenres(db,data.genres) + } } - async update(db){ - return db(this.table) + + async insert(db,data){ + this.id = await db(this.table) + .insert(this) + .returning("id") + await this.updateAppropriateJunctions(db,data) + } + async update(db,data){ + await db(this.table) .where('id',this.id) .update(this) + await this.updateAppropriateJunctions(db,data) } - async del(db){ + async del(db,data){ if(!this?.id){throw new Error("cannot delete without an id!")} - return db(this.table) + await db(this.table) .where('id',this.id) .del() + //RUN JUNCTION OPS IF APPROPRIATE } } diff --git a/objects/Title.mjs b/objects/Title.mjs index 40b4832..02333fc 100644 --- a/objects/Title.mjs +++ b/objects/Title.mjs @@ -1,67 +1,64 @@ import Entity from "./Entity.mjs"; import dataValidation from "./dataValidation.mjs"; +import logger from "../logger.mjs"; //THIS CLASS WILL HANDLE JUNCTION TABLE STUFF -export default class Title extends Entity{ +export default class Title extends Entity { #genres - set _title(prop){ - if(prop){ - if(!dataValidation.isString(prop)){throw new TypeError("title must be a string")} - this.title=prop + set _title(prop) { + if (prop) { + if (!dataValidation.isString(prop)) { throw new TypeError("title must be a string") } + this.title = prop } } - 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 + 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 } } - set _deleted(prop){ - if(prop){ - if(prop===1 || prop===0){ - this.deleted=prop + set _deleted(prop) { + if (prop) { + if (prop === 1 || prop === 0) { + this.deleted = prop } } } - get genres(){ + get genres() { return this.#genres } - constructor(data){ + constructor(data) { super(data) this._title = data?.title this._genres = data?.genres this._deleted = data?.deleted } - 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) + 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) .del() - continue + console.log("RES: "+res) } - 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???!!! - }) - } - - - + }) } diff --git a/submissions b/submissions index e164b8e2e65ed10d40fdd4a7e4204bd728a86621..9be69fd04a29f40eca7794150ccae6efefe5db98 100644 GIT binary patch delta 512 zcmZvY&nrYx6vyv7cix@%p4>Cc_%VLeUC3&XSg4U16FbVUQc65|A&lwGP_yu3B^yPP zrIIXd{K&>e%F0gi4_NpIC_B!5C`za9=YGHUoO{l_TQ{(E1ABD)G^aTTFi7 zG49AGte02VjxTsCpD!v@uPnJe-EsNG;?QF8^-*_WRu$$wHMo+^7d*do)svn~Fb9$_ zJ~m&+O$HrSd2+~K$>kQZb80mgWM^C-!*teY@w*W(TTp#&5to<=$|g5uITM4v%J}Z` z%#xQ+s4;huIuuoZ(aV=cQv=3h=_u8iV2nkfGWC-It<;WFSoUg9a9 z;2|F1E^gxnu3u$k4FffLX5nFkMc)9o@ttg6U}6 fp-*WwY|*TXXt1V$oLoIHa1jp-(emXV??pZV>Of!8Tgp2nZiRdQWXT*`Po?+m}EJaj|QdYl_N0J$+!