From 562c57ccc8f3c39d494dd8b929e65db975370a1b Mon Sep 17 00:00:00 2001 From: Gauthier Roebroeck Date: Fri, 25 Feb 2022 16:37:15 +0800 Subject: [PATCH] feat(api): retrieve all sharing labels --- .../persistence/ReferentialRepository.kt | 4 +++ .../infrastructure/jooq/ReferentialDao.kt | 32 +++++++++++++++++++ .../api/rest/ReferentialController.kt | 12 +++++++ 3 files changed, 48 insertions(+) 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 aae0e6774..c47694cc5 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 @@ -49,4 +49,8 @@ interface ReferentialRepository { fun findAllSeriesReleaseDates(filterOnLibraryIds: Collection?): Set fun findAllSeriesReleaseDatesByLibrary(libraryId: String, filterOnLibraryIds: Collection?): Set fun findAllSeriesReleaseDatesByCollection(collectionId: String, filterOnLibraryIds: Collection?): Set + + fun findAllSharingLabels(filterOnLibraryIds: Collection?): Set + fun findAllSharingLabelsByLibrary(libraryId: String, filterOnLibraryIds: Collection?): Set + fun findAllSharingLabelsByCollection(collectionId: String, filterOnLibraryIds: Collection?): 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 a6ae00897..318e7d14e 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 @@ -35,6 +35,7 @@ class ReferentialDao( private val st = Tables.SERIES_METADATA_TAG private val cs = Tables.COLLECTION_SERIES private val rb = Tables.READLIST_BOOK + private val sl = Tables.SERIES_METADATA_SHARING override fun findAllAuthorsByName(search: String, filterOnLibraryIds: Collection?): List = dsl.selectDistinct(a.NAME, a.ROLE) @@ -463,6 +464,37 @@ class ReferentialDao( .orderBy(bma.RELEASE_DATE.desc()) .fetchSet(bma.RELEASE_DATE) + override fun findAllSharingLabels(filterOnLibraryIds: Collection?): Set = + dsl.selectDistinct(sl.LABEL) + .from(sl) + .apply { + filterOnLibraryIds?.let { + leftJoin(s).on(sl.SERIES_ID.eq(s.ID)) + .where(s.LIBRARY_ID.`in`(it)) + } + } + .orderBy(sl.LABEL.collate(SqliteUdfDataSource.collationUnicode3)) + .fetchSet(sl.LABEL) + + override fun findAllSharingLabelsByLibrary(libraryId: String, filterOnLibraryIds: Collection?): Set = + dsl.selectDistinct(sl.LABEL) + .from(sl) + .leftJoin(s).on(sl.SERIES_ID.eq(s.ID)) + .where(s.LIBRARY_ID.eq(libraryId)) + .apply { filterOnLibraryIds?.let { and(s.LIBRARY_ID.`in`(it)) } } + .orderBy(sl.LABEL.collate(SqliteUdfDataSource.collationUnicode3)) + .fetchSet(sl.LABEL) + + override fun findAllSharingLabelsByCollection(collectionId: String, filterOnLibraryIds: Collection?): Set = + dsl.selectDistinct(sl.LABEL) + .from(sl) + .leftJoin(cs).on(sl.SERIES_ID.eq(cs.SERIES_ID)) + .apply { filterOnLibraryIds?.let { leftJoin(s).on(sl.SERIES_ID.eq(s.ID)) } } + .where(cs.COLLECTION_ID.eq(collectionId)) + .apply { filterOnLibraryIds?.let { and(s.LIBRARY_ID.`in`(it)) } } + .orderBy(sl.LABEL.collate(SqliteUdfDataSource.collationUnicode3)) + .fetchSet(sl.LABEL) + private fun BookMetadataAuthorRecord.toDomain(): Author = Author( name = name, diff --git a/komga/src/main/kotlin/org/gotson/komga/interfaces/api/rest/ReferentialController.kt b/komga/src/main/kotlin/org/gotson/komga/interfaces/api/rest/ReferentialController.kt index e8324464a..f02dde79d 100644 --- a/komga/src/main/kotlin/org/gotson/komga/interfaces/api/rest/ReferentialController.kt +++ b/komga/src/main/kotlin/org/gotson/komga/interfaces/api/rest/ReferentialController.kt @@ -92,6 +92,18 @@ class ReferentialController( else -> referentialRepository.findAllGenres(principal.user.getAuthorizedLibraryIds(null)) } + @GetMapping("v1/sharing-labels") + fun getSharingLabels( + @AuthenticationPrincipal principal: KomgaPrincipal, + @RequestParam(name = "library_id", required = false) libraryId: String?, + @RequestParam(name = "collection_id", required = false) collectionId: String?, + ): Set = + when { + libraryId != null -> referentialRepository.findAllSharingLabelsByLibrary(libraryId, principal.user.getAuthorizedLibraryIds(null)) + collectionId != null -> referentialRepository.findAllSharingLabelsByCollection(collectionId, principal.user.getAuthorizedLibraryIds(null)) + else -> referentialRepository.findAllSharingLabels(principal.user.getAuthorizedLibraryIds(null)) + } + @GetMapping("v1/tags") fun getTags( @AuthenticationPrincipal principal: KomgaPrincipal,