Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Tableau de bord]: sauvegarde du tableau de bord et gestion des onglets #1765

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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>,
val regulatoryAreas: List<Int>,
val reportings: List<Int>,
val vigilanceAreas: List<Int>,
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import fr.gouv.cacem.monitorenv.domain.entities.regulatoryArea.RegulatoryAreaEnt
import fr.gouv.cacem.monitorenv.domain.entities.vigilanceArea.VigilanceAreaEntity
import fr.gouv.cacem.monitorenv.domain.use_cases.reportings.dtos.ReportingDTO

class ExtractedAreaEntity(
data class ExtractedAreaEntity(
val inseeCode: String?,
val reportings: List<ReportingDTO>,
val regulatoryAreas: List<RegulatoryAreaEntity>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,7 @@ enum class BackendUsageErrorCode {

/** Thrown when an entity contain an unvalid property. */
UNVALID_PROPERTY,

/** Thrown when an entity could not be saved. */
ENTITY_NOT_SAVED,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package fr.gouv.cacem.monitorenv.domain.repositories

import fr.gouv.cacem.monitorenv.domain.entities.dashboard.DashboardEntity

interface IDashboardRepository {
fun save(dashboard: DashboardEntity): DashboardEntity
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package fr.gouv.cacem.monitorenv.domain.use_cases.dashboard

import fr.gouv.cacem.monitorenv.config.UseCase
import fr.gouv.cacem.monitorenv.domain.entities.dashboard.DashboardEntity
import fr.gouv.cacem.monitorenv.domain.exceptions.BackendUsageErrorCode
import fr.gouv.cacem.monitorenv.domain.exceptions.BackendUsageException
import fr.gouv.cacem.monitorenv.domain.repositories.IDashboardRepository
import org.slf4j.LoggerFactory

@UseCase
class SaveDashboard(
private val dashboardRepository: IDashboardRepository,
) {
private val logger = LoggerFactory.getLogger(SaveDashboard::class.java)

fun execute(dashboard: DashboardEntity): DashboardEntity {
try {
return dashboardRepository.save(dashboard)
} catch (e: Exception) {
val errorMessage = "dashboard ${dashboard.id} couldn't be saved"
logger.error(errorMessage, e)
throw BackendUsageException(BackendUsageErrorCode.ENTITY_NOT_SAVED, message = errorMessage)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package fr.gouv.cacem.monitorenv.infrastructure.api.adapters.bff.inputs.dashboards

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>,
val controlUnits: List<Int>,
val regulatoryAreas: List<Int>,
val reportings: List<Int>,
val vigilanceAreas: List<Int>,
) {
fun toDashboardEntity(): DashboardEntity {
return DashboardEntity(
id = id,
name = name,
geom = geom,
comments = comments,
createdAt = createdAt,
updatedAt = updatedAt,
inseeCode = inseeCode,
amps = amps,
controlUnits = controlUnits,
reportings = reportings,
regulatoryAreas = regulatoryAreas,
vigilanceAreas = vigilanceAreas,
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package fr.gouv.cacem.monitorenv.infrastructure.api.adapters.bff.outputs.dashboards

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>,
val regulatoryAreas: List<Int>,
val reportings: List<Int>,
val vigilanceAreas: List<Int>,
) {
companion object {
fun fromDashboardEntity(dashboardEntity: DashboardEntity): DashboardDataOutput {
return DashboardDataOutput(
id = dashboardEntity.id,
name = dashboardEntity.name,
geom = dashboardEntity.geom,
comments = dashboardEntity.comments,
createdAt = dashboardEntity.createdAt,
updatedAt = dashboardEntity.updatedAt,
inseeCode = dashboardEntity.inseeCode,
amps = dashboardEntity.amps,
controlUnits = dashboardEntity.controlUnits,
regulatoryAreas = dashboardEntity.regulatoryAreas,
reportings = dashboardEntity.reportings,
vigilanceAreas = dashboardEntity.vigilanceAreas,
)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package fr.gouv.cacem.monitorenv.infrastructure.api.adapters.bff.outputs
package fr.gouv.cacem.monitorenv.infrastructure.api.adapters.bff.outputs.dashboards

import fr.gouv.cacem.monitorenv.domain.entities.dashboard.ExtractedAreaEntity
import fr.gouv.cacem.monitorenv.infrastructure.api.adapters.bff.outputs.AMPDataOutput
import fr.gouv.cacem.monitorenv.infrastructure.api.adapters.bff.outputs.AMPDataOutput.Companion.fromAMPEntity
import fr.gouv.cacem.monitorenv.infrastructure.api.adapters.bff.outputs.RegulatoryAreaDataOutput
import fr.gouv.cacem.monitorenv.infrastructure.api.adapters.bff.outputs.RegulatoryAreaDataOutput.Companion.fromRegulatoryAreaEntity
import fr.gouv.cacem.monitorenv.infrastructure.api.adapters.bff.outputs.reportings.ReportingDataOutput
import fr.gouv.cacem.monitorenv.infrastructure.api.adapters.bff.outputs.vigilanceArea.VigilanceAreasDataOutput
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,41 @@
package fr.gouv.cacem.monitorenv.infrastructure.api.endpoints.bff.v1

import fr.gouv.cacem.monitorenv.domain.use_cases.dashboard.ExtractArea
import fr.gouv.cacem.monitorenv.infrastructure.api.adapters.bff.outputs.ExtractedAreaDataOutput
import fr.gouv.cacem.monitorenv.infrastructure.api.adapters.bff.outputs.ExtractedAreaDataOutput.Companion.fromExtractAreaEntity
import fr.gouv.cacem.monitorenv.domain.use_cases.dashboard.SaveDashboard
import fr.gouv.cacem.monitorenv.infrastructure.api.adapters.bff.inputs.dashboards.DashboardDataInput
import fr.gouv.cacem.monitorenv.infrastructure.api.adapters.bff.outputs.dashboards.DashboardDataOutput
import fr.gouv.cacem.monitorenv.infrastructure.api.adapters.bff.outputs.dashboards.DashboardDataOutput.Companion.fromDashboardEntity
import fr.gouv.cacem.monitorenv.infrastructure.api.adapters.bff.outputs.dashboards.ExtractedAreaDataOutput
import fr.gouv.cacem.monitorenv.infrastructure.api.adapters.bff.outputs.dashboards.ExtractedAreaDataOutput.Companion.fromExtractAreaEntity
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import org.locationtech.jts.io.WKTReader
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PutMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("/bff/v1/dashboard")
@RequestMapping("/bff/v1/dashboards")
@Tag(name = "BFF.Dashboard")
class Dashboard(private val extractArea: ExtractArea) {
class Dashboard(private val extractArea: ExtractArea, private val saveDashboard: SaveDashboard) {
@GetMapping("/extract")
@Operation(summary = "Extract all data that intercept the given geometry")
fun get(
fun extract(
@RequestParam(name = "geometry") pGeometry: String,
): ExtractedAreaDataOutput {
val wktReader = WKTReader()
val geometry = wktReader.read(pGeometry)
return fromExtractAreaEntity(extractArea.execute(geometry = geometry))
}

@PutMapping("", consumes = ["application/json"])
@Operation(summary = "create or update the given dashboard")
fun put(
@RequestBody dashboardDataInput: DashboardDataInput,
): DashboardDataOutput {
return fromDashboardEntity(saveDashboard.execute(dashboardDataInput.toDashboardEntity()))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package fr.gouv.cacem.monitorenv.infrastructure.database.model

import fr.gouv.cacem.monitorenv.infrastructure.database.model.reportings.ReportingModel
import jakarta.persistence.Column
import jakarta.persistence.Entity
import jakarta.persistence.FetchType
import jakarta.persistence.GeneratedValue
import jakarta.persistence.GenerationType
import jakarta.persistence.Id
import jakarta.persistence.JoinColumn
import jakarta.persistence.ManyToOne
import jakarta.persistence.OneToOne
import jakarta.persistence.Table
import java.util.UUID

@Entity
@Table(name = "dashboard_datas")
data class DashboardDatasModel(
@Id
@Column(name = "id", unique = true, nullable = false)
@GeneratedValue(strategy = GenerationType.UUID)
val id: UUID?,
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "dashboard_id")
var dashboard: DashboardModel?,
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "amp_cacem_id")
val amp: AMPModel?,
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "reportings_id")
val reportingModel: ReportingModel?,
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "vigilance_area_id")
val vigilanceAreaModel: VigilanceAreaModel?,
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "regulations_cacem_id")
val regulatoryAreaModel: RegulatoryAreaModel?,
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "control_unit_id")
val controlUnitModel: ControlUnitModel?,
@Column(name = "insee_code")
val inseeCode: String?,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package fr.gouv.cacem.monitorenv.infrastructure.database.model

import fr.gouv.cacem.monitorenv.domain.entities.dashboard.DashboardEntity
import jakarta.persistence.CascadeType
import jakarta.persistence.Column
import jakarta.persistence.Entity
import jakarta.persistence.FetchType
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
@Table(name = "dashboard")
data class DashboardModel(
@Id
@Column(name = "id", unique = true, nullable = false)
@GeneratedValue(strategy = GenerationType.UUID)
val id: UUID?,
val name: String,
val geom: Geometry,
val comments: String?,
var createdAt: ZonedDateTime?,
var updatedAt: ZonedDateTime?,
@OneToMany(
mappedBy = "dashboard",
fetch = FetchType.LAZY,
cascade = [CascadeType.ALL],
)
val dashboardDatas: MutableList<DashboardDatasModel>,
) {
fun toDashboardEntity(): DashboardEntity {
val amps: MutableList<Int> = mutableListOf()
val regulatoryAreas: MutableList<Int> = mutableListOf()
val vigilanceAreas: MutableList<Int> = mutableListOf()
val reportings: MutableList<Int> = mutableListOf()
val controlUnits: MutableList<Int> = mutableListOf()
var inseeCode: String? = null
dashboardDatas.forEach { datas ->
datas.amp.let {
if (it?.id != null) {
amps.add(it.id)
}
}
datas.regulatoryAreaModel.let {
if (it?.id != null) {
regulatoryAreas.add(it.id)
}
}
datas.vigilanceAreaModel.let {
if (it?.id != null) {
vigilanceAreas.add(it.id)
}
}
datas.reportingModel.let {
if (it?.id != null) {
reportings.add(it.id)
}
}
datas.controlUnitModel.let {
if (it?.id != null) {
controlUnits.add(it.id)
}
}
if (datas.inseeCode != null) {
inseeCode = datas.inseeCode
}
}
return DashboardEntity(
id = id,
name = name,
geom = geom,
comments = comments,
createdAt = createdAt,
updatedAt = updatedAt,
inseeCode = inseeCode,
amps = amps,
controlUnits = controlUnits,
regulatoryAreas = regulatoryAreas,
reportings = reportings,
vigilanceAreas = vigilanceAreas,
)
}

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,
dashboardDatasModels: List<DashboardDatasModel>,
): DashboardModel {
val dashboardModel =
DashboardModel(
id = dashboardEntity.id,
name = dashboardEntity.name,
geom = dashboardEntity.geom,
comments = dashboardEntity.comments,
createdAt = dashboardEntity.createdAt,
updatedAt = dashboardEntity.updatedAt,
dashboardDatas = mutableListOf(),
)
dashboardDatasModels.forEach {
dashboardModel.addDashboardDatas(it)
}
return dashboardModel
}
}
}
Loading
Loading