Skip to content

Commit

Permalink
FEATURE : complete film delete function(cascade delete) #4
Browse files Browse the repository at this point in the history
  • Loading branch information
stephano-tri committed Mar 5, 2024
1 parent efe97de commit 4a9ea89
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/main/kotlin/eom/improve/kafkaboot/service/FilmService.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package eom.improve.kafkaboot.service

import eom.improve.kafkaboot.model.FilmEntity
import eom.improve.kafkaboot.repository.FilmRepository
import eom.improve.kafkaboot.repository.InventoryRepository
import eom.improve.kafkaboot.repository.PaymentRepository
import eom.improve.kafkaboot.repository.RentalRepository
import eom.improve.kafkaboot.repository.*
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import reactor.core.publisher.Flux
import reactor.core.publisher.Mono
import reactor.kotlin.core.publisher.toMono
Expand All @@ -17,7 +13,9 @@ class FilmService(
private val filmRepository : FilmRepository,
private val paymentRepository : PaymentRepository,
private val inventoryRepository : InventoryRepository,
private val rentalRepository : RentalRepository
private val rentalRepository : RentalRepository,
private val filmActorRepository: FilmActorRepository,
private val filmCategoryRepository: FilmCategoryRepository
) {
fun findAll() : Flux<FilmEntity> = filmRepository.findAllBy()

Expand All @@ -31,7 +29,6 @@ class FilmService(
return filmRepository.save(toBeSavedFilm);
}

@Transactional
fun deleteFilm(filmId : Int) : Mono<Void> {
// need to implement cascade delete(maybe soft) for table data that set foreign key
return filmRepository.findById(filmId)
Expand All @@ -52,13 +49,24 @@ class FilmService(
}.then(inventoryEn.toMono())
}
.flatMap { inventoryEn ->
println(inventoryEn.inventoryId)
inventoryRepository.deleteByInventoryId(inventoryEn.inventoryId)
}
.then(filmEn.toMono())
}
.flatMap { filmEn ->
filmRepository.deleteByFilmId(filmEn.filmId)
Mono.zip(
filmActorRepository.findAllByFilmId(filmEn.filmId)
.flatMap { filmActorRepository.deleteByFilmId(it.filmId) }
.then()
,
filmCategoryRepository.findAllByFilmId(filmEn.filmId)
.flatMap { filmCategoryRepository.deleteByFilmId(it.filmId) }
.then()
)
.then(filmEn.toMono())
}
.flatMap {
filmRepository.deleteByFilmId(it.filmId)
}
}
}

0 comments on commit 4a9ea89

Please sign in to comment.