https://kotlinlang.org logo
#exposed
Title
# exposed
j

John Coleman

11/23/2023, 10:29 AM
Another question on 1 to many is how to sync the changes from my domain class, here for example I will delete all the related legs using the foreign key and then insert a fresh set. This is not so bad for this particular case, but would not be a solution if the legs were actually modified, maybe some being deleted, some new or some changed. How could that be implemented?
Copy code
private fun update(cargo: Cargo) {
    mapDeliveryFields(CargoEntity[cargo.id!!], cargo)
    if (cargo is Cargo.Routed) {
        Legs.deleteWhere { cargoId eq cargo.id!! }
        cargo.itinerary.legs.forEach {
            it.id = LegEntity.new {
                loadTime = it.loadTime.toKotlinDateTime()
                unloadTime = it.unloadTime.toKotlinDateTime()
                loadLocation = LocationEntity[it.loadLocation.id!!]
                unloadLocation = LocationEntity[it.unloadLocation.id!!]
                voyage = VoyageEntity[it.voyage.id!!]
                this.cargo = CargoEntity[cargo.id!!]
            }.id.value
        }
    }
}