Fix:Get library items endpoint limit & total entities count

This commit is contained in:
advplyr 2022-11-13 13:25:20 -06:00
parent 31be2ba4fb
commit 8f83752abc
2 changed files with 25 additions and 23 deletions

View File

@ -1,15 +1,12 @@
<template> <template>
<div id="bookshelf" class="w-full overflow-y-auto"> <div id="bookshelf" class="w-full overflow-y-auto">
<template v-for="shelf in totalShelves"> <template v-for="shelf in totalShelves">
<div :key="shelf" :id="`shelf-${shelf - 1}`" class="w-full px-4 sm:px-8 relative" <div :key="shelf" :id="`shelf-${shelf - 1}`" class="w-full px-4 sm:px-8 relative" :class="{ bookshelfRow: !isAlternativeBookshelfView }" :style="{ height: shelfHeight + 'px' }">
:class="{ bookshelfRow: !isAlternativeBookshelfView }" :style="{ height: shelfHeight + 'px' }"> <div v-if="!isAlternativeBookshelfView" class="bookshelfDivider w-full absolute bottom-0 left-0 right-0 z-20" :class="`h-${shelfDividerHeightIndex}`" />
<div v-if="!isAlternativeBookshelfView" class="bookshelfDivider w-full absolute bottom-0 left-0 right-0 z-20"
:class="`h-${shelfDividerHeightIndex}`" />
</div> </div>
</template> </template>
<div v-if="initialized && !totalShelves && !hasFilter && entityName === 'books'" <div v-if="initialized && !totalShelves && !hasFilter && entityName === 'books'" class="w-full flex flex-col items-center justify-center py-12">
class="w-full flex flex-col items-center justify-center py-12">
<p class="text-center text-2xl font-book mb-4 py-4">{{ libraryName }} Library is empty!</p> <p class="text-center text-2xl font-book mb-4 py-4">{{ libraryName }} Library is empty!</p>
<div v-if="userIsAdminOrUp" class="flex"> <div v-if="userIsAdminOrUp" class="flex">
<ui-btn to="/config" color="primary" class="w-52 mr-2">Configure Scanner</ui-btn> <ui-btn to="/config" color="primary" class="w-52 mr-2">Configure Scanner</ui-btn>
@ -335,20 +332,9 @@ export default {
} }
} }
this.$eventBus.$emit('bookshelf-total-entities', this.getEntitiesCount()) this.$eventBus.$emit('bookshelf-total-entities', this.totalEntities)
} }
}, },
getEntitiesCount() {
let uniqueEntities = new Set()
this.entities.forEach(entity => {
if (entity.collapsedSeries) {
entity.collapsedSeries.libraryItemIds.forEach(uniqueEntities.add, uniqueEntities)
} else {
uniqueEntities.add(entity.id)
}
});
return uniqueEntities.size
},
loadPage(page) { loadPage(page) {
this.pagesLoaded[page] = true this.pagesLoaded[page] = true
this.fetchEntites(page) this.fetchEntites(page)
@ -531,8 +517,8 @@ export default {
var indexOf = this.entities.findIndex((ent) => ent && ent.id === libraryItem.id) var indexOf = this.entities.findIndex((ent) => ent && ent.id === libraryItem.id)
if (indexOf >= 0) { if (indexOf >= 0) {
this.entities = this.entities.filter((ent) => ent.id !== libraryItem.id) this.entities = this.entities.filter((ent) => ent.id !== libraryItem.id)
this.totalEntities = this.entities.length this.totalEntities--
this.$eventBus.$emit('bookshelf-total-entities', this.getEntitiesCount()) this.$eventBus.$emit('bookshelf-total-entities', this.totalEntities)
this.executeRebuild() this.executeRebuild()
} }
} }
@ -569,8 +555,8 @@ export default {
var indexOf = this.entities.findIndex((ent) => ent && ent.id === collection.id) var indexOf = this.entities.findIndex((ent) => ent && ent.id === collection.id)
if (indexOf >= 0) { if (indexOf >= 0) {
this.entities = this.entities.filter((ent) => ent.id !== collection.id) this.entities = this.entities.filter((ent) => ent.id !== collection.id)
this.totalEntities = this.entities.length this.totalEntities--
this.$eventBus.$emit('bookshelf-total-entities', this.getEntitiesCount()) this.$eventBus.$emit('bookshelf-total-entities', this.totalEntities)
this.executeRebuild() this.executeRebuild()
} }
}, },

View File

@ -188,7 +188,17 @@ class LibraryController {
if (!(collapsedItems.length == 1 && collapsedItems[0].collapsedSeries)) { if (!(collapsedItems.length == 1 && collapsedItems[0].collapsedSeries)) {
libraryItems = collapsedItems libraryItems = collapsedItems
payload.total = libraryItems.length
// Get accurate total entities
let uniqueEntities = new Set()
libraryItems.forEach((item) => {
if (item.collapsedSeries) {
item.collapsedSeries.books.forEach(book => uniqueEntities.add(book.id))
} else {
uniqueEntities.add(item.id)
}
})
payload.total = uniqueEntities.size
} }
} }
@ -265,6 +275,12 @@ class LibraryController {
libraryItems = naturalSort(libraryItems).by(sortArray) libraryItems = naturalSort(libraryItems).by(sortArray)
} }
// Step 3.5: Limit items
if (payload.limit) {
var startIndex = payload.page * payload.limit
libraryItems = libraryItems.slice(startIndex, startIndex + payload.limit)
}
// Step 4 - Transform the items to pass to the client side // Step 4 - Transform the items to pass to the client side
payload.results = libraryItems.map(li => { payload.results = libraryItems.map(li => {
let json = payload.minified ? li.toJSONMinified() : li.toJSON() let json = payload.minified ? li.toJSONMinified() : li.toJSON()