Skip to content

Commit

Permalink
add MissionRestController
Browse files Browse the repository at this point in the history
  • Loading branch information
aleckvincent committed Jan 6, 2025
1 parent 32f2fd4 commit c9e609e
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package fr.gouv.dgampa.rapportnav.domain.repositories.v2.mission

import fr.gouv.dgampa.rapportnav.domain.entities.mission.v2.env.MissionEntity
import fr.gouv.dgampa.rapportnav.infrastructure.monitorenv.v2.inputs.CreateOrUpdateMissionDataInput
import fr.gouv.dgampa.rapportnav.infrastructure.api.bff.model.v2.MissionEnv

interface IEnvMissionRepository {
fun createMission(input: CreateOrUpdateMissionDataInput): MissionEntity?
fun createMission(mission: MissionEnv): MissionEntity?
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import fr.gouv.dgampa.rapportnav.domain.entities.mission.env.controlResources.Le
import fr.gouv.dgampa.rapportnav.domain.entities.mission.v2.env.MissionEntity
import fr.gouv.dgampa.rapportnav.domain.repositories.v2.controlUnit.IEnvControlUnitRepository
import fr.gouv.dgampa.rapportnav.domain.repositories.v2.mission.IEnvMissionRepository
import fr.gouv.dgampa.rapportnav.infrastructure.monitorenv.v2.inputs.CreateOrUpdateMissionDataInput
import fr.gouv.dgampa.rapportnav.infrastructure.api.bff.model.v2.MissionEnv
import org.slf4j.LoggerFactory

@UseCase
Expand All @@ -15,8 +15,7 @@ class CreateEnvMission(
) {
private val logger = LoggerFactory.getLogger(CreateEnvMission::class.java)


fun execute(input: CreateOrUpdateMissionDataInput, controlUnitIds: List<Int>?): MissionEntity? {
fun execute(mission: MissionEnv, controlUnitIds: List<Int>?): MissionEntity? {
try {
if (controlUnitIds !== null && controlUnitIds.isNotEmpty()) {
val controlUnits = monitorEnvControlUnitRepo.findAll()
Expand All @@ -25,9 +24,9 @@ class CreateEnvMission(
?.filter { it.id in controlUnitIds }
?: emptyList()

input.controlUnits = matchedControlUnits
mission.controlUnits = matchedControlUnits

return monitorEnvRepo.createMission(input)
return monitorEnvRepo.createMission(mission)
}

throw Exception("CreateEnvMission : controlUnit is empty for this user")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package fr.gouv.dgampa.rapportnav.infrastructure.api.bff.model.v2

import fr.gouv.dgampa.rapportnav.domain.entities.mission.env.MissionSourceEnum
import fr.gouv.dgampa.rapportnav.domain.entities.mission.env.MissionTypeEnum
import fr.gouv.dgampa.rapportnav.domain.entities.mission.env.controlResources.LegacyControlUnitEntity
import fr.gouv.dgampa.rapportnav.domain.entities.mission.v2.env.MissionEntity
import org.locationtech.jts.geom.MultiPolygon
import java.time.ZonedDateTime

data class MissionEnv(
val id: Int? = null,
val missionTypes: List<MissionTypeEnum>,
var controlUnits: List<LegacyControlUnitEntity> = listOf(),
val openBy: String? = null,
val completedBy: String? = null,
val observationsCacem: String? = null,
val observationsCnsp: String? = null,
val facade: String? = null,
val geom: MultiPolygon? = null,
val startDateTimeUtc: ZonedDateTime,
val endDateTimeUtc: ZonedDateTime? = null,
val missionSource: MissionSourceEnum,
val hasMissionOrder: Boolean,
val isUnderJdp: Boolean = false,
val isGeometryComputedFromControls: Boolean = false,
) {

companion object {
fun fromMissionEntity(mission: MissionEntity): MissionEnv {
return MissionEnv(
id = mission.id,
missionSource = mission.missionSource,
startDateTimeUtc = mission.startDateTimeUtc,
endDateTimeUtc = mission.endDateTimeUtc,
openBy = mission.openBy,
controlUnits = mission.controlUnits,
hasMissionOrder = mission.hasMissionOrder,
missionTypes = mission.missionTypes,
)
}
}
fun toMissionEntity(): MissionEntity {
return MissionEntity(
id = this.id,
missionTypes = this.missionTypes,
controlUnits = this.controlUnits,
openBy = this.openBy,
completedBy = this.completedBy,
observationsCacem = this.observationsCacem,
observationsCnsp = this.observationsCnsp,
facade = this.facade,
geom = this.geom,
startDateTimeUtc = this.startDateTimeUtc,
endDateTimeUtc = this.endDateTimeUtc,
isDeleted = false,
missionSource = this.missionSource,
hasMissionOrder = this.hasMissionOrder,
isUnderJdp = this.isUnderJdp,
isGeometryComputedFromControls = this.isGeometryComputedFromControls,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class MissionController(
private val getMission: GetMission,
private val getControlUnitsForUser: GetControlUnitsForUser,
private val fakeMissionData: FakeMissionData,
private val createMission: CreateEnvMission
) {

private val logger = LoggerFactory.getLogger(MissionController::class.java)
Expand Down Expand Up @@ -66,17 +65,4 @@ class MissionController(
throw Exception(e)
}
}

@MutationMapping
fun create(@Argument input: CreateOrUpdateMissionDataInput): MissionEntity? {
try {
return createMission.execute(
input,
controlUnitIds = getControlUnitsForUser.execute()
)
} catch (e: Exception) {
logger.error("MissionController (ULAM v2) - failed to create mission from MonitorEnv", e)
throw Exception(e)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package fr.gouv.dgampa.rapportnav.infrastructure.api.bff.v2

import fr.gouv.dgampa.rapportnav.domain.use_cases.mission.CreateEnvMission
import fr.gouv.dgampa.rapportnav.domain.use_cases.user.GetControlUnitsForUser
import fr.gouv.dgampa.rapportnav.infrastructure.api.bff.model.v2.MissionEnv
import io.swagger.v3.oas.annotations.Operation
import org.slf4j.LoggerFactory
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("/api/v2/missions")
class MissionRestController(
private val createEnvMission: CreateEnvMission,
private val getControlUnitsForUser: GetControlUnitsForUser,
) {

private val logger = LoggerFactory.getLogger(MissionRestController::class.java)

@PostMapping("")
@Operation(summary = "Create a new MonitorEnv mission")
fun create(
@RequestBody body: MissionEnv
): MissionEnv? {
try {
val mission = createEnvMission.execute(
mission = body,
controlUnitIds = getControlUnitsForUser.execute()
)

if (mission != null) {
return MissionEnv.fromMissionEntity(mission)
}

throw RuntimeException("Error - can't create new MonitorEnv mission")
}
catch (e: Exception) {
logger.error("Error while creating MonitorEnv mission : ", e)
return null
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.fasterxml.jackson.module.kotlin.readValue
import fr.gouv.dgampa.rapportnav.config.HttpClientFactory
import fr.gouv.dgampa.rapportnav.domain.entities.mission.v2.env.MissionEntity
import fr.gouv.dgampa.rapportnav.domain.repositories.v2.mission.IEnvMissionRepository
import fr.gouv.dgampa.rapportnav.infrastructure.monitorenv.v2.inputs.CreateOrUpdateMissionDataInput
import fr.gouv.dgampa.rapportnav.infrastructure.api.bff.model.v2.MissionEnv
import fr.gouv.dgampa.rapportnav.infrastructure.monitorenv.v2.outputs.MissionDataOutput
import fr.gouv.dgampa.rapportnav.infrastructure.utils.GsonSerializer
import org.n52.jackson.datatype.jts.JtsModule
Expand All @@ -28,17 +28,19 @@ class APIEnvMissionRepositoryV2(

private val client = clientFactory.create();

private val host = System.getenv("MONITORENV_HOST")
// private val host = System.getenv("MONITORENV_HOST")

override fun createMission(input: CreateOrUpdateMissionDataInput): MissionEntity? {
private val host = "https://monitorenv.din.developpement-durable.gouv.fr"

override fun createMission(mission: MissionEnv): MissionEntity? {
val url = "$host/api/v1/missions";
logger.info("Sending POST request for Env mission creation URL: $url")
return try {

val request = HttpRequest
.newBuilder()
.uri(URI.create(url))
.method("POST", HttpRequest.BodyPublishers.ofString(gson.toJson(input)))
.method("POST", HttpRequest.BodyPublishers.ofString(gson.toJson(mission)))
.header("Content-Type", "application/json")
.build();

Expand Down

0 comments on commit c9e609e

Please sign in to comment.