Update library API endpoints to load library items from db

This commit is contained in:
advplyr 2023-08-13 17:45:53 -05:00
parent 3651fffbee
commit 6d6e8613cf
8 changed files with 352 additions and 114 deletions

View file

@ -122,5 +122,45 @@ module.exports = {
libraryItems.push(libraryItem)
}
return libraryItems
},
/**
* Get all library items that have narrators
* @param {string[]} narrators
* @returns {Promise<LibraryItem[]>}
*/
async getAllLibraryItemsWithNarrators(narrators) {
const libraryItems = []
const booksWithGenre = await Database.models.book.findAll({
where: Sequelize.where(Sequelize.literal(`(SELECT count(*) FROM json_each(narrators) WHERE json_valid(narrators) AND json_each.value IN (:narrators))`), {
[Sequelize.Op.gte]: 1
}),
replacements: {
narrators
},
include: [
{
model: Database.models.libraryItem
},
{
model: Database.models.author,
through: {
attributes: []
}
},
{
model: Database.models.series,
through: {
attributes: ['sequence']
}
}
]
})
for (const book of booksWithGenre) {
const libraryItem = book.libraryItem
libraryItem.media = book
libraryItems.push(libraryItem)
}
return libraryItems
}
}