Skip to content

Commit

Permalink
fix: find all animes by lang types
Browse files Browse the repository at this point in the history
  • Loading branch information
Ziedelth committed Sep 7, 2024
1 parent 968dd4c commit 1b96e5d
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package fr.shikkanime.caches
import fr.shikkanime.dtos.enums.Status
import fr.shikkanime.entities.SortParameter
import fr.shikkanime.entities.enums.CountryCode
import fr.shikkanime.entities.enums.LangType
import java.util.*

data class CountryCodeUUIDSortPaginationKeyCache(
Expand All @@ -11,5 +12,6 @@ data class CountryCodeUUIDSortPaginationKeyCache(
val sort: List<SortParameter>,
override val page: Int,
override val limit: Int,
val searchTypes: List<LangType>?,
val status: Status? = null,
) : CountryCodePaginationKeyCache(countryCode, page, limit)
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,15 @@ class AnimeController : HasPageableRoute() {
if (!name.isNullOrBlank()) {
animeCacheService.findAllByName(countryParam, name, page, limit, searchTypes?.toList())
} else {
animeCacheService.findAllBy(countryParam, simulcastParam, sortParameters, page, limit, statusParam)
animeCacheService.findAllBy(
countryParam,
simulcastParam,
sortParameters,
page,
limit,
searchTypes?.toList(),
statusParam
)
}
)
}
Expand Down
10 changes: 8 additions & 2 deletions src/main/kotlin/fr/shikkanime/repositories/AnimeRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,24 @@ class AnimeRepository : AbstractRepository<Anime>() {
sort: List<SortParameter>,
page: Int,
limit: Int,
searchTypes: List<LangType>?,
status: Status? = null
): Pageable<Anime> {
return database.entityManager.use { entityManager ->
val cb = entityManager.criteriaBuilder
val query = cb.createQuery(getEntityClass())
val root = query.from(getEntityClass())
query.distinct(true).select(root)

val predicates = mutableListOf<Predicate>()
simulcast?.let { predicates.add(cb.equal(root.join(Anime_.simulcasts), it)) }
countryCode?.let { predicates.add(cb.equal(root[Anime_.countryCode], it)) }
status?.let { predicates.add(cb.equal(root[Anime_.status], it)) }
query.where(*predicates.toTypedArray())
val orPredicate = predicates(countryCode, predicates, cb, root, searchTypes)

query.where(
*predicates.toTypedArray(),
if (orPredicate.isNotEmpty()) cb.or(*orPredicate.toTypedArray()) else cb.conjunction()
)

val orders = sort.mapNotNull { sortParameter ->
val order = if (sortParameter.order == SortParameter.Order.ASC) cb::asc else cb::desc
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/fr/shikkanime/services/AnimeService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ class AnimeService : AbstractService<Anime, AnimeRepository>() {
sort: List<SortParameter>,
page: Int,
limit: Int,
searchTypes: List<LangType>?,
status: Status? = null,
) = animeRepository.findAllBy(countryCode, simulcast, sort, page, limit, status)
) = animeRepository.findAllBy(countryCode, simulcast, sort, page, limit, searchTypes, status)

fun findAllByName(
countryCode: CountryCode?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class AnimeCacheService : AbstractCacheService {
it.sort,
it.page,
it.limit,
it.searchTypes,
it.status
),
AnimeDto::class.java
Expand Down Expand Up @@ -109,8 +110,9 @@ class AnimeCacheService : AbstractCacheService {
sort: List<SortParameter>,
page: Int,
limit: Int,
searchTypes: List<LangType>? = null,
status: Status? = null,
) = findAllByCache[CountryCodeUUIDSortPaginationKeyCache(countryCode, uuid, sort, page, limit, status)]
) = findAllByCache[CountryCodeUUIDSortPaginationKeyCache(countryCode, uuid, sort, page, limit, searchTypes, status)]

fun findAllByName(countryCode: CountryCode?, name: String, page: Int, limit: Int, searchTypes: List<LangType>?) =
findAllByNameCache[CountryCodeNamePaginationKeyCache(countryCode, name, page, limit, searchTypes)]
Expand Down

0 comments on commit 1b96e5d

Please sign in to comment.