Skip to content

Commit

Permalink
FIX: pagination sorting func #4
Browse files Browse the repository at this point in the history
  • Loading branch information
stephano-tri committed Aug 12, 2024
1 parent f2cc7ce commit 26a85de
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ import reactor.core.publisher.Mono
interface FilmRepository : R2dbcRepository<FilmEntity, Int> {
fun findAllBy() : Flux<FilmEntity>
fun deleteByFilmId(filmId: Int) : Mono<Void>
fun findAllBy(pageable: Pageable) : Flux<FilmEntity>
fun findAllByOrderByFilmId(pageable: Pageable) : Flux<FilmEntity>
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,7 @@ class FilmControllerImpl(
}

override fun getFilms(page: Long, limit: Long): Mono<PaginatedResponse<Film>> {
return filmService.findAllByPageable(PageRequest.of(page.toInt(), limit.toInt()))
.map { it.convert2Pojo() }
.collectList()
.flatMap { films ->
PaginatedResponse(
response = films,
currentPage = page,
totalPages = (films.size / limit).toLong()
).toMono()
}
return filmService.findAllByPageable(PageRequest.of(page.toInt() , limit.toInt()))
}

override fun modifyFilm(updatedFilm : Film) : Mono<Film> {
Expand Down
8 changes: 7 additions & 1 deletion src/main/kotlin/eom/improve/kafkaboot/service/FilmService.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package eom.improve.kafkaboot.service

import eom.improve.kafkaboot.common.PaginatedResponse
import eom.improve.kafkaboot.dto.Film
import eom.improve.kafkaboot.model.FilmEntity
import eom.improve.kafkaboot.model.InventoryEntity
import eom.improve.kafkaboot.model.RentalEntity
Expand All @@ -21,7 +23,11 @@ class FilmService(
private val filmCategoryRepository: FilmCategoryRepository
) {

fun findAllByPageable(pageable: Pageable) : Flux<FilmEntity> = filmRepository.findAllBy(pageable)
fun findAllByPageable(pageable: Pageable) : Mono<PaginatedResponse<Film>> = filmRepository.findAllByOrderByFilmId(pageable)
.collectSortedList { o1, o2 -> o1.filmId - o2.filmId }
.zipWith(filmRepository.count().toMono())
.map { pagination -> PaginatedResponse<Film>( pagination.t1.map { it.convert2Pojo() } , pageable.pageNumber.toLong() ,pagination.t2 / pageable.pageSize ) }


fun findAll() : Flux<FilmEntity> = filmRepository.findAllBy()

Expand Down

0 comments on commit 26a85de

Please sign in to comment.