feat(api): filter referential data by library or series

required for #290
This commit is contained in:
Gauthier Roebroeck 2020-08-27 17:32:05 +08:00
parent 5fe015ede0
commit 982983e3db
3 changed files with 81 additions and 12 deletions

View file

@ -2,8 +2,17 @@ package org.gotson.komga.domain.persistence
interface ReferentialRepository {
fun findAuthorsByName(search: String): List<String>
fun findAllGenres(): Set<String>
fun findAllGenresByLibrary(libraryId: String): Set<String>
fun findAllTags(): Set<String>
fun findAllTagsByLibrary(libraryId: String): Set<String>
fun findAllTagsBySeries(seriesId: String): Set<String>
fun findAllLanguages(): Set<String>
fun findAllLanguagesByLibrary(libraryId: String): Set<String>
fun findAllPublishers(): Set<String>
fun findAllPublishersByLibrary(libraryId: String): Set<String>
}

View file

@ -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<String> =
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<String> =
dsl.selectDistinct(g.GENRE)
@ -31,6 +33,14 @@ class ReferentialDao(
.orderBy(lower(g.GENRE))
.fetchSet(g.GENRE)
override fun findAllGenresByLibrary(libraryId: String): Set<String> =
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<String> =
dsl.select(bt.TAG.`as`("tag"))
.from(bt)
@ -41,6 +51,22 @@ class ReferentialDao(
.sortedBy { it.toLowerCase() }
.toSet()
override fun findAllTagsByLibrary(libraryId: String): Set<String> =
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<String> =
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<String> =
dsl.selectDistinct(sd.LANGUAGE)
.from(sd)
@ -48,10 +74,28 @@ class ReferentialDao(
.orderBy(sd.LANGUAGE)
.fetchSet(sd.LANGUAGE)
override fun findAllLanguagesByLibrary(libraryId: String): Set<String> =
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<String> =
dsl.selectDistinct(sd.PUBLISHER)
.from(sd)
.where(sd.PUBLISHER.ne(""))
.orderBy(sd.PUBLISHER)
.fetchSet(sd.PUBLISHER)
override fun findAllPublishersByLibrary(libraryId: String): Set<String> =
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)
}

View file

@ -21,18 +21,34 @@ class ReferentialController(
referentialRepository.findAuthorsByName(search)
@GetMapping("/genres")
fun getGenres(): Set<String> =
referentialRepository.findAllGenres()
fun getGenres(
@RequestParam(name = "library_id", required = false) libraryId: String?
): Set<String> =
if (libraryId != null) referentialRepository.findAllGenresByLibrary(libraryId)
else referentialRepository.findAllGenres()
@GetMapping("/tags")
fun getTags(): Set<String> =
referentialRepository.findAllTags()
fun getTags(
@RequestParam(name = "library_id", required = false) libraryId: String?,
@RequestParam(name = "series_id", required = false) seriesId: String?
): Set<String> =
when {
libraryId != null -> referentialRepository.findAllTagsByLibrary(libraryId)
seriesId != null -> referentialRepository.findAllTagsBySeries(seriesId)
else -> referentialRepository.findAllTags()
}
@GetMapping("/languages")
fun getLanguages(): Set<String> =
referentialRepository.findAllLanguages()
fun getLanguages(
@RequestParam(name = "library_id", required = false) libraryId: String?
): Set<String> =
if (libraryId != null) referentialRepository.findAllLanguagesByLibrary(libraryId)
else referentialRepository.findAllLanguages()
@GetMapping("/publishers")
fun getPublishers(): Set<String> =
referentialRepository.findAllPublishers()
fun getPublishers(
@RequestParam(name = "library_id", required = false) libraryId: String?
): Set<String> =
if (libraryId != null) referentialRepository.findAllPublishersByLibrary(libraryId)
else referentialRepository.findAllPublishers()
}