Skip to content

Commit

Permalink
Finalize control unit resources archiving
Browse files Browse the repository at this point in the history
  • Loading branch information
ivangabriele committed Oct 24, 2023
1 parent cc5c521 commit f943405
Show file tree
Hide file tree
Showing 17 changed files with 255 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import fr.gouv.cacem.monitorenv.domain.entities.controlUnit.ControlUnitResourceE
import fr.gouv.cacem.monitorenv.domain.use_cases.controlUnit.dtos.FullControlUnitResourceDTO

interface IControlUnitResourceRepository {
fun archiveById(controlUnitResourceId: Int)

fun deleteById(controlUnitResourceId: Int)

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

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

@UseCase
class ArchiveControlUnitResource(private val controlUnitResourceRepository: IControlUnitResourceRepository) {
fun execute(controlUnitResourceId: Int) {
return controlUnitResourceRepository.archiveById(controlUnitResourceId)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ data class FullControlUnitResourceDataOutput(
val baseId: Int,
val controlUnit: ControlUnitDataOutput,
val controlUnitId: Int,
val isArchived: Boolean,
val name: String,
val note: String? = null,
val photo: ByteArray? = byteArrayOf(),
Expand All @@ -27,6 +28,7 @@ data class FullControlUnitResourceDataOutput(
baseId = base.id,
controlUnit,
controlUnitId = fullControlUnitResource.controlUnitResource.controlUnitId,
isArchived = fullControlUnitResource.controlUnitResource.isArchived,
name = fullControlUnitResource.controlUnitResource.name,
note = fullControlUnitResource.controlUnitResource.note,
photo = fullControlUnitResource.controlUnitResource.photo,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package fr.gouv.cacem.monitorenv.infrastructure.api.endpoints.publicapi

import fr.gouv.cacem.monitorenv.domain.use_cases.controlUnit.CreateOrUpdateControlUnitResource
import fr.gouv.cacem.monitorenv.domain.use_cases.controlUnit.DeleteControlUnitResource
import fr.gouv.cacem.monitorenv.domain.use_cases.controlUnit.GetControlUnitResourceById
import fr.gouv.cacem.monitorenv.domain.use_cases.controlUnit.GetControlUnitResources
import fr.gouv.cacem.monitorenv.domain.use_cases.controlUnit.*
import fr.gouv.cacem.monitorenv.infrastructure.api.adapters.publicapi.inputs.CreateOrUpdateControlUnitResourceDataInput
import fr.gouv.cacem.monitorenv.infrastructure.api.adapters.publicapi.outputs.ControlUnitResourceDataOutput
import fr.gouv.cacem.monitorenv.infrastructure.api.adapters.publicapi.outputs.FullControlUnitResourceDataOutput
Expand All @@ -17,11 +14,22 @@ import org.springframework.web.bind.annotation.*
@RequestMapping("/api/v1/control_unit_resources")
@Tag(name = "Control Unit Resources")
class ApiControlUnitResourcesController(
private val archiveControlUnitResource: ArchiveControlUnitResource,
private val createOrUpdateControlUnitResource: CreateOrUpdateControlUnitResource,
private val deleteControlUnitResource: DeleteControlUnitResource,
private val getControlUnitResources: GetControlUnitResources,
private val getControlUnitResourceById: GetControlUnitResourceById,
) {
@PostMapping("/{controlUnitResourceId}/archive")
@Operation(summary = "Archive a control unit resource")
fun archive(
@PathParam("Control unit resource ID")
@PathVariable(name = "controlUnitResourceId")
controlUnitResourceId: Int,
) {
archiveControlUnitResource.execute(controlUnitResourceId)
}

@PostMapping("", consumes = ["application/json"])
@Operation(summary = "Create a control unit resource")
@ResponseStatus(HttpStatus.CREATED)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ class JpaControlUnitResourceRepository(
private val dbControlUnitResourceRepository: IDBControlUnitResourceRepository,
private val dbBaseRepository: IDBBaseRepository,
) : IControlUnitResourceRepository {
@Transactional
override fun archiveById(controlUnitResourceId: Int) {
dbControlUnitResourceRepository.archiveById(controlUnitResourceId)
}

override fun deleteById(controlUnitResourceId: Int) {
dbControlUnitResourceRepository.deleteById(controlUnitResourceId)
}
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.ControlUnitResourceModel
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 IDBControlUnitResourceRepository : CrudRepository<ControlUnitResourceModel, Int> {
@Modifying(clearAutomatically = true)
@Query(
value = """
UPDATE control_unit_resources
SET is_archived = TRUE
WHERE id = :controlUnitResourceId
""",
nativeQuery = true,
)
fun archiveById(controlUnitResourceId: Int)

@Query(
value = """
SELECT *
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class ApiControlUnitContactsControllerITests {
private lateinit var objectMapper: ObjectMapper

@Test
fun `Should create a contact`() {
fun `create() should create a contact`() {
val expectedCreatedControlUnitContact = ControlUnitContactEntity(
id = 1,
controlUnitId = 0,
Expand Down Expand Up @@ -80,7 +80,7 @@ class ApiControlUnitContactsControllerITests {
}

@Test
fun `Should get a contact by its ID`() {
fun `get() should get a contact by its ID`() {
val expectedFullControlUnitContact = FullControlUnitContactDTO(
controlUnit = ControlUnitEntity(
id = 0,
Expand Down Expand Up @@ -111,7 +111,7 @@ class ApiControlUnitContactsControllerITests {
}

@Test
fun `Should get all contacts`() {
fun `getAll() should get all contacts`() {
val expectedFullControlUnitContacts = listOf(
FullControlUnitContactDTO(
controlUnit = ControlUnitEntity(
Expand Down Expand Up @@ -162,7 +162,7 @@ class ApiControlUnitContactsControllerITests {
}

@Test
fun `Should update a contact`() {
fun `update() should update a contact`() {
val expectedUpdatedControlUnitContact = ControlUnitContactEntity(
id = 1,
controlUnitId = 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ import fr.gouv.cacem.monitorenv.domain.entities.base.BaseEntity
import fr.gouv.cacem.monitorenv.domain.entities.controlUnit.ControlUnitEntity
import fr.gouv.cacem.monitorenv.domain.entities.controlUnit.ControlUnitResourceEntity
import fr.gouv.cacem.monitorenv.domain.entities.controlUnit.ControlUnitResourceType
import fr.gouv.cacem.monitorenv.domain.use_cases.controlUnit.CreateOrUpdateControlUnitResource
import fr.gouv.cacem.monitorenv.domain.use_cases.controlUnit.DeleteControlUnitResource
import fr.gouv.cacem.monitorenv.domain.use_cases.controlUnit.GetControlUnitResourceById
import fr.gouv.cacem.monitorenv.domain.use_cases.controlUnit.GetControlUnitResources
import fr.gouv.cacem.monitorenv.domain.use_cases.controlUnit.*
import fr.gouv.cacem.monitorenv.domain.use_cases.controlUnit.dtos.FullControlUnitResourceDTO
import fr.gouv.cacem.monitorenv.infrastructure.api.adapters.publicapi.inputs.CreateOrUpdateControlUnitResourceDataInput
import org.hamcrest.Matchers
Expand All @@ -35,6 +32,9 @@ class ApiControlUnitResourcesControllerITests {
@Autowired
private lateinit var mockMvc: MockMvc

@MockBean
private lateinit var archiveControlUnitResource: ArchiveControlUnitResource

@MockBean
private lateinit var createOrUpdateControlUnitResource: CreateOrUpdateControlUnitResource

Expand All @@ -51,7 +51,19 @@ class ApiControlUnitResourcesControllerITests {
private lateinit var objectMapper: ObjectMapper

@Test
fun `Should create a resource`() {
fun `archive() should archive a control unit resource`() {
val controlUnitResourceId = 1

mockMvc.perform(
post("/api/v1/control_unit_resources/$controlUnitResourceId/archive"),
)
.andExpect(status().isOk)

BDDMockito.verify(archiveControlUnitResource).execute(controlUnitResourceId)
}

@Test
fun `create() should create a resource`() {
val expectedCreatedControlUnitResource = ControlUnitResourceEntity(
id = 1,
baseId = 0,
Expand Down Expand Up @@ -88,7 +100,19 @@ class ApiControlUnitResourcesControllerITests {
}

@Test
fun `Should get a resource by its ID`() {
fun `delete() should delete a control unit resource`() {
val controlUnitResourceId = 1

mockMvc.perform(
delete("/api/v1/control_unit_resources/$controlUnitResourceId"),
)
.andExpect(status().isOk)

BDDMockito.verify(deleteControlUnitResource).execute(controlUnitResourceId)
}

@Test
fun `get() should get a control unit resource by its ID`() {
val expectedFullControlUnitResource = FullControlUnitResourceDTO(
base = BaseEntity(
id = 0,
Expand Down Expand Up @@ -128,7 +152,7 @@ class ApiControlUnitResourcesControllerITests {
}

@Test
fun `Should get all resources`() {
fun `getAll() should get all control unit resources`() {
val expectedFullControlUnitResources = listOf(
FullControlUnitResourceDTO(
base = BaseEntity(
Expand Down Expand Up @@ -197,7 +221,7 @@ class ApiControlUnitResourcesControllerITests {
}

@Test
fun `Should update a resource`() {
fun `update() should update a control unit resource`() {
val expectedUpdatedControlUnitResource = ControlUnitResourceEntity(
id = 1,
baseId = 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ class JpaControlUnitResourceRepositoryITests : AbstractDBTests() {
@Autowired
private lateinit var jpaControlUnitResourceRepository: JpaControlUnitResourceRepository

@Test
@Transactional
fun `archiveById() should archive a control unit resource by its ID`() {
val beforeFullControlUnitResource = jpaControlUnitResourceRepository.findById(1)

assertThat(beforeFullControlUnitResource.controlUnitResource.isArchived).isFalse()

jpaControlUnitResourceRepository.archiveById(1)

val afterFullControlUnitResource = jpaControlUnitResourceRepository.findById(1)

assertThat(afterFullControlUnitResource.controlUnitResource.isArchived).isTrue()
}

@Test
@Transactional
fun `findAll() should find all resources`() {
Expand Down
1 change: 1 addition & 0 deletions frontend/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ module.exports = {
rules: {
// TODO Check why either Prettier or ESLint auto-formatting does that and why this rule is not enabled.
// 'max-len': ['warn', { code: 120 }],
'prefer-destructuring': 'off',

'@typescript-eslint/naming-convention': 'off',
'@typescript-eslint/no-unused-expressions': 'off',
Expand Down
Loading

0 comments on commit f943405

Please sign in to comment.