Fix:Updating author name to update author name on each library item

This commit is contained in:
advplyr 2022-03-18 09:38:36 -05:00
parent f00b120e96
commit 69fcb103e4
2 changed files with 25 additions and 1 deletions

View File

@ -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(),

View File

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