diff --git a/src/main/kotlin/eom/improve/kafkaboot/repository/FilmRepository.kt b/src/main/kotlin/eom/improve/kafkaboot/repository/FilmRepository.kt index 4a50394..9f68a91 100644 --- a/src/main/kotlin/eom/improve/kafkaboot/repository/FilmRepository.kt +++ b/src/main/kotlin/eom/improve/kafkaboot/repository/FilmRepository.kt @@ -11,5 +11,5 @@ import reactor.core.publisher.Mono interface FilmRepository : R2dbcRepository { fun findAllBy() : Flux fun deleteByFilmId(filmId: Int) : Mono - fun findAllBy(pageable: Pageable) : Flux + fun findAllByOrderByFilmId(pageable: Pageable) : Flux } diff --git a/src/main/kotlin/eom/improve/kafkaboot/service/FilmControllerImpl.kt b/src/main/kotlin/eom/improve/kafkaboot/service/FilmControllerImpl.kt index 03c00fb..f37efe5 100644 --- a/src/main/kotlin/eom/improve/kafkaboot/service/FilmControllerImpl.kt +++ b/src/main/kotlin/eom/improve/kafkaboot/service/FilmControllerImpl.kt @@ -19,16 +19,7 @@ class FilmControllerImpl( } override fun getFilms(page: Long, limit: Long): Mono> { - 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 { diff --git a/src/main/kotlin/eom/improve/kafkaboot/service/FilmService.kt b/src/main/kotlin/eom/improve/kafkaboot/service/FilmService.kt index 21c97fe..25b153d 100644 --- a/src/main/kotlin/eom/improve/kafkaboot/service/FilmService.kt +++ b/src/main/kotlin/eom/improve/kafkaboot/service/FilmService.kt @@ -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 @@ -21,7 +23,11 @@ class FilmService( private val filmCategoryRepository: FilmCategoryRepository ) { - fun findAllByPageable(pageable: Pageable) : Flux = filmRepository.findAllBy(pageable) + fun findAllByPageable(pageable: Pageable) : Mono> = filmRepository.findAllByOrderByFilmId(pageable) + .collectSortedList { o1, o2 -> o1.filmId - o2.filmId } + .zipWith(filmRepository.count().toMono()) + .map { pagination -> PaginatedResponse( pagination.t1.map { it.convert2Pojo() } , pageable.pageNumber.toLong() ,pagination.t2 / pageable.pageSize ) } + fun findAll() : Flux = filmRepository.findAllBy()