Skip to content

Commit f2cc7ce

Browse files
committed
FEATURE : add pagination for load film informations #4
1 parent e1bc715 commit f2cc7ce

File tree

7 files changed

+8896
-23
lines changed

7 files changed

+8896
-23
lines changed

docker-compose.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ services:
4545
environment:
4646
- POSTGRESQL_REPLICATION_MODE=slave
4747
- POSTGRESQL_REPLICATION_USER=repl_user
48-
- POSTGRESQL_REPLICATION_PASSWORD=repl_password
48+
- POSTGRESQL_REPLICATION_PASSWORD=repl_cpassword
4949
- POSTGRESQL_MASTER_HOST=postgresql-main
5050
- POSTGRESQL_MASTER_PORT_NUMBER=5432
5151
- POSTGRESQL_USERNAME=my_user

hs_err_pid20680.log

+995
Large diffs are not rendered by default.

replay_pid20680.log

+7,858
Large diffs are not rendered by default.

src/main/kotlin/eom/improve/kafkaboot/controller/FilmController.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ interface FilmController {
1414
fun getAllFilms() : Mono<List<Film>>
1515

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

1919
@PutMapping("/modify")
2020
fun modifyFilm(@RequestBody updatedFilm : Film) : Mono<Film>

src/main/kotlin/eom/improve/kafkaboot/service/FilmControllerImpl.kt

+12-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package eom.improve.kafkaboot.service
33
import eom.improve.kafkaboot.common.PaginatedResponse
44
import eom.improve.kafkaboot.controller.FilmController
55
import eom.improve.kafkaboot.dto.Film
6+
import org.springframework.data.domain.PageRequest
67
import org.springframework.stereotype.Service
78
import reactor.core.publisher.Mono
89
import reactor.kotlin.core.publisher.toMono
@@ -17,7 +18,17 @@ class FilmControllerImpl(
1718
.collectSortedList((Comparator<Film> { o1, o2 -> o1.filmId.compareTo(o2.filmId) }))
1819
}
1920

20-
override fun getFilms(page: Long, limit: Long): Mono<PaginatedResponse<List<Film>>> {
21+
override fun getFilms(page: Long, limit: Long): Mono<PaginatedResponse<Film>> {
22+
return filmService.findAllByPageable(PageRequest.of(page.toInt(), limit.toInt()))
23+
.map { it.convert2Pojo() }
24+
.collectList()
25+
.flatMap { films ->
26+
PaginatedResponse(
27+
response = films,
28+
currentPage = page,
29+
totalPages = (films.size / limit).toLong()
30+
).toMono()
31+
}
2132
}
2233

2334
override fun modifyFilm(updatedFilm : Film) : Mono<Film> {

src/main/kotlin/eom/improve/kafkaboot/service/FilmService.kt

+24-18
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package eom.improve.kafkaboot.service
22

33
import eom.improve.kafkaboot.model.FilmEntity
4+
import eom.improve.kafkaboot.model.InventoryEntity
5+
import eom.improve.kafkaboot.model.RentalEntity
46
import eom.improve.kafkaboot.repository.*
7+
import org.springframework.data.domain.Pageable
58
import org.springframework.stereotype.Service
69
import reactor.core.publisher.Flux
710
import reactor.core.publisher.Mono
@@ -17,6 +20,9 @@ class FilmService(
1720
private val filmActorRepository: FilmActorRepository,
1821
private val filmCategoryRepository: FilmCategoryRepository
1922
) {
23+
24+
fun findAllByPageable(pageable: Pageable) : Flux<FilmEntity> = filmRepository.findAllBy(pageable)
25+
2026
fun findAll() : Flux<FilmEntity> = filmRepository.findAllBy()
2127

2228
fun updateFilm(updatedFilm : FilmEntity) : Mono<FilmEntity> {
@@ -34,31 +40,31 @@ class FilmService(
3440
return filmRepository.findById(filmId)
3541
.switchIfEmpty(RuntimeException("Not registered film").toMono())
3642
.flatMap { filmEn ->
37-
inventoryRepository.findAllByFilmId(filmEn.filmId)
38-
.flatMap { inventoryEn ->
39-
rentalRepository.findAllByInventoryId(inventoryEn.inventoryId)
40-
.flatMap { rentalEn ->
41-
paymentRepository.findAllByRentalId(rentalEn.rentalId)
42-
.flatMap { paymentEn ->
43-
paymentRepository.deleteByPaymentId(paymentEn.paymentId)
44-
}
45-
.then(rentalEn.toMono())
46-
}
47-
.flatMap { rentalEn ->
43+
inventoryRepository.findAllByFilmId(filmEn.filmId)
44+
.flatMap { inventoryEn ->
45+
rentalRepository.findAllByInventoryId(inventoryEn.inventoryId)
46+
.flatMap { rentalEn ->
47+
paymentRepository.findAllByRentalId(rentalEn.rentalId)
48+
.flatMap { paymentEn ->
49+
paymentRepository.deleteByPaymentId(paymentEn.paymentId)
50+
}
51+
.then(rentalEn.toMono())
52+
}
53+
.flatMap { rentalEn ->
4854
rentalRepository.deleteByRentalId(rentalEn.rentalId)
49-
}.then(inventoryEn.toMono())
50-
}
51-
.flatMap { inventoryEn ->
52-
inventoryRepository.deleteByInventoryId(inventoryEn.inventoryId)
53-
}
54-
.then(filmEn.toMono())
55+
}.then(inventoryEn.toMono())
56+
}
57+
.flatMap { inventoryEn ->
58+
inventoryRepository.deleteByInventoryId(inventoryEn.inventoryId)
59+
}
60+
.then(filmEn.toMono())
5561
}
5662
.flatMap { filmEn ->
5763
Mono.zip(
5864
filmActorRepository.findAllByFilmId(filmEn.filmId)
5965
.flatMap { filmActorRepository.deleteByFilmId(it.filmId) }
6066
.then()
61-
,
67+
,
6268
filmCategoryRepository.findAllByFilmId(filmEn.filmId)
6369
.flatMap { filmCategoryRepository.deleteByFilmId(it.filmId) }
6470
.then()

src/main/resources/application.yml

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
spring:
22
r2dbc:
33
url: r2dbc:postgresql://localhost:5432/dvdrental
4-
username: postgres
5-
password: example
4+
username: my_user
5+
password: my_password
66

77
#kafka
88
kafka:
@@ -20,3 +20,6 @@ spring:
2020
spring.json.use.type.headers: false
2121
properties:
2222
spring.json.trusted.packages: '*'
23+
24+
server:
25+
port: 7477

0 commit comments

Comments
 (0)