fix(api): ignore search parameter when blank

This commit is contained in:
Gauthier Roebroeck 2021-07-30 10:08:16 +08:00
parent 9d9a73f8b2
commit 48637e26e4
7 changed files with 26 additions and 22 deletions

View file

@ -13,10 +13,10 @@ interface ReferentialRepository {
fun findAllAuthorsNamesByName(search: String, filterOnLibraryIds: Collection<String>?): List<String>
fun findAllAuthorsRoles(filterOnLibraryIds: Collection<String>?): List<String>
fun findAllAuthorsByName(search: String, role: String?, filterOnLibraryIds: Collection<String>?, pageable: Pageable): Page<Author>
fun findAllAuthorsByNameAndLibrary(search: String, role: String?, libraryId: String, filterOnLibraryIds: Collection<String>?, pageable: Pageable): Page<Author>
fun findAllAuthorsByNameAndCollection(search: String, role: String?, collectionId: String, filterOnLibraryIds: Collection<String>?, pageable: Pageable): Page<Author>
fun findAllAuthorsByNameAndSeries(search: String, role: String?, seriesId: String, filterOnLibraryIds: Collection<String>?, pageable: Pageable): Page<Author>
fun findAllAuthorsByName(search: String?, role: String?, filterOnLibraryIds: Collection<String>?, pageable: Pageable): Page<Author>
fun findAllAuthorsByNameAndLibrary(search: String?, role: String?, libraryId: String, filterOnLibraryIds: Collection<String>?, pageable: Pageable): Page<Author>
fun findAllAuthorsByNameAndCollection(search: String?, role: String?, collectionId: String, filterOnLibraryIds: Collection<String>?, pageable: Pageable): Page<Author>
fun findAllAuthorsByNameAndSeries(search: String?, role: String?, seriesId: String, filterOnLibraryIds: Collection<String>?, pageable: Pageable): Page<Author>
fun findAllGenres(filterOnLibraryIds: Collection<String>?): Set<String>
fun findAllGenresByLibrary(libraryId: String, filterOnLibraryIds: Collection<String>?): Set<String>

View file

@ -281,7 +281,7 @@ class BookDtoDao(
private fun BookSearchWithReadProgress.toCondition(): Condition {
var c: Condition = DSL.trueCondition()
searchTerm?.let { c = c.and(fts.match(it)) }
if (!searchTerm.isNullOrBlank()) c = c.and(fts.match(searchTerm))
if (!libraryIds.isNullOrEmpty()) c = c.and(b.LIBRARY_ID.`in`(libraryIds))
if (!seriesIds.isNullOrEmpty()) c = c.and(b.SERIES_ID.`in`(seriesIds))
if (!mediaStatus.isNullOrEmpty()) c = c.and(m.STATUS.`in`(mediaStatus))

View file

@ -51,8 +51,8 @@ class ReadListDao(
.firstOrNull()
override fun findAll(search: String?, pageable: Pageable): Page<ReadList> {
val conditions = search?.let { searchCondition(it) }
?: DSL.trueCondition()
val conditions = if (!search.isNullOrBlank()) searchCondition(search)
else DSL.trueCondition()
return try {
val count = dsl.selectCount()
@ -87,7 +87,7 @@ class ReadListDao(
override fun findAllByLibraryIds(belongsToLibraryIds: Collection<String>, filterOnLibraryIds: Collection<String>?, search: String?, pageable: Pageable): Page<ReadList> {
val conditions = b.LIBRARY_ID.`in`(belongsToLibraryIds)
.apply { search?.let { and(searchCondition(it)) } }
.apply { if (!search.isNullOrBlank()) and(searchCondition(search)) }
.apply { filterOnLibraryIds?.let { and(b.LIBRARY_ID.`in`(it)) } }
return try {

View file

@ -11,6 +11,7 @@ import org.jooq.DSLContext
import org.jooq.impl.DSL.field
import org.jooq.impl.DSL.lower
import org.jooq.impl.DSL.select
import org.jooq.impl.DSL.trueCondition
import org.springframework.data.domain.Page
import org.springframework.data.domain.PageImpl
import org.springframework.data.domain.PageRequest
@ -82,19 +83,19 @@ class ReferentialDao(
.fetchInto(bmaa)
.map { it.toDomain() }
override fun findAllAuthorsByName(search: String, role: String?, filterOnLibraryIds: Collection<String>?, pageable: Pageable): Page<Author> {
override fun findAllAuthorsByName(search: String?, role: String?, filterOnLibraryIds: Collection<String>?, pageable: Pageable): Page<Author> {
return findAuthorsByName(search, role, filterOnLibraryIds, pageable, null)
}
override fun findAllAuthorsByNameAndLibrary(search: String, role: String?, libraryId: String, filterOnLibraryIds: Collection<String>?, pageable: Pageable): Page<Author> {
override fun findAllAuthorsByNameAndLibrary(search: String?, role: String?, libraryId: String, filterOnLibraryIds: Collection<String>?, pageable: Pageable): Page<Author> {
return findAuthorsByName(search, role, filterOnLibraryIds, pageable, FilterBy(FilterByType.LIBRARY, libraryId))
}
override fun findAllAuthorsByNameAndCollection(search: String, role: String?, collectionId: String, filterOnLibraryIds: Collection<String>?, pageable: Pageable): Page<Author> {
override fun findAllAuthorsByNameAndCollection(search: String?, role: String?, collectionId: String, filterOnLibraryIds: Collection<String>?, pageable: Pageable): Page<Author> {
return findAuthorsByName(search, role, filterOnLibraryIds, pageable, FilterBy(FilterByType.COLLECTION, collectionId))
}
override fun findAllAuthorsByNameAndSeries(search: String, role: String?, seriesId: String, filterOnLibraryIds: Collection<String>?, pageable: Pageable): Page<Author> {
override fun findAllAuthorsByNameAndSeries(search: String?, role: String?, seriesId: String, filterOnLibraryIds: Collection<String>?, pageable: Pageable): Page<Author> {
return findAuthorsByName(search, role, filterOnLibraryIds, pageable, FilterBy(FilterByType.SERIES, seriesId))
}
@ -109,14 +110,15 @@ class ReferentialDao(
val id: String,
)
private fun findAuthorsByName(search: String, role: String?, filterOnLibraryIds: Collection<String>?, pageable: Pageable, filterBy: FilterBy?): Page<Author> {
private fun findAuthorsByName(search: String?, role: String?, filterOnLibraryIds: Collection<String>?, pageable: Pageable, filterBy: FilterBy?): Page<Author> {
return try {
val query = dsl.selectDistinct(bmaa.NAME, bmaa.ROLE)
.from(bmaa)
.join(ftsAuthors).on(ftsAuthors.rowid().eq(bmaa.rowid()))
.apply { if (!search.isNullOrBlank()) join(ftsAuthors).on(ftsAuthors.rowid().eq(bmaa.rowid())) }
.apply { if (filterOnLibraryIds != null || filterBy?.type == FilterByType.LIBRARY) leftJoin(s).on(bmaa.SERIES_ID.eq(s.ID)) }
.apply { if (filterBy?.type == FilterByType.COLLECTION) leftJoin(cs).on(bmaa.SERIES_ID.eq(cs.SERIES_ID)) }
.where(ftsAuthors.match(search))
.where(trueCondition())
.apply { if (!search.isNullOrBlank()) and(ftsAuthors.match(search)) }
.apply { role?.let { and(bmaa.ROLE.eq(role)) } }
.apply { filterOnLibraryIds?.let { and(s.LIBRARY_ID.`in`(it)) } }
.apply {
@ -130,9 +132,11 @@ class ReferentialDao(
}
val count = dsl.fetchCount(query)
val sort = if (!search.isNullOrBlank()) field("rank")
else lower(bmaa.NAME.udfStripAccents())
val items = query
.orderBy(field("rank"))
.orderBy(sort)
.apply { if (pageable.isPaged) limit(pageable.pageSize).offset(pageable.offset) }
.fetchInto(a)
.map { it.toDomain() }

View file

@ -50,8 +50,8 @@ class SeriesCollectionDao(
.firstOrNull()
override fun findAll(search: String?, pageable: Pageable): Page<SeriesCollection> {
val conditions = search?.let { searchCondition(search) }
?: DSL.trueCondition()
val conditions = if (!search.isNullOrBlank()) searchCondition(search)
else DSL.trueCondition()
return try {
val count = dsl.selectCount()
@ -86,7 +86,7 @@ class SeriesCollectionDao(
override fun findAllByLibraryIds(belongsToLibraryIds: Collection<String>, filterOnLibraryIds: Collection<String>?, search: String?, pageable: Pageable): Page<SeriesCollection> {
val conditions = s.LIBRARY_ID.`in`(belongsToLibraryIds)
.apply { search?.let { and(searchCondition(it)) } }
.apply { if (!search.isNullOrBlank()) and(searchCondition(search)) }
.apply { filterOnLibraryIds?.let { and(s.LIBRARY_ID.`in`(it)) } }
return try {

View file

@ -251,7 +251,7 @@ class SeriesDtoDao(
private fun SeriesSearchWithReadProgress.toCondition(): Condition {
var c: Condition = DSL.trueCondition()
searchTerm?.let { c = c.and(fts.match(it)) }
if (!searchTerm.isNullOrBlank()) c = c.and(fts.match(searchTerm))
if (!libraryIds.isNullOrEmpty()) c = c.and(s.LIBRARY_ID.`in`(libraryIds))
if (!collectionIds.isNullOrEmpty()) c = c.and(cs.COLLECTION_ID.`in`(collectionIds))
searchRegex?.let { c = c.and((it.second.toColumn()).likeRegex(it.first)) }

View file

@ -42,8 +42,8 @@ class ReferentialController(
@GetMapping("v2/authors")
fun getAuthors(
@AuthenticationPrincipal principal: KomgaPrincipal,
@RequestParam(name = "search", defaultValue = "") search: String,
@RequestParam(name = "role") role: String?,
@RequestParam(name = "search", required = false) search: String?,
@RequestParam(name = "role", required = false) role: String?,
@RequestParam(name = "library_id", required = false) libraryId: String?,
@RequestParam(name = "collection_id", required = false) collectionId: String?,
@RequestParam(name = "series_id", required = false) seriesId: String?,