Skip to content

Commit

Permalink
feat: claire's review
Browse files Browse the repository at this point in the history
  • Loading branch information
maximeperrault authored and maximeperraultdev committed Oct 14, 2024
1 parent 9602fca commit f34f31e
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 125 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package fr.gouv.cacem.monitorenv.domain.entities.dashboard

import org.locationtech.jts.geom.Geometry
import java.time.ZonedDateTime
import java.util.UUID

data class DashboardEntity(
val id: UUID?,
val name: String,
val geom: Geometry,
val comments: String?,
val createdAt: ZonedDateTime?,
val updatedAt: ZonedDateTime?,
val inseeCode: String?,
val amps: List<Int>,
val controlUnits: List<Int>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ enum class BackendUsageErrorCode {
/** Thrown when an entity contain an unvalid property. */
UNVALID_PROPERTY,

/** Thrown when an entity could be saved. */
/** Thrown when an entity could not be saved. */
ENTITY_NOT_SAVED,
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package fr.gouv.cacem.monitorenv.infrastructure.api.adapters.bff.inputs.dashboar

import fr.gouv.cacem.monitorenv.domain.entities.dashboard.DashboardEntity
import org.locationtech.jts.geom.Geometry
import java.time.ZonedDateTime
import java.util.UUID

class DashboardDataInput(
val id: UUID?,
val name: String,
val geom: Geometry,
val createdAt: ZonedDateTime?,
val updatedAt: ZonedDateTime?,
val comments: String?,
val inseeCode: String?,
val amps: List<Int>,
Expand All @@ -22,6 +25,8 @@ class DashboardDataInput(
name = name,
geom = geom,
comments = comments,
createdAt = createdAt,
updatedAt = updatedAt,
inseeCode = inseeCode,
amps = amps,
controlUnits = controlUnits,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package fr.gouv.cacem.monitorenv.infrastructure.api.adapters.bff.outputs.dashboa

import fr.gouv.cacem.monitorenv.domain.entities.dashboard.DashboardEntity
import org.locationtech.jts.geom.Geometry
import java.time.ZonedDateTime
import java.util.UUID

class DashboardDataOutput(
val id: UUID?,
val name: String,
val geom: Geometry,
val comments: String?,
val createdAt: ZonedDateTime?,
val updatedAt: ZonedDateTime?,
val inseeCode: String?,
val amps: List<Int>,
val controlUnits: List<Int>,
Expand All @@ -23,6 +26,8 @@ class DashboardDataOutput(
name = dashboardEntity.name,
geom = dashboardEntity.geom,
comments = dashboardEntity.comments,
createdAt = dashboardEntity.createdAt,
updatedAt = dashboardEntity.updatedAt,
inseeCode = dashboardEntity.inseeCode,
amps = dashboardEntity.amps,
controlUnits = dashboardEntity.controlUnits,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ import jakarta.persistence.GeneratedValue
import jakarta.persistence.GenerationType
import jakarta.persistence.Id
import jakarta.persistence.OneToMany
import jakarta.persistence.PrePersist
import jakarta.persistence.PreUpdate
import jakarta.persistence.Table
import org.locationtech.jts.geom.Geometry
import java.time.ZonedDateTime
import java.util.UUID

@Entity
Expand All @@ -23,6 +26,8 @@ data class DashboardModel(
val name: String,
val geom: Geometry,
val comments: String?,
var createdAt: ZonedDateTime?,
var updatedAt: ZonedDateTime?,
@OneToMany(
mappedBy = "dashboard",
fetch = FetchType.LAZY,
Expand Down Expand Up @@ -72,6 +77,8 @@ data class DashboardModel(
name = name,
geom = geom,
comments = comments,
createdAt = createdAt,
updatedAt = updatedAt,
inseeCode = inseeCode,
amps = amps,
controlUnits = controlUnits,
Expand All @@ -81,11 +88,21 @@ data class DashboardModel(
)
}

fun addBriefing(dashboardDatasModel: DashboardDatasModel) {
fun addDashboardDatas(dashboardDatasModel: DashboardDatasModel) {
dashboardDatasModel.dashboard = this
this.dashboardDatas.add(dashboardDatasModel)
}

@PrePersist
private fun prePersist() {
this.createdAt = ZonedDateTime.now()
}

@PreUpdate
private fun preUpdate() {
this.updatedAt = ZonedDateTime.now()
}

companion object {
fun fromDashboardEntity(
dashboardEntity: DashboardEntity,
Expand All @@ -97,10 +114,12 @@ data class DashboardModel(
name = dashboardEntity.name,
geom = dashboardEntity.geom,
comments = dashboardEntity.comments,
createdAt = dashboardEntity.createdAt,
updatedAt = dashboardEntity.updatedAt,
dashboardDatas = mutableListOf(),
)
dashboardDatasModels.forEach {
dashboardModel.addBriefing(it)
dashboardModel.addDashboardDatas(it)
}
return dashboardModel
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import fr.gouv.cacem.monitorenv.domain.repositories.IDashboardRepository
import fr.gouv.cacem.monitorenv.infrastructure.database.model.DashboardDatasModel
import fr.gouv.cacem.monitorenv.infrastructure.database.model.DashboardModel.Companion.fromDashboardEntity
import fr.gouv.cacem.monitorenv.infrastructure.database.repositories.interfaces.IDBAMPRepository
import fr.gouv.cacem.monitorenv.infrastructure.database.repositories.interfaces.IDBBriefingRepository
import fr.gouv.cacem.monitorenv.infrastructure.database.repositories.interfaces.IDBControlUnitRepository
import fr.gouv.cacem.monitorenv.infrastructure.database.repositories.interfaces.IDBDashboardDatasRepository
import fr.gouv.cacem.monitorenv.infrastructure.database.repositories.interfaces.IDBDashboardRepository
import fr.gouv.cacem.monitorenv.infrastructure.database.repositories.interfaces.IDBRegulatoryAreaRepository
import fr.gouv.cacem.monitorenv.infrastructure.database.repositories.interfaces.IDBReportingRepository
Expand All @@ -17,7 +17,7 @@ import org.springframework.transaction.annotation.Transactional
@Repository
class JpaDashboardRepository(
private val dashboardRepository: IDBDashboardRepository,
private val briefingRepository: IDBBriefingRepository,
private val dashboardDatasRepository: IDBDashboardDatasRepository,
private val ampRepository: IDBAMPRepository,
private val controlUnitRepository: IDBControlUnitRepository,
private val regulatoryAreaRepository: IDBRegulatoryAreaRepository,
Expand All @@ -27,24 +27,24 @@ class JpaDashboardRepository(
IDashboardRepository {
@Transactional
override fun save(dashboard: DashboardEntity): DashboardEntity {
dashboard.id?.let { briefingRepository.deleteAllByDashboardId(dashboardId = it) }
val briefingsToSave: MutableList<DashboardDatasModel> = mutableListOf()
addAmps(dashboard, briefingsToSave)
addInseeCode(dashboard, briefingsToSave)
addReportings(dashboard, briefingsToSave)
addVigilanceAreas(dashboard, briefingsToSave)
addRegulatoryAreas(dashboard, briefingsToSave)
addControlUnits(dashboard, briefingsToSave)
val dashboardModel = dashboardRepository.save(fromDashboardEntity(dashboard, briefingsToSave))
dashboard.id?.let { dashboardDatasRepository.deleteAllByDashboardId(dashboardId = it) }
val dashboardDatasToSave: MutableList<DashboardDatasModel> = mutableListOf()
addAmps(dashboard, dashboardDatasToSave)
addInseeCode(dashboard, dashboardDatasToSave)
addReportings(dashboard, dashboardDatasToSave)
addVigilanceAreas(dashboard, dashboardDatasToSave)
addRegulatoryAreas(dashboard, dashboardDatasToSave)
addControlUnits(dashboard, dashboardDatasToSave)
val dashboardModel = dashboardRepository.saveAndFlush(fromDashboardEntity(dashboard, dashboardDatasToSave))
return dashboardModel.toDashboardEntity()
}

private fun addRegulatoryAreas(
dashboard: DashboardEntity,
briefingsToSave: MutableList<DashboardDatasModel>,
dashboardDatasToSave: MutableList<DashboardDatasModel>,
) {
dashboard.regulatoryAreas.forEach {
briefingsToSave.add(
dashboardDatasToSave.add(
DashboardDatasModel(
id = null,
dashboard = null,
Expand All @@ -61,10 +61,10 @@ class JpaDashboardRepository(

private fun addVigilanceAreas(
dashboard: DashboardEntity,
briefingsToSave: MutableList<DashboardDatasModel>,
dashboardDatasToSave: MutableList<DashboardDatasModel>,
) {
dashboard.vigilanceAreas.forEach {
briefingsToSave.add(
dashboardDatasToSave.add(
DashboardDatasModel(
id = null,
dashboard = null,
Expand All @@ -81,10 +81,10 @@ class JpaDashboardRepository(

private fun addReportings(
dashboard: DashboardEntity,
briefingsToSave: MutableList<DashboardDatasModel>,
dashboardDatasToSave: MutableList<DashboardDatasModel>,
) {
dashboard.reportings.forEach {
briefingsToSave.add(
dashboardDatasToSave.add(
DashboardDatasModel(
id = null,
dashboard = null,
Expand All @@ -101,10 +101,10 @@ class JpaDashboardRepository(

private fun addInseeCode(
dashboard: DashboardEntity,
briefingsToSave: MutableList<DashboardDatasModel>,
dashboardDatasToSave: MutableList<DashboardDatasModel>,
) {
dashboard.inseeCode?.let {
briefingsToSave.add(
dashboardDatasToSave.add(
DashboardDatasModel(
id = null,
dashboard = null,
Expand All @@ -121,10 +121,10 @@ class JpaDashboardRepository(

private fun addAmps(
dashboard: DashboardEntity,
briefingsToSave: MutableList<DashboardDatasModel>,
dashboardDatasToSave: MutableList<DashboardDatasModel>,
) {
dashboard.amps.forEach {
briefingsToSave.add(
dashboardDatasToSave.add(
DashboardDatasModel(
id = null,
dashboard = null,
Expand All @@ -141,10 +141,10 @@ class JpaDashboardRepository(

private fun addControlUnits(
dashboard: DashboardEntity,
briefingsToSave: MutableList<DashboardDatasModel>,
dashboardDatasToSave: MutableList<DashboardDatasModel>,
) {
dashboard.controlUnits.forEach {
briefingsToSave.add(
dashboardDatasToSave.add(
DashboardDatasModel(
id = null,
dashboard = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import fr.gouv.cacem.monitorenv.infrastructure.database.model.DashboardDatasMode
import org.springframework.data.jpa.repository.JpaRepository
import java.util.UUID

interface IDBBriefingRepository : JpaRepository<DashboardDatasModel, UUID> {
interface IDBDashboardDatasRepository : JpaRepository<DashboardDatasModel, UUID> {
fun deleteAllByDashboardId(dashboardId: UUID)
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
CREATE TABLE dashboard
(
id uuid PRIMARY KEY,
name VARCHAR(255) NOT NULL,
geom geometry(geometry, 4326) NOT NULL,
comments VARCHAR(255)
id uuid PRIMARY KEY,
name VARCHAR(255) NOT NULL,
geom geometry(geometry, 4326) NOT NULL,
comments VARCHAR(255),
created_at TIMESTAMP NOT NULL,
updated_at TIMESTAMP
);

CREATE TABLE dashboard_datas
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class DashboardFixture {
name = name,
geom = geom,
comments = comments,
createdAt = null,
updatedAt = null,
amps = amps,
regulatoryAreas = regulatoryAreas,
inseeCode = inseeCode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ class DashboardITest {
id = id,
name = name,
comments = comments,
createdAt = null,
updatedAt = null,
geom = geometry,
inseeCode = inseeCode,
amps = amps,
Expand Down
Loading

0 comments on commit f34f31e

Please sign in to comment.