From d7fd2964928530e221be800d4cafe4334dbbf368 Mon Sep 17 00:00:00 2001 From: Gauthier Roebroeck Date: Fri, 28 Aug 2020 11:34:32 +0800 Subject: [PATCH] feat(api): get referential data by collection --- .../persistence/ReferentialRepository.kt | 5 +++ .../infrastructure/jooq/ReferentialDao.kt | 43 ++++++++++++++++++ .../interfaces/rest/ReferentialController.kt | 45 ++++++++++++------- 3 files changed, 78 insertions(+), 15 deletions(-) diff --git a/komga/src/main/kotlin/org/gotson/komga/domain/persistence/ReferentialRepository.kt b/komga/src/main/kotlin/org/gotson/komga/domain/persistence/ReferentialRepository.kt index b39d1d70d..f37f52b7e 100644 --- a/komga/src/main/kotlin/org/gotson/komga/domain/persistence/ReferentialRepository.kt +++ b/komga/src/main/kotlin/org/gotson/komga/domain/persistence/ReferentialRepository.kt @@ -5,17 +5,22 @@ interface ReferentialRepository { fun findAllGenres(): Set fun findAllGenresByLibrary(libraryId: String): Set + fun findAllGenresByCollection(collectionId: String): Set fun findAllTags(): Set fun findAllTagsByLibrary(libraryId: String): Set fun findAllTagsBySeries(seriesId: String): Set + fun findAllTagsByCollection(collectionId: String): Set fun findAllLanguages(): Set fun findAllLanguagesByLibrary(libraryId: String): Set + fun findAllLanguagesByCollection(collectionId: String): Set fun findAllPublishers(): Set fun findAllPublishersByLibrary(libraryId: String): Set + fun findAllPublishersByCollection(collectionId: String): Set fun findAllAgeRatings(): Set fun findAllAgeRatingsByLibrary(libraryId: String): Set + fun findAllAgeRatingsByCollection(collectionId: String): Iterable } diff --git a/komga/src/main/kotlin/org/gotson/komga/infrastructure/jooq/ReferentialDao.kt b/komga/src/main/kotlin/org/gotson/komga/infrastructure/jooq/ReferentialDao.kt index 17777b046..55b44b8ad 100644 --- a/komga/src/main/kotlin/org/gotson/komga/infrastructure/jooq/ReferentialDao.kt +++ b/komga/src/main/kotlin/org/gotson/komga/infrastructure/jooq/ReferentialDao.kt @@ -19,6 +19,7 @@ class ReferentialDao( private val g = Tables.SERIES_METADATA_GENRE private val bt = Tables.BOOK_METADATA_TAG private val st = Tables.SERIES_METADATA_TAG + private val cs = Tables.COLLECTION_SERIES override fun findAuthorsByName(search: String): List = dsl.selectDistinct(a.NAME) @@ -41,6 +42,14 @@ class ReferentialDao( .orderBy(lower(g.GENRE)) .fetchSet(g.GENRE) + override fun findAllGenresByCollection(collectionId: String): Set = + dsl.selectDistinct(g.GENRE) + .from(g) + .leftJoin(cs).on(g.SERIES_ID.eq(cs.SERIES_ID)) + .where(cs.COLLECTION_ID.eq(collectionId)) + .orderBy(lower(g.GENRE)) + .fetchSet(g.GENRE) + override fun findAllTags(): Set = dsl.select(bt.TAG.`as`("tag")) .from(bt) @@ -67,6 +76,14 @@ class ReferentialDao( .orderBy(lower(bt.TAG)) .fetchSet(bt.TAG) + override fun findAllTagsByCollection(collectionId: String): Set = + dsl.select(st.TAG) + .from(st) + .leftJoin(cs).on(st.SERIES_ID.eq(cs.SERIES_ID)) + .where(cs.COLLECTION_ID.eq(collectionId)) + .orderBy(lower(st.TAG)) + .fetchSet(st.TAG) + override fun findAllLanguages(): Set = dsl.selectDistinct(sd.LANGUAGE) .from(sd) @@ -83,6 +100,15 @@ class ReferentialDao( .orderBy(sd.LANGUAGE) .fetchSet(sd.LANGUAGE) + override fun findAllLanguagesByCollection(collectionId: String): Set = + dsl.selectDistinct(sd.LANGUAGE) + .from(sd) + .leftJoin(cs).on(sd.SERIES_ID.eq(cs.SERIES_ID)) + .where(sd.LANGUAGE.ne("")) + .and(cs.COLLECTION_ID.eq(collectionId)) + .orderBy(sd.LANGUAGE) + .fetchSet(sd.LANGUAGE) + override fun findAllPublishers(): Set = dsl.selectDistinct(sd.PUBLISHER) .from(sd) @@ -99,6 +125,15 @@ class ReferentialDao( .orderBy(sd.PUBLISHER) .fetchSet(sd.PUBLISHER) + override fun findAllPublishersByCollection(collectionId: String): Set = + dsl.selectDistinct(sd.PUBLISHER) + .from(sd) + .leftJoin(cs).on(sd.SERIES_ID.eq(cs.SERIES_ID)) + .where(sd.PUBLISHER.ne("")) + .and(cs.COLLECTION_ID.eq(collectionId)) + .orderBy(sd.PUBLISHER) + .fetchSet(sd.PUBLISHER) + override fun findAllAgeRatings(): Set = dsl.selectDistinct(sd.AGE_RATING) .from(sd) @@ -112,4 +147,12 @@ class ReferentialDao( .where(s.LIBRARY_ID.eq(libraryId)) .orderBy(sd.AGE_RATING) .fetchSet(sd.AGE_RATING) + + override fun findAllAgeRatingsByCollection(collectionId: String): Iterable = + dsl.selectDistinct(sd.AGE_RATING) + .from(sd) + .leftJoin(cs).on(sd.SERIES_ID.eq(cs.SERIES_ID)) + .where(cs.COLLECTION_ID.eq(collectionId)) + .orderBy(sd.AGE_RATING) + .fetchSet(sd.AGE_RATING) } diff --git a/komga/src/main/kotlin/org/gotson/komga/interfaces/rest/ReferentialController.kt b/komga/src/main/kotlin/org/gotson/komga/interfaces/rest/ReferentialController.kt index f44ca0a48..08f684cff 100644 --- a/komga/src/main/kotlin/org/gotson/komga/interfaces/rest/ReferentialController.kt +++ b/komga/src/main/kotlin/org/gotson/komga/interfaces/rest/ReferentialController.kt @@ -22,43 +22,58 @@ class ReferentialController( @GetMapping("/genres") fun getGenres( - @RequestParam(name = "library_id", required = false) libraryId: String? + @RequestParam(name = "library_id", required = false) libraryId: String?, + @RequestParam(name = "collection_id", required = false) collectionId: String? ): Set = - if (libraryId != null) referentialRepository.findAllGenresByLibrary(libraryId) - else referentialRepository.findAllGenres() + when { + libraryId != null -> referentialRepository.findAllGenresByLibrary(libraryId) + collectionId != null -> referentialRepository.findAllGenresByCollection(collectionId) + else -> referentialRepository.findAllGenres() + } @GetMapping("/tags") fun getTags( @RequestParam(name = "library_id", required = false) libraryId: String?, - @RequestParam(name = "series_id", required = false) seriesId: String? + @RequestParam(name = "series_id", required = false) seriesId: String?, + @RequestParam(name = "collection_id", required = false) collectionId: String? ): Set = when { libraryId != null -> referentialRepository.findAllTagsByLibrary(libraryId) seriesId != null -> referentialRepository.findAllTagsBySeries(seriesId) + collectionId != null -> referentialRepository.findAllTagsByCollection(collectionId) else -> referentialRepository.findAllTags() } @GetMapping("/languages") fun getLanguages( - @RequestParam(name = "library_id", required = false) libraryId: String? + @RequestParam(name = "library_id", required = false) libraryId: String?, + @RequestParam(name = "collection_id", required = false) collectionId: String? ): Set = - if (libraryId != null) referentialRepository.findAllLanguagesByLibrary(libraryId) - else referentialRepository.findAllLanguages() + when { + libraryId != null -> referentialRepository.findAllLanguagesByLibrary(libraryId) + collectionId != null -> referentialRepository.findAllLanguagesByCollection(collectionId) + else -> referentialRepository.findAllLanguages() + } @GetMapping("/publishers") fun getPublishers( - @RequestParam(name = "library_id", required = false) libraryId: String? + @RequestParam(name = "library_id", required = false) libraryId: String?, + @RequestParam(name = "collection_id", required = false) collectionId: String? ): Set = - if (libraryId != null) referentialRepository.findAllPublishersByLibrary(libraryId) - else referentialRepository.findAllPublishers() + when { + libraryId != null -> referentialRepository.findAllPublishersByLibrary(libraryId) + collectionId != null -> referentialRepository.findAllPublishersByCollection(collectionId) + else -> referentialRepository.findAllPublishers() + } @GetMapping("/age-ratings") fun getAgeRatings( - @RequestParam(name = "library_id", required = false) libraryId: String? + @RequestParam(name = "library_id", required = false) libraryId: String?, + @RequestParam(name = "collection_id", required = false) collectionId: String? ): Set = - if (libraryId != null) { - referentialRepository.findAllAgeRatingsByLibrary(libraryId) - } else { - referentialRepository.findAllAgeRatings() + when { + libraryId != null -> referentialRepository.findAllAgeRatingsByLibrary(libraryId) + collectionId != null -> referentialRepository.findAllAgeRatingsByCollection(collectionId) + else -> referentialRepository.findAllAgeRatings() }.map { it?.toString() ?: "None" }.toSet() }