From 66f3d80c9eda0b0f9d45241cc8c6eddf783d25a5 Mon Sep 17 00:00:00 2001 From: Gauthier Roebroeck Date: Mon, 16 Dec 2019 18:22:49 +0800 Subject: [PATCH] revamp logic to getAll series to reduce the database load --- .../interfaces/web/rest/SeriesController.kt | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/komga/src/main/kotlin/org/gotson/komga/interfaces/web/rest/SeriesController.kt b/komga/src/main/kotlin/org/gotson/komga/interfaces/web/rest/SeriesController.kt index 2334f77bd..f3575c81d 100644 --- a/komga/src/main/kotlin/org/gotson/komga/interfaces/web/rest/SeriesController.kt +++ b/komga/src/main/kotlin/org/gotson/komga/interfaces/web/rest/SeriesController.kt @@ -53,19 +53,25 @@ class SeriesController( ) return mutableListOf>().let { specs -> + when { + !principal.user.sharedAllLibraries && !libraryIds.isNullOrEmpty() -> { + val authorizedLibraryIDs = libraryIds.intersect(principal.user.sharedLibraries.map { it.id }) + if (authorizedLibraryIDs.isEmpty()) return@let Page.empty(pageRequest) + else specs.add(Series::library.`in`(libraryRepository.findAllById(authorizedLibraryIDs))) + } + + !principal.user.sharedAllLibraries -> specs.add(Series::library.`in`(principal.user.sharedLibraries)) + + !libraryIds.isNullOrEmpty() -> { + val values = libraryRepository.findAllById(libraryIds) + specs.add(Series::library.`in`(values)) + } + } + if (!searchTerm.isNullOrEmpty()) { specs.add(Series::name.likeLower("%$searchTerm%")) } - if (!principal.user.sharedAllLibraries) { - specs.add(Series::library.`in`(principal.user.sharedLibraries)) - } - - if (!libraryIds.isNullOrEmpty()) { - val libraries = libraryRepository.findAllById(libraryIds) - specs.add(Series::library.`in`(libraries)) - } - if (specs.isNotEmpty()) { seriesRepository.findAll(specs.reduce { acc, spec -> acc.and(spec)!! }, pageRequest) } else {