Skip to content

Commit

Permalink
FEATURE : add pagination for load film informations #4
Browse files Browse the repository at this point in the history
  • Loading branch information
stephano-tri committed Aug 12, 2024
1 parent e1bc715 commit f2cc7ce
Show file tree
Hide file tree
Showing 7 changed files with 8,896 additions and 23 deletions.
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ services:
environment:
- POSTGRESQL_REPLICATION_MODE=slave
- POSTGRESQL_REPLICATION_USER=repl_user
- POSTGRESQL_REPLICATION_PASSWORD=repl_password
- POSTGRESQL_REPLICATION_PASSWORD=repl_cpassword
- POSTGRESQL_MASTER_HOST=postgresql-main
- POSTGRESQL_MASTER_PORT_NUMBER=5432
- POSTGRESQL_USERNAME=my_user
Expand Down
995 changes: 995 additions & 0 deletions hs_err_pid20680.log

Large diffs are not rendered by default.

7,858 changes: 7,858 additions & 0 deletions replay_pid20680.log

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ interface FilmController {
fun getAllFilms() : Mono<List<Film>>

@GetMapping("/list/{page}/{limit}")
fun getFilms(@PathVariable page: Long, @PathVariable limit: Long) : Mono<PaginatedResponse<List<Film>>>
fun getFilms(@PathVariable page: Long, @PathVariable limit: Long) : Mono<PaginatedResponse<Film>>

@PutMapping("/modify")
fun modifyFilm(@RequestBody updatedFilm : Film) : Mono<Film>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package eom.improve.kafkaboot.service
import eom.improve.kafkaboot.common.PaginatedResponse
import eom.improve.kafkaboot.controller.FilmController
import eom.improve.kafkaboot.dto.Film
import org.springframework.data.domain.PageRequest
import org.springframework.stereotype.Service
import reactor.core.publisher.Mono
import reactor.kotlin.core.publisher.toMono
Expand All @@ -17,7 +18,17 @@ class FilmControllerImpl(
.collectSortedList((Comparator<Film> { o1, o2 -> o1.filmId.compareTo(o2.filmId) }))
}

override fun getFilms(page: Long, limit: Long): Mono<PaginatedResponse<List<Film>>> {
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()
}
}

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

import eom.improve.kafkaboot.model.FilmEntity
import eom.improve.kafkaboot.model.InventoryEntity
import eom.improve.kafkaboot.model.RentalEntity
import eom.improve.kafkaboot.repository.*
import org.springframework.data.domain.Pageable
import org.springframework.stereotype.Service
import reactor.core.publisher.Flux
import reactor.core.publisher.Mono
Expand All @@ -17,6 +20,9 @@ class FilmService(
private val filmActorRepository: FilmActorRepository,
private val filmCategoryRepository: FilmCategoryRepository
) {

fun findAllByPageable(pageable: Pageable) : Flux<FilmEntity> = filmRepository.findAllBy(pageable)

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

fun updateFilm(updatedFilm : FilmEntity) : Mono<FilmEntity> {
Expand All @@ -34,31 +40,31 @@ class FilmService(
return filmRepository.findById(filmId)
.switchIfEmpty(RuntimeException("Not registered film").toMono())
.flatMap { filmEn ->
inventoryRepository.findAllByFilmId(filmEn.filmId)
.flatMap { inventoryEn ->
rentalRepository.findAllByInventoryId(inventoryEn.inventoryId)
.flatMap { rentalEn ->
paymentRepository.findAllByRentalId(rentalEn.rentalId)
.flatMap { paymentEn ->
paymentRepository.deleteByPaymentId(paymentEn.paymentId)
}
.then(rentalEn.toMono())
}
.flatMap { rentalEn ->
inventoryRepository.findAllByFilmId(filmEn.filmId)
.flatMap { inventoryEn ->
rentalRepository.findAllByInventoryId(inventoryEn.inventoryId)
.flatMap { rentalEn ->
paymentRepository.findAllByRentalId(rentalEn.rentalId)
.flatMap { paymentEn ->
paymentRepository.deleteByPaymentId(paymentEn.paymentId)
}
.then(rentalEn.toMono())
}
.flatMap { rentalEn ->
rentalRepository.deleteByRentalId(rentalEn.rentalId)
}.then(inventoryEn.toMono())
}
.flatMap { inventoryEn ->
inventoryRepository.deleteByInventoryId(inventoryEn.inventoryId)
}
.then(filmEn.toMono())
}.then(inventoryEn.toMono())
}
.flatMap { inventoryEn ->
inventoryRepository.deleteByInventoryId(inventoryEn.inventoryId)
}
.then(filmEn.toMono())
}
.flatMap { filmEn ->
Mono.zip(
filmActorRepository.findAllByFilmId(filmEn.filmId)
.flatMap { filmActorRepository.deleteByFilmId(it.filmId) }
.then()
,
,
filmCategoryRepository.findAllByFilmId(filmEn.filmId)
.flatMap { filmCategoryRepository.deleteByFilmId(it.filmId) }
.then()
Expand Down
7 changes: 5 additions & 2 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
spring:
r2dbc:
url: r2dbc:postgresql://localhost:5432/dvdrental
username: postgres
password: example
username: my_user
password: my_password

#kafka
kafka:
Expand All @@ -20,3 +20,6 @@ spring:
spring.json.use.type.headers: false
properties:
spring.json.trusted.packages: '*'

server:
port: 7477

0 comments on commit f2cc7ce

Please sign in to comment.