Ajout du support pour fiche signalitiques dans l'API REST.

This commit is contained in:
FyloZ 2020-11-25 13:34:30 -05:00
parent dd36390a08
commit c81f046804
4 changed files with 71 additions and 18 deletions

View File

@ -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)

View File

@ -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<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))
@GetMapping("{id}/simdut", produces = [MediaType.APPLICATION_PDF_VALUE])
@ResponseStatus(HttpStatus.OK)
fun getSimdut(@PathVariable id: Long): ResponseEntity<ByteArray> {
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<Material> = super.save(entity)
fun save(entity: MaterialSaveDto, simdutFile: MultipartFile): ResponseEntity<Material> =
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()
@PutMapping(consumes = [MediaType.MULTIPART_FORM_DATA_VALUE])
override fun update(entity: MaterialUpdateDto): ResponseEntity<Void> {
return super.update(entity)
}
fun update(entity: MaterialUpdateDto, simdutFile: MultipartFile): ResponseEntity<Void> =
super.update(materialUpdateDto(name = entity.name, inventoryQuantity = entity.inventoryQuantity, materialType = entity.materialType, simdutFile = simdutFile))
@PutMapping("oldupdate")
override fun update(entity: MaterialUpdateDto): ResponseEntity<Void> =
super.update(entity)
}

View File

@ -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<Material, MaterialSaveDto, MaterialUpdateDto, MaterialRepository> {
@ -17,6 +15,12 @@ interface MaterialService : NamedModelService<Material, MaterialSaveDto, Materia
/** Checks if the given [material] is used by at least one mix. */
fun isLinkedToMixes(material: Material): Boolean
/** Checks if the material with the given [id] has a SIMDUT file. */
fun hasSimdut(id: Long): Boolean
/** Gets the SIMDUT file of the material with the given [id]. */
fun getSimdut(id: Long): ByteArray
/** Gets all materials that are not a mix type. */
fun getAllNotMixType(): Collection<Material>
}
@ -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<Material> = getAll().filter { !it.isMixType }
override fun save(entity: MaterialSaveDto): Material =

View File

@ -77,6 +77,29 @@ class MaterialServiceTest : AbstractNamedModelServiceTest<Material, MaterialSave
}
}
@Nested
inner class HasSimdut {
@Test
fun `returns false when simdutService_exists() returns false`() {
whenever(simdutService.exists(entity)).doReturn(false)
doReturn(entity).whenever(service).getById(entity.id!!)
val found = service.hasSimdut(entity.id!!)
assertFalse(found)
}
@Test
fun `returns true when simdutService_exists() returns true`() {
whenever(simdutService.exists(entity)).doReturn(true)
doReturn(entity).whenever(service).getById(entity.id!!)
val found = service.hasSimdut(entity.id!!)
assertTrue(found)
}
}
@Nested
inner class GetAllNotMixType {
@Test