From 41110237f5fde4d3b5d20c616812d1469123c928 Mon Sep 17 00:00:00 2001 From: FyloZ Date: Thu, 28 Jan 2021 23:53:12 -0500 Subject: [PATCH] =?UTF-8?q?Ajout=20d'endpoints=20pour=20r=C3=A9cup=C3=A9re?= =?UTF-8?q?r=20les=20produits=20pour=20la=20cr=C3=A9ation=20et=20la=20mise?= =?UTF-8?q?=20=C3=A0=20jour=20de=20m=C3=A9langes.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../rest/MaterialController.kt | 41 +++++++++++-- .../service/MaterialService.kt | 24 +++++++- .../service/MaterialServiceTest.kt | 59 ++++++++++++++++++- 3 files changed, 114 insertions(+), 10 deletions(-) diff --git a/src/main/kotlin/dev/fyloz/trial/colorrecipesexplorer/rest/MaterialController.kt b/src/main/kotlin/dev/fyloz/trial/colorrecipesexplorer/rest/MaterialController.kt index 8bebbc7..8a29191 100644 --- a/src/main/kotlin/dev/fyloz/trial/colorrecipesexplorer/rest/MaterialController.kt +++ b/src/main/kotlin/dev/fyloz/trial/colorrecipesexplorer/rest/MaterialController.kt @@ -16,11 +16,15 @@ private const val MATERIAL_CONTROLLER_PATH = "api/material" @RestController @RequestMapping(MATERIAL_CONTROLLER_PATH) @Profile("rest") -class MaterialController(materialService: MaterialService) : AbstractModelRestApiController(materialService, MATERIAL_CONTROLLER_PATH) { +class MaterialController(materialService: MaterialService) : + AbstractModelRestApiController( + materialService, + MATERIAL_CONTROLLER_PATH + ) { @GetMapping("{id}/simdut/exists") @ResponseStatus(HttpStatus.OK) fun hasSimdut(@PathVariable id: Long): ResponseEntity = - ResponseEntity.ok(service.hasSimdut(id)) + ResponseEntity.ok(service.hasSimdut(id)) @GetMapping("{id}/simdut", produces = [MediaType.APPLICATION_PDF_VALUE]) @ResponseStatus(HttpStatus.OK) @@ -34,19 +38,44 @@ class MaterialController(materialService: MaterialService) : AbstractModelRestAp } } + @GetMapping("mix/create/{recipeId}") + @ResponseStatus(HttpStatus.OK) + fun getAllForMixCreation(@PathVariable recipeId: Long): ResponseEntity> = + ResponseEntity.ok(service.getAllForMixCreation(recipeId)) + + @GetMapping("mix/update/{mixId}") + @ResponseStatus(HttpStatus.OK) + fun getAllForMixUpdate(@PathVariable mixId: Long): ResponseEntity> = + ResponseEntity.ok(service.getAllForMixUpdate(mixId)) + @PostMapping(consumes = [MediaType.MULTIPART_FORM_DATA_VALUE]) fun saveTest(@Valid entity: MaterialSaveDto, simdutFile: MultipartFile?): ResponseEntity = - super.save(materialSaveDto(name = entity.name, inventoryQuantity = entity.inventoryQuantity, materialType = entity.materialType, simdutFile = simdutFile)) + super.save( + materialSaveDto( + name = entity.name, + inventoryQuantity = entity.inventoryQuantity, + materialType = entity.materialType, + simdutFile = simdutFile + ) + ) @PostMapping("oldsave") override fun save(entity: MaterialSaveDto): ResponseEntity = - ResponseEntity.notFound().build() + ResponseEntity.notFound().build() @PutMapping(consumes = [MediaType.MULTIPART_FORM_DATA_VALUE]) fun update(@Valid entity: MaterialUpdateDto, simdutFile: MultipartFile?): ResponseEntity = - super.update(materialUpdateDto(id = entity.id, name = entity.name, inventoryQuantity = entity.inventoryQuantity, materialType = entity.materialType, simdutFile = simdutFile)) + super.update( + materialUpdateDto( + id = entity.id, + name = entity.name, + inventoryQuantity = entity.inventoryQuantity, + materialType = entity.materialType, + simdutFile = simdutFile + ) + ) @PutMapping("oldupdate") override fun update(entity: MaterialUpdateDto): ResponseEntity = - ResponseEntity.notFound().build() + ResponseEntity.notFound().build() } diff --git a/src/main/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/MaterialService.kt b/src/main/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/MaterialService.kt index cf653d2..8f97ccd 100644 --- a/src/main/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/MaterialService.kt +++ b/src/main/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/MaterialService.kt @@ -19,12 +19,20 @@ interface MaterialService : /** Gets all materials that are not a mix type. */ fun getAllNotMixType(): Collection + + /** Gets all materials available for the creation of a mix for the recipe with the given [recipeId], including normal materials and materials from [MixType]s included in the said recipe. */ + fun getAllForMixCreation(recipeId: Long): Collection + + /** Gets all materials available for updating the mix with the given [mixId], including normal materials and materials from [MixType]s included in the mix recipe, excluding the material of the [MixType] of the said mix. */ + fun getAllForMixUpdate(mixId: Long): Collection } @Service class MaterialServiceImpl( materialRepository: MaterialRepository, - val simdutService: SimdutService + val simdutService: SimdutService, + val recipeService: RecipeService, + val mixService: MixService ) : AbstractExternalNamedModelService( materialRepository @@ -61,6 +69,20 @@ class MaterialServiceImpl( } } + override fun getAllForMixCreation(recipeId: Long): Collection { + val recipesMixTypes = recipeService.getById(recipeId).mixTypes + return getAll() + .filter { !it.isMixType || recipesMixTypes.any { mixType -> mixType.material.id == it.id } } + } + + override fun getAllForMixUpdate(mixId: Long): Collection { + val mix = mixService.getById(mixId) + val recipesMixTypes = mix.recipe.mixTypes + return getAll() + .filter { !it.isMixType || recipesMixTypes.any { mixType -> mixType.material.id == it.id } } + .filter { it.id != mix.mixType.material.id } + } + private fun assertMaterialType(materialType: MaterialType?) { Assert.notNull(materialType, "A persisted material has a null material type") } diff --git a/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/MaterialServiceTest.kt b/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/MaterialServiceTest.kt index 882e357..af13ff6 100644 --- a/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/MaterialServiceTest.kt +++ b/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/MaterialServiceTest.kt @@ -14,10 +14,14 @@ import kotlin.test.assertEquals import kotlin.test.assertFalse import kotlin.test.assertTrue -class MaterialServiceTest : AbstractExternalNamedModelServiceTest() { - private val simdutService: SimdutService = mock() +class MaterialServiceTest : + AbstractExternalNamedModelServiceTest() { override val repository: MaterialRepository = mock() - override val service: MaterialService = spy(MaterialServiceImpl(repository, simdutService)) + private val simdutService: SimdutService = mock() + private val recipeService: RecipeService = mock() + private val mixService: MixService = mock() + override val service: MaterialService = + spy(MaterialServiceImpl(repository, simdutService, recipeService, mixService)) override val entity: Material = material(id = 0L, name = "material") override val anotherEntity: Material = material(id = 1L, name = "another material") @@ -136,6 +140,55 @@ class MaterialServiceTest : AbstractExternalNamedModelServiceTest.contains(material: Material): Boolean = + any { it.id == material.id } + + @Nested + inner class GetAllForMixCreation { + @Test + fun `returns all normal materials and all mix type materials for the given recipe`() { + val normalMaterial = material(id = 0L, isMixType = false) + val mixTypeMaterial = material(id = 1L, isMixType = true) + val anotherMixTypeMaterial = material(id = 2L, isMixType = true) + val materials = listOf(normalMaterial, mixTypeMaterial, anotherMixTypeMaterial) + val recipe = + recipe(id = 0L, mixes = mutableListOf(mix(mixType = mixType(id = 0L, material = mixTypeMaterial)))) + + whenever(recipeService.getById(recipe.id!!)).doReturn(recipe) + doReturn(materials).whenever(service).getAll() + + val found = service.getAllForMixCreation(recipe.id!!) + + assertTrue(found contains normalMaterial) + assertTrue(found contains mixTypeMaterial) + assertFalse(found contains anotherMixTypeMaterial) + } + } + + @Nested + inner class GetAllForMixUpdate { + @Test + fun `returns all normal materials and all mix type materials for the recipe of the given mix without the mix type of the said mix`() { + val normalMaterial = material(id = 0L, isMixType = false) + val mixTypeMaterial = material(id = 1L, isMixType = true) + val anotherMixTypeMaterial = material(id = 2L, isMixType = true) + val materials = listOf(normalMaterial, mixTypeMaterial, anotherMixTypeMaterial) + val recipe = recipe(id = 0L, mixes = mutableListOf(mix(mixType = mixType(material = mixTypeMaterial)))) + val mix = mix(id = 1L, recipe = recipe, mixType = mixType(material = anotherMixTypeMaterial)) + recipe.mixes.add(mix) + + whenever(mixService.getById(mix.id!!)).doReturn(mix) + doReturn(materials).whenever(service).getAll() + + val found = service.getAllForMixUpdate(mix.id!!) + + assertTrue(found contains normalMaterial) + assertTrue(found contains mixTypeMaterial) + assertFalse(found contains anotherMixTypeMaterial) + } + } + // @Nested // inner class UpdateDto { // @Test