Update:Remove oldSeries model

This commit is contained in:
advplyr 2024-09-01 15:26:43 -05:00
parent b6a86d11d2
commit 9f60017cfe
5 changed files with 17 additions and 123 deletions

View File

@ -442,11 +442,6 @@ class Database {
await this.models.feed.removeById(feedId) await this.models.feed.removeById(feedId)
} }
updateSeries(oldSeries) {
if (!this.sequelize) return false
return this.models.series.updateFromOld(oldSeries)
}
async createBulkBookAuthors(bookAuthors) { async createBulkBookAuthors(bookAuthors) {
if (!this.sequelize) return false if (!this.sequelize) return false
await this.models.bookAuthor.bulkCreate(bookAuthors) await this.models.bookAuthor.bulkCreate(bookAuthors)

View File

@ -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 {SeriesControllerRequest} req
* @param {Response} res * @param {Response} res
*/ */
async update(req, res) { async update(req, res) {
const oldSeries = req.series.getOldSeries() const keysToUpdate = ['name', 'description']
const hasUpdated = oldSeries.update(req.body) const payload = {}
if (hasUpdated) { for (const key of keysToUpdate) {
await Database.updateSeries(oldSeries) if (req.body[key] !== undefined && typeof req.body[key] === 'string') {
SocketAuthority.emitter('series_updated', oldSeries.toJSON()) 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())
} }
/** /**

View File

@ -1,6 +1,5 @@
const { DataTypes, Model, where, fn, col } = require('sequelize') const { DataTypes, Model, where, fn, col } = require('sequelize')
const oldSeries = require('../objects/entities/Series')
const { getTitlePrefixAtEnd } = require('../utils/index') const { getTitlePrefixAtEnd } = require('../utils/index')
class Series extends Model { class Series extends Model {
@ -23,36 +22,6 @@ class Series extends Model {
this.updatedAt 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 * Check if series exists
* @param {string} seriesId * @param {string} seriesId

View File

@ -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

View File

@ -196,7 +196,7 @@ module.exports = {
* @param {import('../../models/User')} user * @param {import('../../models/User')} user
* @param {string[]} include * @param {string[]} include
* @param {number} limit * @param {number} limit
* @returns {{ series:import('../../objects/entities/Series')[], count:number}} * @returns {{ series:any[], count:number}}
*/ */
async getSeriesMostRecentlyAdded(library, user, include, limit) { async getSeriesMostRecentlyAdded(library, user, include, limit) {
if (!library.isBook) return { series: [], count: 0 } if (!library.isBook) return { series: [], count: 0 }