Skip to content

Commit

Permalink
Add administrations archiving & deleting in backoffice
Browse files Browse the repository at this point in the history
  • Loading branch information
ivangabriele committed Sep 28, 2023
1 parent 03c1c40 commit 21d1ac8
Show file tree
Hide file tree
Showing 40 changed files with 411 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ package fr.gouv.cacem.monitorenv.domain.entities.administration

data class AdministrationEntity(
val id: Int? = null,
val isArchived: Boolean,
val name: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import fr.gouv.cacem.monitorenv.domain.entities.administration.AdministrationEnt
import fr.gouv.cacem.monitorenv.domain.use_cases.administration.dtos.FullAdministrationDTO

interface IAdministrationRepository {
fun archiveById(administrationId: Int)

fun deleteById(administrationId: Int)

fun findById(administrationId: Int): FullAdministrationDTO
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package fr.gouv.cacem.monitorenv.domain.use_cases.administration

import fr.gouv.cacem.monitorenv.config.UseCase
import fr.gouv.cacem.monitorenv.domain.repositories.IAdministrationRepository

@UseCase
class ArchiveAdministration(private val administrationRepository: IAdministrationRepository) {
fun execute(administrationId: Int) {
administrationRepository.archiveById(administrationId)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package fr.gouv.cacem.monitorenv.domain.use_cases.administration

import fr.gouv.cacem.monitorenv.config.UseCase
import fr.gouv.cacem.monitorenv.domain.repositories.IAdministrationRepository

@UseCase
class DeleteAdministration(private val administrationRepository: IAdministrationRepository) {
fun execute(administrationId: Int) {
administrationRepository.deleteById(administrationId)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ data class FullAdministrationDTO(
val id: Int? = null,
val controlUnitIds: List<Int>,
val controlUnits: List<ControlUnitEntity>,
val isArchived: Boolean,
val name: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import fr.gouv.cacem.monitorenv.domain.entities.administration.AdministrationEnt

data class CreateOrUpdateAdministrationDataInput(
val id: Int? = null,
val isArchived: Boolean,
val name: String,
) {
fun toAdministration(): AdministrationEntity {
return AdministrationEntity(
id = this.id,
isArchived = this.isArchived,
name = this.name,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import fr.gouv.cacem.monitorenv.domain.entities.administration.AdministrationEnt

data class AdministrationDataOutput(
val id: Int,
val isArchived: Boolean,
val name: String,
) {
companion object {
fun fromAdministration(administration: AdministrationEntity): AdministrationDataOutput {
return AdministrationDataOutput(
id = requireNotNull(administration.id),
isArchived = administration.isArchived,
name = administration.name,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ data class FullAdministrationDataOutput(
val id: Int,
val controlUnitIds: List<Int>,
val controlUnits: List<ControlUnitEntity>,
val isArchived: Boolean,
val name: String,
) {
companion object {
Expand All @@ -15,6 +16,7 @@ data class FullAdministrationDataOutput(
id = requireNotNull(fullAdministration.id),
controlUnits = fullAdministration.controlUnits,
controlUnitIds = fullAdministration.controlUnitIds,
isArchived = fullAdministration.isArchived,
name = fullAdministration.name,
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package fr.gouv.cacem.monitorenv.infrastructure.api.endpoints.publicapi

import fr.gouv.cacem.monitorenv.domain.use_cases.administration.CreateOrUpdateAdministration
import fr.gouv.cacem.monitorenv.domain.use_cases.administration.GetAdministrationById
import fr.gouv.cacem.monitorenv.domain.use_cases.administration.GetAdministrations
import fr.gouv.cacem.monitorenv.domain.use_cases.administration.*
import fr.gouv.cacem.monitorenv.infrastructure.api.adapters.publicapi.inputs.CreateOrUpdateAdministrationDataInput
import fr.gouv.cacem.monitorenv.infrastructure.api.adapters.publicapi.outputs.AdministrationDataOutput
import fr.gouv.cacem.monitorenv.infrastructure.api.adapters.publicapi.outputs.FullAdministrationDataOutput
Expand All @@ -16,10 +14,22 @@ import org.springframework.web.bind.annotation.*
@RequestMapping("/api/v1/administrations")
@Tag(name = "Administrations")
class ApiAdministrationsController(
private val archiveAdministration: ArchiveAdministration,
private val createOrUpdateAdministration: CreateOrUpdateAdministration,
private val deleteAdministration: DeleteAdministration,
private val getAdministrations: GetAdministrations,
private val getAdministrationById: GetAdministrationById,
) {
@PostMapping("/{administrationId}/archive")
@Operation(summary = "Archive an administration")
fun archive(
@PathParam("Administration ID")
@PathVariable(name = "administrationId")
administrationId: Int,
) {
archiveAdministration.execute(administrationId)
}

@PostMapping("", consumes = ["application/json"])
@Operation(summary = "Create an administration")
@ResponseStatus(HttpStatus.CREATED)
Expand All @@ -33,8 +43,18 @@ class ApiAdministrationsController(
return AdministrationDataOutput.fromAdministration(createdAdministration)
}

@DeleteMapping("/{administrationId}")
@Operation(summary = "Delete an administration")
fun delete(
@PathParam("Administration ID")
@PathVariable(name = "administrationId")
controlUnitId: Int,
) {
deleteAdministration.execute(controlUnitId)
}

@GetMapping("/{administrationId}")
@Operation(summary = "Get a administration by its ID")
@Operation(summary = "Get an administration by its ID")
fun get(
@PathParam("Administration ID")
@PathVariable(name = "administrationId")
Expand All @@ -56,7 +76,7 @@ class ApiAdministrationsController(
@PutMapping(value = ["/{administrationId}"], consumes = ["application/json"])
@Operation(summary = "Update an administration")
fun update(
@PathParam("Control unit administration ID")
@PathParam("Administration ID")
@PathVariable(name = "administrationId")
administrationId: Int,
@RequestBody
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ data class AdministrationModel(
@JsonManagedReference
var controlUnits: List<ControlUnitModel>? = mutableListOf(),

@Column(name = "is_archived")
var isArchived: Boolean,

@Column(name = "name", nullable = false, unique = true)
var name: String,

Expand All @@ -44,6 +47,7 @@ data class AdministrationModel(
return AdministrationModel(
id = administration.id,
controlUnits = controlUnitModels,
isArchived = administration.isArchived,
name = administration.name,
)
}
Expand All @@ -52,6 +56,7 @@ data class AdministrationModel(
fun toAdministration(): AdministrationEntity {
return AdministrationEntity(
id,
isArchived,
name,
)
}
Expand All @@ -62,6 +67,7 @@ data class AdministrationModel(
controlUnitIds = requireIds(controlUnits) { it.id },
// TODO Remove `requireNotNullList()` once `controlUnits` is non-nullable.
controlUnits = requireNotNullList(controlUnits).map { it.toControlUnit() },
isArchived,
name,
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
package fr.gouv.cacem.monitorenv.infrastructure.database.model

import fr.gouv.cacem.monitorenv.domain.entities.controlUnit.LegacyControlUnitEntity
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.Table
import jakarta.persistence.*

@Entity
@Table(name = "missions_control_units")
Expand All @@ -35,7 +27,7 @@ data class MissionControlUnitModel(
MissionControlUnitModel(
unit = ControlUnitModel(
id = legacyControlUnit.id,
administration = AdministrationModel(name = legacyControlUnit.administration),
administration = AdministrationModel(isArchived = false, name = legacyControlUnit.administration),
areaNote = null,
controlUnitContacts = listOf(),
controlUnitResources = listOf(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ import org.springframework.transaction.annotation.Transactional
class JpaAdministrationRepository(
private val dbAdministrationRepository: IDBAdministrationRepository,
) : IAdministrationRepository {
@Transactional
override fun archiveById(administrationId: Int) {
dbAdministrationRepository.archiveById(administrationId)
}

override fun deleteById(administrationId: Int) {
dbAdministrationRepository.deleteById(administrationId)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,23 @@ package fr.gouv.cacem.monitorenv.infrastructure.database.repositories.interfaces

import fr.gouv.cacem.monitorenv.infrastructure.database.model.AdministrationModel
import org.hibernate.annotations.DynamicUpdate
import org.springframework.data.jpa.repository.Modifying
import org.springframework.data.jpa.repository.Query
import org.springframework.data.repository.CrudRepository

@DynamicUpdate
interface IDBAdministrationRepository : CrudRepository<AdministrationModel, Int> {
@Modifying(clearAutomatically = true)
@Query(
value = """
UPDATE administrations
SET is_archived = TRUE
WHERE id = :administrationId
""",
nativeQuery = true,
)
fun archiveById(administrationId: Int)

@Query(
value = """
SELECT *
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE public.administrations
ADD COLUMN is_archived BOOLEAN NOT NULL DEFAULT FALSE;
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class CreateOrUpdateAdministrationUTests {
@Test
fun `execute() should return save() result`() {
val newAdministration = AdministrationEntity(
isArchived = false,
name = "Administration Name"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ class CreateOrUpdateReportingUTests {
id = 1,
administration = AdministrationEntity(
id = 1,
isArchived = false,
name = "administration 1",
),
administrationId = 2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import com.nhaarman.mockitokotlin2.any
import fr.gouv.cacem.monitorenv.config.MapperConfiguration
import fr.gouv.cacem.monitorenv.config.WebSecurityConfig
import fr.gouv.cacem.monitorenv.domain.entities.administration.AdministrationEntity
import fr.gouv.cacem.monitorenv.domain.use_cases.administration.CreateOrUpdateAdministration
import fr.gouv.cacem.monitorenv.domain.use_cases.administration.GetAdministrationById
import fr.gouv.cacem.monitorenv.domain.use_cases.administration.GetAdministrations
import fr.gouv.cacem.monitorenv.domain.use_cases.administration.*
import fr.gouv.cacem.monitorenv.domain.use_cases.administration.dtos.FullAdministrationDTO
import fr.gouv.cacem.monitorenv.infrastructure.api.adapters.publicapi.inputs.CreateOrUpdateAdministrationDataInput
import org.hamcrest.Matchers
Expand All @@ -31,9 +29,15 @@ class ApiAdministrationsControllerITests {
@Autowired
private lateinit var mockMvc: MockMvc

@MockBean
private lateinit var archiveAdministration: ArchiveAdministration

@MockBean
private lateinit var createOrUpdateAdministration: CreateOrUpdateAdministration

@MockBean
private lateinit var deleteAdministration: DeleteAdministration

@MockBean
private lateinit var getAdministrationById: GetAdministrationById

Expand All @@ -47,10 +51,12 @@ class ApiAdministrationsControllerITests {
fun `Should create an administration`() {
val expectedCreatedAdministration = AdministrationEntity(
id = 1,
isArchived = false,
name = "Administration Name",
)

val newAdministrationData = CreateOrUpdateAdministrationDataInput(
isArchived = false,
name = "Administration Name",
)
val requestBody = objectMapper.writeValueAsString(newAdministrationData)
Expand All @@ -72,6 +78,7 @@ class ApiAdministrationsControllerITests {
id = 1,
controlUnitIds = listOf(),
controlUnits = listOf(),
isArchived = false,
name = "Administration Name",
)

Expand All @@ -92,13 +99,15 @@ class ApiAdministrationsControllerITests {
id = 1,
controlUnitIds = listOf(),
controlUnits = listOf(),
isArchived = false,
name = "Administration Name",
),

FullAdministrationDTO(
id = 2,
controlUnitIds = listOf(),
controlUnits = listOf(),
isArchived = false,
name = "Administration Name 2",
)
)
Expand All @@ -116,11 +125,13 @@ class ApiAdministrationsControllerITests {
fun `Should update an administration`() {
val expectedUpdatedAdministration = AdministrationEntity(
id = 1,
isArchived = false,
name = "Updated Administration Name",
)

val nextAdministrationData = CreateOrUpdateAdministrationDataInput(
id = 1,
isArchived = false,
name = "Updated Administration Name",
)
val requestBody = objectMapper.writeValueAsString(nextAdministrationData)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class ApiControlUnitsControllerITests {
id = 1,
administration = AdministrationEntity(
id = 0,
isArchived = false,
name = "Administration Name",
),
administrationId = 0,
Expand Down Expand Up @@ -117,6 +118,7 @@ class ApiControlUnitsControllerITests {
id = 1,
administration = AdministrationEntity(
id = 0,
isArchived = false,
name = "Administration Name",
),
administrationId = 0,
Expand All @@ -134,6 +136,7 @@ class ApiControlUnitsControllerITests {
id = 2,
administration = AdministrationEntity(
id = 0,
isArchived = false,
name = "Administration Name",
),
administrationId = 0,
Expand Down
Loading

0 comments on commit 21d1ac8

Please sign in to comment.