diff --git a/backend/src/main/kotlin/fr/gouv/cacem/monitorenv/infrastructure/database/model/BaseModel.kt b/backend/src/main/kotlin/fr/gouv/cacem/monitorenv/infrastructure/database/model/BaseModel.kt index a917757373..4d30d63a9d 100644 --- a/backend/src/main/kotlin/fr/gouv/cacem/monitorenv/infrastructure/database/model/BaseModel.kt +++ b/backend/src/main/kotlin/fr/gouv/cacem/monitorenv/infrastructure/database/model/BaseModel.kt @@ -3,6 +3,7 @@ package fr.gouv.cacem.monitorenv.infrastructure.database.model import com.fasterxml.jackson.annotation.JsonManagedReference import fr.gouv.cacem.monitorenv.domain.entities.base.BaseEntity import fr.gouv.cacem.monitorenv.domain.use_cases.base.dtos.FullBaseDTO +import fr.gouv.cacem.monitorenv.infrastructure.database.repositories.exceptions.ForeignKeyConstraintException import jakarta.persistence.* import org.hibernate.annotations.CreationTimestamp import org.hibernate.annotations.UpdateTimestamp @@ -18,7 +19,7 @@ data class BaseModel( @OneToMany(fetch = FetchType.LAZY, mappedBy = "base") @JsonManagedReference - val controlUnitResources: List? = mutableListOf(), + val controlUnitResources: List = listOf(), @Column(name = "latitude", nullable = false) val latitude: Double, @@ -47,7 +48,7 @@ data class BaseModel( ): BaseModel { return BaseModel( id = base.id, - controlUnitResources = controlUnitResourceModels, + controlUnitResources = controlUnitResourceModels ?: listOf(), latitude = base.latitude, longitude = base.longitude, name = base.name, @@ -63,7 +64,7 @@ data class BaseModel( ): BaseModel { return BaseModel( id = fullBase.base.id, - controlUnitResources = controlUnitResourceModels, + controlUnitResources = controlUnitResourceModels ?: listOf(), latitude = fullBase.base.latitude, longitude = fullBase.base.longitude, name = fullBase.base.name, @@ -71,6 +72,15 @@ data class BaseModel( } } + @PreRemove + fun checkControlUnitResourcesBeforeRemoval() { + if (controlUnitResources.isNotEmpty()) { + throw ForeignKeyConstraintException( + "Cannot delete base (ID=$id) due to existing relationships.", + ) + } + } + fun toBase(): BaseEntity { return BaseEntity( id, @@ -83,7 +93,7 @@ data class BaseModel( fun toFullBase(): FullBaseDTO { return FullBaseDTO( base = toBase(), - controlUnitResources = requireNotNull(controlUnitResources).map { it.toControlUnitResource() }, + controlUnitResources = controlUnitResources.map { it.toControlUnitResource() }, ) } } diff --git a/backend/src/main/kotlin/fr/gouv/cacem/monitorenv/infrastructure/database/repositories/JpaBaseRepository.kt b/backend/src/main/kotlin/fr/gouv/cacem/monitorenv/infrastructure/database/repositories/JpaBaseRepository.kt index 937d4243ed..b12798a064 100644 --- a/backend/src/main/kotlin/fr/gouv/cacem/monitorenv/infrastructure/database/repositories/JpaBaseRepository.kt +++ b/backend/src/main/kotlin/fr/gouv/cacem/monitorenv/infrastructure/database/repositories/JpaBaseRepository.kt @@ -15,14 +15,6 @@ class JpaBaseRepository( private val dbBaseRepository: IDBBaseRepository, ) : IBaseRepository { override fun deleteById(baseId: Int) { - val fullBase = findById(baseId) -// println(fullBase.controlUnitResources) - /*if (fullBase.controlUnitResources.isNotEmpty()) { - throw ForeignKeyConstraintException( - "Cannot delete base (ID=$baseId) due to existing relationships.", - ) - }*/ - dbBaseRepository.deleteById(baseId) }