diff --git a/db.mjs b/db.mjs index 02e644b..8f17058 100644 --- a/db.mjs +++ b/db.mjs @@ -14,4 +14,10 @@ export const db = knex({ filename: "./test.db" }, useNullAsDefault: true - }) \ No newline at end of file + }) + +const memoryDB = knex({ + client:'sqlite3', + connection: ':memory:' + }) + diff --git a/objects/Story.mjs b/objects/Story.mjs index c76ee63..4576e1b 100644 --- a/objects/Story.mjs +++ b/objects/Story.mjs @@ -11,6 +11,9 @@ export default class Story extends Title{ get table(){ return 'stories' } + get idName(){ + return 'story_id' + } constructor(data){ super(data) this._word_count=data?.word_count diff --git a/objects/Submission.mjs b/objects/Submission.mjs index d8c8491..da44d8a 100644 --- a/objects/Submission.mjs +++ b/objects/Submission.mjs @@ -38,6 +38,9 @@ export default class Submission extends Entity{ get table(){ return 'subs' } + get idName(){ + return 'sub_id' + } constructor(data){ super(data) this._story_id=data?.story_id diff --git a/objects/Title.mjs b/objects/Title.mjs index 6698c2a..40b4832 100644 --- a/objects/Title.mjs +++ b/objects/Title.mjs @@ -32,4 +32,40 @@ export default class Title extends Entity{ 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) + .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???!!! + }) + } + + + + } + + + + + } \ No newline at end of file diff --git a/submissions b/submissions index 6325a64..e164b8e 100644 Binary files a/submissions and b/submissions differ