From 9f60017cfea0ea68525e815051e3865156052ef2 Mon Sep 17 00:00:00 2001 From: advplyr Date: Sun, 1 Sep 2024 15:26:43 -0500 Subject: [PATCH] Update:Remove oldSeries model --- server/Database.js | 5 -- server/controllers/SeriesController.js | 23 +++++--- server/models/Series.js | 31 ---------- server/objects/entities/Series.js | 79 -------------------------- server/utils/queries/libraryFilters.js | 2 +- 5 files changed, 17 insertions(+), 123 deletions(-) delete mode 100644 server/objects/entities/Series.js diff --git a/server/Database.js b/server/Database.js index 465ae9f7..2e109fa8 100644 --- a/server/Database.js +++ b/server/Database.js @@ -442,11 +442,6 @@ class Database { await this.models.feed.removeById(feedId) } - updateSeries(oldSeries) { - if (!this.sequelize) return false - return this.models.series.updateFromOld(oldSeries) - } - async createBulkBookAuthors(bookAuthors) { if (!this.sequelize) return false await this.models.bookAuthor.bulkCreate(bookAuthors) diff --git a/server/controllers/SeriesController.js b/server/controllers/SeriesController.js index 1b5b6708..5d761ba9 100644 --- a/server/controllers/SeriesController.js +++ b/server/controllers/SeriesController.js @@ -59,19 +59,28 @@ class SeriesController { } /** - * TODO: Update to use new model + * TODO: Currently unused in the client, should check for duplicate name * * @param {SeriesControllerRequest} req * @param {Response} res */ async update(req, res) { - const oldSeries = req.series.getOldSeries() - const hasUpdated = oldSeries.update(req.body) - if (hasUpdated) { - await Database.updateSeries(oldSeries) - SocketAuthority.emitter('series_updated', oldSeries.toJSON()) + const keysToUpdate = ['name', 'description'] + const payload = {} + for (const key of keysToUpdate) { + if (req.body[key] !== undefined && typeof req.body[key] === 'string') { + payload[key] = req.body[key] + } } - res.json(oldSeries.toJSON()) + if (!Object.keys(payload).length) { + return res.status(400).send('No valid fields to update') + } + req.series.set(payload) + if (req.series.changed()) { + await req.series.save() + SocketAuthority.emitter('series_updated', req.series.toOldJSON()) + } + res.json(req.series.toOldJSON()) } /** diff --git a/server/models/Series.js b/server/models/Series.js index 493de150..c57a1a11 100644 --- a/server/models/Series.js +++ b/server/models/Series.js @@ -1,6 +1,5 @@ const { DataTypes, Model, where, fn, col } = require('sequelize') -const oldSeries = require('../objects/entities/Series') const { getTitlePrefixAtEnd } = require('../utils/index') class Series extends Model { @@ -23,36 +22,6 @@ class Series extends Model { this.updatedAt } - getOldSeries() { - return new oldSeries({ - id: this.id, - name: this.name, - description: this.description, - libraryId: this.libraryId, - addedAt: this.createdAt.valueOf(), - updatedAt: this.updatedAt.valueOf() - }) - } - - static updateFromOld(oldSeries) { - const series = this.getFromOld(oldSeries) - return this.update(series, { - where: { - id: series.id - } - }) - } - - static getFromOld(oldSeries) { - return { - id: oldSeries.id, - name: oldSeries.name, - nameIgnorePrefix: oldSeries.nameIgnorePrefix, - description: oldSeries.description, - libraryId: oldSeries.libraryId - } - } - /** * Check if series exists * @param {string} seriesId diff --git a/server/objects/entities/Series.js b/server/objects/entities/Series.js deleted file mode 100644 index 59987f6f..00000000 --- a/server/objects/entities/Series.js +++ /dev/null @@ -1,79 +0,0 @@ -const uuidv4 = require("uuid").v4 -const { getTitleIgnorePrefix, getTitlePrefixAtEnd } = require('../../utils/index') - -class Series { - constructor(series) { - this.id = null - this.name = null - this.description = null - this.addedAt = null - this.updatedAt = null - this.libraryId = null - - if (series) { - this.construct(series) - } - } - - construct(series) { - this.id = series.id - this.name = series.name - this.description = series.description || null - this.addedAt = series.addedAt - this.updatedAt = series.updatedAt - this.libraryId = series.libraryId - } - - get nameIgnorePrefix() { - if (!this.name) return '' - return getTitleIgnorePrefix(this.name) - } - - toJSON() { - return { - id: this.id, - name: this.name, - nameIgnorePrefix: getTitlePrefixAtEnd(this.name), - description: this.description, - addedAt: this.addedAt, - updatedAt: this.updatedAt, - libraryId: this.libraryId - } - } - - toJSONMinimal(sequence) { - return { - id: this.id, - name: this.name, - sequence - } - } - - setData(data, libraryId) { - this.id = uuidv4() - this.name = data.name - this.description = data.description || null - this.addedAt = Date.now() - this.updatedAt = Date.now() - this.libraryId = libraryId - } - - update(series) { - if (!series) return false - const keysToUpdate = ['name', 'description'] - let hasUpdated = false - for (const key of keysToUpdate) { - if (series[key] !== undefined && series[key] !== this[key]) { - this[key] = series[key] - hasUpdated = true - } - } - return hasUpdated - } - - checkNameEquals(name) { - if (!name || !this.name) return false - return this.name.toLowerCase() == name.toLowerCase().trim() - } -} -module.exports = Series \ No newline at end of file diff --git a/server/utils/queries/libraryFilters.js b/server/utils/queries/libraryFilters.js index a8b0cc46..5c7b7dc0 100644 --- a/server/utils/queries/libraryFilters.js +++ b/server/utils/queries/libraryFilters.js @@ -196,7 +196,7 @@ module.exports = { * @param {import('../../models/User')} user * @param {string[]} include * @param {number} limit - * @returns {{ series:import('../../objects/entities/Series')[], count:number}} + * @returns {{ series:any[], count:number}} */ async getSeriesMostRecentlyAdded(library, user, include, limit) { if (!library.isBook) return { series: [], count: 0 }