Ajout d'endpoints pour récupérer les produits pour la création et la mise à jour de mélanges.

This commit is contained in:
FyloZ 2021-01-28 23:53:12 -05:00
parent ca5d489cc4
commit 41110237f5
3 changed files with 114 additions and 10 deletions

View File

@ -16,11 +16,15 @@ private const val MATERIAL_CONTROLLER_PATH = "api/material"
@RestController
@RequestMapping(MATERIAL_CONTROLLER_PATH)
@Profile("rest")
class MaterialController(materialService: MaterialService) : AbstractModelRestApiController<Material, MaterialSaveDto, MaterialUpdateDto, MaterialService>(materialService, MATERIAL_CONTROLLER_PATH) {
class MaterialController(materialService: MaterialService) :
AbstractModelRestApiController<Material, MaterialSaveDto, MaterialUpdateDto, MaterialService>(
materialService,
MATERIAL_CONTROLLER_PATH
) {
@GetMapping("{id}/simdut/exists")
@ResponseStatus(HttpStatus.OK)
fun hasSimdut(@PathVariable id: Long): ResponseEntity<Boolean> =
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<Collection<Material>> =
ResponseEntity.ok(service.getAllForMixCreation(recipeId))
@GetMapping("mix/update/{mixId}")
@ResponseStatus(HttpStatus.OK)
fun getAllForMixUpdate(@PathVariable mixId: Long): ResponseEntity<Collection<Material>> =
ResponseEntity.ok(service.getAllForMixUpdate(mixId))
@PostMapping(consumes = [MediaType.MULTIPART_FORM_DATA_VALUE])
fun saveTest(@Valid entity: MaterialSaveDto, simdutFile: MultipartFile?): ResponseEntity<Material> =
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<Material> =
ResponseEntity.notFound().build()
ResponseEntity.notFound().build()
@PutMapping(consumes = [MediaType.MULTIPART_FORM_DATA_VALUE])
fun update(@Valid entity: MaterialUpdateDto, simdutFile: MultipartFile?): ResponseEntity<Void> =
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<Void> =
ResponseEntity.notFound().build()
ResponseEntity.notFound().build()
}

View File

@ -19,12 +19,20 @@ interface MaterialService :
/** Gets all materials that are not a mix type. */
fun getAllNotMixType(): Collection<Material>
/** 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<Material>
/** 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<Material>
}
@Service
class MaterialServiceImpl(
materialRepository: MaterialRepository,
val simdutService: SimdutService
val simdutService: SimdutService,
val recipeService: RecipeService,
val mixService: MixService
) :
AbstractExternalNamedModelService<Material, MaterialSaveDto, MaterialUpdateDto, MaterialRepository>(
materialRepository
@ -61,6 +69,20 @@ class MaterialServiceImpl(
}
}
override fun getAllForMixCreation(recipeId: Long): Collection<Material> {
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<Material> {
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")
}

View File

@ -14,10 +14,14 @@ import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue
class MaterialServiceTest : AbstractExternalNamedModelServiceTest<Material, MaterialSaveDto, MaterialUpdateDto, MaterialService, MaterialRepository>() {
private val simdutService: SimdutService = mock()
class MaterialServiceTest :
AbstractExternalNamedModelServiceTest<Material, MaterialSaveDto, MaterialUpdateDto, MaterialService, MaterialRepository>() {
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<Material, Mate
}
}
/** Helper function to replace collections.in because the id is not considered in the equals function of Material while Thymeleaf is supported. */
private infix fun Collection<Material>.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