From 0d07ecf09c75f90b7db1d628f3818a376192e88d Mon Sep 17 00:00:00 2001 From: stephano-tri Date: Thu, 22 Feb 2024 23:01:19 +0900 Subject: [PATCH] FIX : change delete logic for cascading #4 --- .../eom/improve/kafkaboot/service/FilmService.kt | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/eom/improve/kafkaboot/service/FilmService.kt b/src/main/kotlin/eom/improve/kafkaboot/service/FilmService.kt index 20aad6d..d857058 100644 --- a/src/main/kotlin/eom/improve/kafkaboot/service/FilmService.kt +++ b/src/main/kotlin/eom/improve/kafkaboot/service/FilmService.kt @@ -35,7 +35,7 @@ class FilmService( fun deleteFilm(filmId : Int) : Mono { // need to implement cascade delete(maybe soft) for table data that set foreign key return filmRepository.findById(filmId) - .switchIfEmpty(Mono.error(RuntimeException("Not registered film"))) + .switchIfEmpty(RuntimeException("Not registered film").toMono()) .flatMap { filmEn -> inventoryRepository.findAllByFilmId(filmEn.filmId) .flatMap { inventoryEn -> @@ -49,9 +49,16 @@ class FilmService( } .flatMap { rentalEn -> rentalRepository.deleteByRentalId(rentalEn.rentalId) - } - }.then() + }.then(inventoryEn.toMono()) + } + .flatMap { inventoryEn -> + println(inventoryEn.inventoryId) + inventoryRepository.deleteByInventoryId(inventoryEn.inventoryId) + } + .then(filmEn.toMono()) + } + .flatMap { filmEn -> + filmRepository.deleteByFilmId(filmEn.filmId) } - } }