mirror of
				https://github.com/advplyr/audiobookshelf.git
				synced 2025-10-27 11:18:14 +01:00 
			
		
		
		
	Update:Remove oldSeries model
This commit is contained in:
		
							parent
							
								
									b6a86d11d2
								
							
						
					
					
						commit
						9f60017cfe
					
				| @ -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) | ||||
|  | ||||
| @ -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()) | ||||
|   } | ||||
| 
 | ||||
|   /** | ||||
|  | ||||
| @ -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 | ||||
|  | ||||
| @ -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 | ||||
| @ -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 } | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user