Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/feature-#4' into fe…
Browse files Browse the repository at this point in the history
…ature-#4
  • Loading branch information
stephano-tri committed Aug 22, 2024
2 parents ed274b1 + 26a85de commit 27ebbb5
Show file tree
Hide file tree
Showing 9 changed files with 8,908 additions and 21 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
@@ -0,0 +1,7 @@
package eom.improve.kafkaboot.common

data class PaginatedResponse<T>(
val response : List<T>,
val currentPage : Long,
val totalPages : Long
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package eom.improve.kafkaboot.controller

import eom.improve.kafkaboot.common.PaginatedResponse
import eom.improve.kafkaboot.dto.Film
import jakarta.validation.Valid
import org.springframework.web.bind.annotation.*
Expand All @@ -12,6 +13,9 @@ interface FilmController {
@GetMapping("/list/all")
fun getAllFilms() : Mono<List<Film>>

@GetMapping("/list/{page}/{limit}")
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
@@ -1,6 +1,7 @@
package eom.improve.kafkaboot.repository

import eom.improve.kafkaboot.model.FilmEntity
import org.springframework.data.domain.Pageable
import org.springframework.data.r2dbc.repository.R2dbcRepository
import org.springframework.stereotype.Repository
import reactor.core.publisher.Flux
Expand All @@ -10,4 +11,5 @@ import reactor.core.publisher.Mono
interface FilmRepository : R2dbcRepository<FilmEntity, Int> {
fun findAllBy() : Flux<FilmEntity>
fun deleteByFilmId(filmId: Int) : Mono<Void>
fun findAllByOrderByFilmId(pageable: Pageable) : Flux<FilmEntity>
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
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 @@ -16,6 +18,10 @@ class FilmControllerImpl(
.collectSortedList((Comparator<Film> { o1, o2 -> o1.filmId.compareTo(o2.filmId) }))
}

override fun getFilms(page: Long, limit: Long): Mono<PaginatedResponse<Film>> {
return filmService.findAllByPageable(PageRequest.of(page.toInt() , limit.toInt()))
}

override fun modifyFilm(updatedFilm : Film) : Mono<Film> {
return filmService.updateFilm(updatedFilm.convert2Entity())
.flatMap { it.convert2Pojo().toMono() }
Expand Down
48 changes: 30 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,12 @@
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
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 +22,13 @@ class FilmService(
private val filmActorRepository: FilmActorRepository,
private val filmCategoryRepository: FilmCategoryRepository
) {

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()

fun updateFilm(updatedFilm : FilmEntity) : Mono<FilmEntity> {
Expand All @@ -34,31 +46,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 27ebbb5

Please sign in to comment.