Skip to content

Commit

Permalink
FEATURE : (on-going) implement cascade delete #4
Browse files Browse the repository at this point in the history
  • Loading branch information
stephano-tri committed Feb 13, 2024
1 parent e3902cd commit 5ec1a83
Showing 1 changed file with 33 additions and 4 deletions.
37 changes: 33 additions & 4 deletions src/main/kotlin/eom/improve/kafkaboot/service/FilmService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@ 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 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


@Service
class FilmService(
private val filmRepository : FilmRepository
private val filmRepository : FilmRepository,
private val paymentRepository : PaymentRepository,
private val inventoryRepository : InventoryRepository,
private val rentalRepository : RentalRepository
) {
fun findAll() : Flux<FilmEntity> = filmRepository.findAllBy()

Expand All @@ -23,11 +31,32 @@ class FilmService(
return filmRepository.save(toBeSavedFilm);
}

@Transactional
fun deleteFilm(filmId : Int) : Mono<Void> {
// need to implement cascade delete for table data that set foreign key
// 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")))
.flatMap { filmRepository.deleteById(filmId) }
.then()
.flatMap { filmEn ->
inventoryRepository.findAllByFilmId(filmId)
.switchIfEmpty { filmRepository.delete(filmEn).then() }
.flatMap { inventoryEn ->
rentalRepository.findAllByInventoryId(inventoryEn.inventoryId)
.flatMap { rentalEn ->
paymentRepository.findAllByRentalId(rentalEn.rentalId)
.flatMap { paymentRepository.delete(it) }
.then(rentalEn.toMono())
}
.flatMap {
rentalRepository.delete(it)
.then(inventoryEn.toMono())
}
}
.flatMap {
inventoryRepository.delete(it)
.then(filmEn.toMono())
}
.flatMap { filmRepository.delete(it).then() }
.then()
}
}
}

0 comments on commit 5ec1a83

Please sign in to comment.