From 69fcb103e4cdd1cd758d5822120a4dec47cdd7a7 Mon Sep 17 00:00:00 2001 From: advplyr Date: Fri, 18 Mar 2022 09:38:36 -0500 Subject: [PATCH] Fix:Updating author name to update author name on each library item --- server/controllers/AuthorController.js | 18 +++++++++++++++++- server/objects/metadata/BookMetadata.js | 8 ++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/server/controllers/AuthorController.js b/server/controllers/AuthorController.js index ea786658..f009144c 100644 --- a/server/controllers/AuthorController.js +++ b/server/controllers/AuthorController.js @@ -20,10 +20,26 @@ class AuthorController { } } + var authorNameUpdate = payload.name !== undefined && payload.name !== req.author.name + var hasUpdated = req.author.update(payload) if (hasUpdated) { + if (authorNameUpdate) { // Update author name on all books + var itemsWithAuthor = this.db.libraryItems.filter(li => li.mediaType === 'book' && li.media.metadata.hasAuthor(req.author.id)) + itemsWithAuthor.forEach(libraryItem => { + libraryItem.media.metadata.updateAuthor(req.author) + }) + if (itemsWithAuthor.length) { + await this.db.updateLibraryItems(itemsWithAuthor) + this.emitter('items_updated', itemsWithAuthor.map(li => li.toJSONExpanded())) + } + } + await this.db.updateEntity('author', req.author) - this.emitter('author_updated', req.author.toJSON()) + var numBooks = this.db.libraryItems.filter(li => { + return li.media.metadata.hasAuthor && li.media.metadata.hasAuthor(req.author.id) + }).length + this.emitter('author_updated', req.author.toJSONExpanded(numBooks)) } res.json({ author: req.author.toJSON(), diff --git a/server/objects/metadata/BookMetadata.js b/server/objects/metadata/BookMetadata.js index 89dcd334..5d235b88 100644 --- a/server/objects/metadata/BookMetadata.js +++ b/server/objects/metadata/BookMetadata.js @@ -140,6 +140,14 @@ class BookMetadata { return hasUpdates } + // Updates author name + updateAuthor(updatedAuthor) { + var author = this.authors.find(au => au.id === updatedAuthor.id) + if (!author || author.name == updatedAuthor.name) return false + author.name = updatedAuthor.name + return true + } + setData(scanMediaData = {}) { this.title = scanMediaData.title || null this.subtitle = scanMediaData.subtitle || null