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 af6be1986..1b2347a69 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 @@ -2,8 +2,17 @@ package org.gotson.komga.domain.persistence interface ReferentialRepository { fun findAuthorsByName(search: String): List + fun findAllGenres(): Set + fun findAllGenresByLibrary(libraryId: String): Set + fun findAllTags(): Set + fun findAllTagsByLibrary(libraryId: String): Set + fun findAllTagsBySeries(seriesId: String): Set + fun findAllLanguages(): Set + fun findAllLanguagesByLibrary(libraryId: String): Set + fun findAllPublishers(): Set + fun findAllPublishersByLibrary(libraryId: String): Set } 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 fc616e0f4..de6e5bd15 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 @@ -14,16 +14,18 @@ class ReferentialDao( private val a = Tables.BOOK_METADATA_AUTHOR private val sd = Tables.SERIES_METADATA + private val s = Tables.SERIES + private val b = Tables.BOOK private val g = Tables.SERIES_METADATA_GENRE private val bt = Tables.BOOK_METADATA_TAG private val st = Tables.SERIES_METADATA_TAG override fun findAuthorsByName(search: String): List = dsl.selectDistinct(a.NAME) - .from(a) - .where(a.NAME.containsIgnoreCase(search)) - .orderBy(a.NAME) - .fetch(a.NAME) + .from(a) + .where(a.NAME.containsIgnoreCase(search)) + .orderBy(a.NAME) + .fetch(a.NAME) override fun findAllGenres(): Set = dsl.selectDistinct(g.GENRE) @@ -31,6 +33,14 @@ class ReferentialDao( .orderBy(lower(g.GENRE)) .fetchSet(g.GENRE) + override fun findAllGenresByLibrary(libraryId: String): Set = + dsl.selectDistinct(g.GENRE) + .from(g) + .leftJoin(s).on(g.SERIES_ID.eq(s.ID)) + .where(s.LIBRARY_ID.eq(libraryId)) + .orderBy(lower(g.GENRE)) + .fetchSet(g.GENRE) + override fun findAllTags(): Set = dsl.select(bt.TAG.`as`("tag")) .from(bt) @@ -41,6 +51,22 @@ class ReferentialDao( .sortedBy { it.toLowerCase() } .toSet() + override fun findAllTagsByLibrary(libraryId: String): Set = + dsl.select(st.TAG) + .from(st) + .leftJoin(s).on(st.SERIES_ID.eq(s.ID)) + .where(s.LIBRARY_ID.eq(libraryId)) + .orderBy(lower(st.TAG)) + .fetchSet(st.TAG) + + override fun findAllTagsBySeries(seriesId: String): Set = + dsl.select(bt.TAG) + .from(bt) + .leftJoin(b).on(bt.BOOK_ID.eq(b.ID)) + .where(b.SERIES_ID.eq(seriesId)) + .orderBy(lower(bt.TAG)) + .fetchSet(bt.TAG) + override fun findAllLanguages(): Set = dsl.selectDistinct(sd.LANGUAGE) .from(sd) @@ -48,10 +74,28 @@ class ReferentialDao( .orderBy(sd.LANGUAGE) .fetchSet(sd.LANGUAGE) + override fun findAllLanguagesByLibrary(libraryId: String): Set = + dsl.selectDistinct(sd.LANGUAGE) + .from(sd) + .leftJoin(s).on(sd.SERIES_ID.eq(s.ID)) + .where(sd.LANGUAGE.ne("")) + .and(s.LIBRARY_ID.eq(libraryId)) + .orderBy(sd.LANGUAGE) + .fetchSet(sd.LANGUAGE) + override fun findAllPublishers(): Set = dsl.selectDistinct(sd.PUBLISHER) .from(sd) .where(sd.PUBLISHER.ne("")) .orderBy(sd.PUBLISHER) .fetchSet(sd.PUBLISHER) + + override fun findAllPublishersByLibrary(libraryId: String): Set = + dsl.selectDistinct(sd.PUBLISHER) + .from(sd) + .leftJoin(s).on(sd.SERIES_ID.eq(s.ID)) + .where(sd.PUBLISHER.ne("")) + .and(s.LIBRARY_ID.eq(libraryId)) + .orderBy(sd.PUBLISHER) + .fetchSet(sd.PUBLISHER) } 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 3cb857222..5cce96ccd 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 @@ -21,18 +21,34 @@ class ReferentialController( referentialRepository.findAuthorsByName(search) @GetMapping("/genres") - fun getGenres(): Set = - referentialRepository.findAllGenres() + fun getGenres( + @RequestParam(name = "library_id", required = false) libraryId: String? + ): Set = + if (libraryId != null) referentialRepository.findAllGenresByLibrary(libraryId) + else referentialRepository.findAllGenres() @GetMapping("/tags") - fun getTags(): Set = - referentialRepository.findAllTags() + fun getTags( + @RequestParam(name = "library_id", required = false) libraryId: String?, + @RequestParam(name = "series_id", required = false) seriesId: String? + ): Set = + when { + libraryId != null -> referentialRepository.findAllTagsByLibrary(libraryId) + seriesId != null -> referentialRepository.findAllTagsBySeries(seriesId) + else -> referentialRepository.findAllTags() + } @GetMapping("/languages") - fun getLanguages(): Set = - referentialRepository.findAllLanguages() + fun getLanguages( + @RequestParam(name = "library_id", required = false) libraryId: String? + ): Set = + if (libraryId != null) referentialRepository.findAllLanguagesByLibrary(libraryId) + else referentialRepository.findAllLanguages() @GetMapping("/publishers") - fun getPublishers(): Set = - referentialRepository.findAllPublishers() + fun getPublishers( + @RequestParam(name = "library_id", required = false) libraryId: String? + ): Set = + if (libraryId != null) referentialRepository.findAllPublishersByLibrary(libraryId) + else referentialRepository.findAllPublishers() }