From c81f0468041f0fe1b68134e9ea699b8e0f24b2d7 Mon Sep 17 00:00:00 2001 From: FyloZ Date: Wed, 25 Nov 2020 13:34:30 -0500 Subject: [PATCH] Ajout du support pour fiche signalitiques dans l'API REST. --- .../colorrecipesexplorer/model/Material.kt | 9 ++-- .../rest/MaterialController.kt | 45 ++++++++++++++----- .../service/MaterialService.kt | 12 +++-- .../service/MaterialServiceTest.kt | 23 ++++++++++ 4 files changed, 71 insertions(+), 18 deletions(-) diff --git a/src/main/kotlin/dev/fyloz/trial/colorrecipesexplorer/model/Material.kt b/src/main/kotlin/dev/fyloz/trial/colorrecipesexplorer/model/Material.kt index 42b3185..c2fb8e3 100644 --- a/src/main/kotlin/dev/fyloz/trial/colorrecipesexplorer/model/Material.kt +++ b/src/main/kotlin/dev/fyloz/trial/colorrecipesexplorer/model/Material.kt @@ -105,7 +105,8 @@ fun material( material: Material, id: Long? = null, name: String? = null, -) = Material(id ?: material.id, name ?: material.name, material.inventoryQuantity, material.isMixType, material.materialType) +) = Material(id ?: material.id, name + ?: material.name, material.inventoryQuantity, material.isMixType, material.materialType) fun materialSaveDto( name: String = "name", @@ -117,9 +118,9 @@ fun materialSaveDto( fun materialUpdateDto( id: Long = 0L, - name: String = "name", - inventoryQuantity: Float = 0f, - materialType: MaterialType = materialType(), + name: String? = "name", + inventoryQuantity: Float? = 0f, + materialType: MaterialType? = materialType(), simdutFile: MultipartFile? = null, op: MaterialUpdateDto.() -> Unit = {} ) = MaterialUpdateDto(id, name, inventoryQuantity, materialType, simdutFile).apply(op) 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 4657207..e105eaf 100644 --- a/src/main/kotlin/dev/fyloz/trial/colorrecipesexplorer/rest/MaterialController.kt +++ b/src/main/kotlin/dev/fyloz/trial/colorrecipesexplorer/rest/MaterialController.kt @@ -1,16 +1,14 @@ package dev.fyloz.trial.colorrecipesexplorer.rest -import dev.fyloz.trial.colorrecipesexplorer.model.Material -import dev.fyloz.trial.colorrecipesexplorer.model.MaterialSaveDto -import dev.fyloz.trial.colorrecipesexplorer.model.MaterialUpdateDto +import dev.fyloz.trial.colorrecipesexplorer.model.* import dev.fyloz.trial.colorrecipesexplorer.service.MaterialService import org.springframework.context.annotation.Profile +import org.springframework.http.HttpHeaders +import org.springframework.http.HttpStatus import org.springframework.http.MediaType import org.springframework.http.ResponseEntity -import org.springframework.web.bind.annotation.PostMapping -import org.springframework.web.bind.annotation.PutMapping -import org.springframework.web.bind.annotation.RequestMapping -import org.springframework.web.bind.annotation.RestController +import org.springframework.web.bind.annotation.* +import org.springframework.web.multipart.MultipartFile private const val MATERIAL_CONTROLLER_PATH = "api/material" @@ -18,11 +16,36 @@ private const val MATERIAL_CONTROLLER_PATH = "api/material" @RequestMapping(MATERIAL_CONTROLLER_PATH) @Profile("rest") class MaterialController(materialService: MaterialService) : AbstractRestModelApiController(materialService, MATERIAL_CONTROLLER_PATH) { + @GetMapping("{id}/simdut/exists") + @ResponseStatus(HttpStatus.OK) + fun hasSimdut(@PathVariable id: Long): ResponseEntity = + ResponseEntity.ok(service.hasSimdut(id)) + + @GetMapping("{id}/simdut", produces = [MediaType.APPLICATION_PDF_VALUE]) + @ResponseStatus(HttpStatus.OK) + fun getSimdut(@PathVariable id: Long): ResponseEntity { + val simdutFile = service.getSimdut(id) + return if (simdutFile.isEmpty()) { + ResponseEntity.notFound().build() + } else { + val headers = HttpHeaders().apply { contentType = MediaType.APPLICATION_PDF } + ResponseEntity(simdutFile, headers, HttpStatus.OK) + } + } + @PostMapping(consumes = [MediaType.MULTIPART_FORM_DATA_VALUE]) - override fun save(entity: MaterialSaveDto): ResponseEntity = super.save(entity) + fun save(entity: MaterialSaveDto, simdutFile: MultipartFile): ResponseEntity = + 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() @PutMapping(consumes = [MediaType.MULTIPART_FORM_DATA_VALUE]) - override fun update(entity: MaterialUpdateDto): ResponseEntity { - return super.update(entity) - } + fun update(entity: MaterialUpdateDto, simdutFile: MultipartFile): ResponseEntity = + super.update(materialUpdateDto(name = entity.name, inventoryQuantity = entity.inventoryQuantity, materialType = entity.materialType, simdutFile = simdutFile)) + + @PutMapping("oldupdate") + override fun update(entity: MaterialUpdateDto): ResponseEntity = + super.update(entity) } 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 af04e2c..395a918 100644 --- a/src/main/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/MaterialService.kt +++ b/src/main/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/MaterialService.kt @@ -1,13 +1,11 @@ package dev.fyloz.trial.colorrecipesexplorer.service -import dev.fyloz.trial.colorrecipesexplorer.repository.MaterialRepository import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityAlreadyExistsRestException -import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityNotFoundRestException import dev.fyloz.trial.colorrecipesexplorer.model.* +import dev.fyloz.trial.colorrecipesexplorer.repository.MaterialRepository import dev.fyloz.trial.colorrecipesexplorer.service.files.SimdutService import dev.fyloz.trial.colorrecipesexplorer.service.model.MixQuantityService import io.jsonwebtoken.lang.Assert -import org.springframework.context.annotation.Profile import org.springframework.stereotype.Service interface MaterialService : NamedModelService { @@ -17,6 +15,12 @@ interface MaterialService : NamedModelService } @@ -27,6 +31,8 @@ class MaterialServiceImpl(materialRepository: MaterialRepository, val mixQuantit MaterialService { override fun existsByMaterialType(materialType: MaterialType): Boolean = repository.existsByMaterialType(materialType) override fun isLinkedToMixes(material: Material): Boolean = mixQuantityService.existsByMaterial(material) + override fun hasSimdut(id: Long): Boolean = simdutService.exists(getById(id)) + override fun getSimdut(id: Long): ByteArray = simdutService.read(getById(id)) override fun getAllNotMixType(): Collection = getAll().filter { !it.isMixType } override fun save(entity: MaterialSaveDto): Material = 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 4ba889d..de58191 100644 --- a/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/MaterialServiceTest.kt +++ b/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/MaterialServiceTest.kt @@ -77,6 +77,29 @@ class MaterialServiceTest : AbstractNamedModelServiceTest