From 1e9470b8402099d3c05e46fb83b2579a50890971 Mon Sep 17 00:00:00 2001 From: advplyr Date: Sat, 4 Jan 2025 15:59:40 -0600 Subject: [PATCH] Update AuthorController library item usage and remove unused --- server/Database.js | 22 ------------ server/controllers/AuthorController.js | 23 +++++++----- server/models/LibraryItem.js | 48 ++------------------------ server/objects/LibraryItem.js | 11 ------ 4 files changed, 16 insertions(+), 88 deletions(-) diff --git a/server/Database.js b/server/Database.js index 45af5248..61385981 100644 --- a/server/Database.js +++ b/server/Database.js @@ -401,17 +401,6 @@ class Database { return this.models.setting.updateSettingObj(settings.toJSON()) } - updateBulkBooks(oldBooks) { - if (!this.sequelize) return false - return Promise.all(oldBooks.map((oldBook) => this.models.book.saveFromOld(oldBook))) - } - - async createLibraryItem(oldLibraryItem) { - if (!this.sequelize) return false - await oldLibraryItem.saveMetadata() - await this.models.libraryItem.fullCreateFromOld(oldLibraryItem) - } - /** * Save metadata file and update library item * @@ -429,17 +418,6 @@ class Database { return updated } - async createBulkBookAuthors(bookAuthors) { - if (!this.sequelize) return false - await this.models.bookAuthor.bulkCreate(bookAuthors) - } - - async removeBulkBookAuthors(authorId = null, bookId = null) { - if (!this.sequelize) return false - if (!authorId && !bookId) return - await this.models.bookAuthor.removeByIds(authorId, bookId) - } - getPlaybackSessions(where = null) { if (!this.sequelize) return false return this.models.playbackSession.getOldPlaybackSessions(where) diff --git a/server/controllers/AuthorController.js b/server/controllers/AuthorController.js index a6b5a2f4..31c94307 100644 --- a/server/controllers/AuthorController.js +++ b/server/controllers/AuthorController.js @@ -44,16 +44,21 @@ class AuthorController { // Used on author landing page to include library items and items grouped in series if (include.includes('items')) { - authorJson.libraryItems = await Database.libraryItemModel.getForAuthor(req.author, req.user) + const libraryItems = await Database.libraryItemModel.getForAuthor(req.author, req.user) if (include.includes('series')) { const seriesMap = {} // Group items into series - authorJson.libraryItems.forEach((li) => { - if (li.media.metadata.series) { - li.media.metadata.series.forEach((series) => { - const itemWithSeries = li.toJSONMinified() - itemWithSeries.media.metadata.series = series + libraryItems.forEach((li) => { + if (li.media.series?.length) { + li.media.series.forEach((series) => { + const itemWithSeries = li.toOldJSONMinified() + itemWithSeries.media.metadata.series = { + id: series.id, + name: series.name, + nameIgnorePrefix: series.nameIgnorePrefix, + sequence: series.bookSeries.sequence + } if (seriesMap[series.id]) { seriesMap[series.id].items.push(itemWithSeries) @@ -76,7 +81,7 @@ class AuthorController { } // Minify library items - authorJson.libraryItems = authorJson.libraryItems.map((li) => li.toJSONMinified()) + authorJson.libraryItems = libraryItems.map((li) => li.toOldJSONMinified()) } return res.json(authorJson) @@ -142,8 +147,8 @@ class AuthorController { }) }) if (libraryItems.length) { - await Database.removeBulkBookAuthors(req.author.id) // Remove all old BookAuthor - await Database.createBulkBookAuthors(bookAuthorsToCreate) // Create all new BookAuthor + await Database.bookAuthorModel.removeByIds(req.author.id) // Remove all old BookAuthor + await Database.bookAuthorModel.bulkCreate(bookAuthorsToCreate) // Create all new BookAuthor for (const libraryItem of libraryItems) { await libraryItem.saveMetadataFile() } diff --git a/server/models/LibraryItem.js b/server/models/LibraryItem.js index 0d87a328..31a6a0b4 100644 --- a/server/models/LibraryItem.js +++ b/server/models/LibraryItem.js @@ -160,40 +160,6 @@ class LibraryItem extends Model { }) } - static async fullCreateFromOld(oldLibraryItem) { - const newLibraryItem = await this.create(this.getFromOld(oldLibraryItem)) - - if (oldLibraryItem.mediaType === 'book') { - const bookObj = this.sequelize.models.book.getFromOld(oldLibraryItem.media) - bookObj.libraryItemId = newLibraryItem.id - const newBook = await this.sequelize.models.book.create(bookObj) - - const oldBookAuthors = oldLibraryItem.media.metadata.authors || [] - const oldBookSeriesAll = oldLibraryItem.media.metadata.series || [] - - for (const oldBookAuthor of oldBookAuthors) { - await this.sequelize.models.bookAuthor.create({ authorId: oldBookAuthor.id, bookId: newBook.id }) - } - for (const oldSeries of oldBookSeriesAll) { - await this.sequelize.models.bookSeries.create({ seriesId: oldSeries.id, bookId: newBook.id, sequence: oldSeries.sequence }) - } - } else if (oldLibraryItem.mediaType === 'podcast') { - const podcastObj = this.sequelize.models.podcast.getFromOld(oldLibraryItem.media) - podcastObj.libraryItemId = newLibraryItem.id - const newPodcast = await this.sequelize.models.podcast.create(podcastObj) - - const oldEpisodes = oldLibraryItem.media.episodes || [] - for (const oldEpisode of oldEpisodes) { - const episodeObj = this.sequelize.models.podcastEpisode.getFromOld(oldEpisode) - episodeObj.libraryItemId = newLibraryItem.id - episodeObj.podcastId = newPodcast.id - await this.sequelize.models.podcastEpisode.create(episodeObj) - } - } - - return newLibraryItem - } - /** * Updates libraryItem, book, authors and series from old library item * @@ -819,21 +785,11 @@ class LibraryItem extends Model { * Get book library items for author, optional use user permissions * @param {import('./Author')} author * @param {import('./User')} user - * @returns {Promise} + * @returns {Promise} */ static async getForAuthor(author, user = null) { const { libraryItems } = await libraryFilters.getLibraryItemsForAuthor(author, user, undefined, undefined) - return libraryItems.map((li) => this.getOldLibraryItem(li)) - } - - /** - * Get book library items in a collection - * @param {oldCollection} collection - * @returns {Promise} - */ - static async getForCollection(collection) { - const libraryItems = await libraryFilters.getLibraryItemsForCollection(collection) - return libraryItems.map((li) => this.getOldLibraryItem(li)) + return libraryItems } /** diff --git a/server/objects/LibraryItem.js b/server/objects/LibraryItem.js index 17d7484c..d955356e 100644 --- a/server/objects/LibraryItem.js +++ b/server/objects/LibraryItem.js @@ -269,16 +269,5 @@ class LibraryItem { this.isSavingMetadata = false }) } - - removeLibraryFile(ino) { - if (!ino) return false - const libraryFile = this.libraryFiles.find((lf) => lf.ino === ino) - if (libraryFile) { - this.libraryFiles = this.libraryFiles.filter((lf) => lf.ino !== ino) - this.updatedAt = Date.now() - return true - } - return false - } } module.exports = LibraryItem