Ajout d'un endpoint pour récupérer tous les produits qui ont une fiche signalitique.
This commit is contained in:
parent
3033e104b2
commit
261ff046ec
|
@ -54,8 +54,12 @@ class MaterialController(materialService: MaterialService) :
|
|||
}
|
||||
}
|
||||
|
||||
@GetMapping("/simdut")
|
||||
fun getAllIdsWithSimdut(): ResponseEntity<Collection<Long>> =
|
||||
ResponseEntity.ok(service.getAllIdsWithSimdut())
|
||||
|
||||
@PostMapping(consumes = [MediaType.MULTIPART_FORM_DATA_VALUE])
|
||||
fun saveTest(@Valid entity: MaterialSaveDto, simdutFile: MultipartFile?): ResponseEntity<Material> =
|
||||
fun save(@Valid entity: MaterialSaveDto, simdutFile: MultipartFile?): ResponseEntity<Material> =
|
||||
super.save(
|
||||
materialSaveDto(
|
||||
name = entity.name,
|
||||
|
@ -65,7 +69,7 @@ class MaterialController(materialService: MaterialService) :
|
|||
)
|
||||
)
|
||||
|
||||
@PostMapping("oldsave")
|
||||
@PostMapping("unused_save")
|
||||
override fun save(entity: MaterialSaveDto): ResponseEntity<Material> =
|
||||
ResponseEntity.notFound().build()
|
||||
|
||||
|
@ -81,7 +85,7 @@ class MaterialController(materialService: MaterialService) :
|
|||
)
|
||||
)
|
||||
|
||||
@PutMapping("oldupdate")
|
||||
@PutMapping("unused_update")
|
||||
override fun update(entity: MaterialUpdateDto): ResponseEntity<Void> =
|
||||
ResponseEntity.notFound().build()
|
||||
}
|
||||
|
|
|
@ -27,6 +27,9 @@ interface MaterialService :
|
|||
/** 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>
|
||||
|
||||
/** Gets the identifier of materials for which a SIMDUT exists. */
|
||||
fun getAllIdsWithSimdut(): Collection<Long>
|
||||
|
||||
/** Updates the quantity of the given [material] with the given [factor]. */
|
||||
fun updateQuantity(material: Material, factor: Float)
|
||||
}
|
||||
|
@ -38,9 +41,7 @@ class MaterialServiceImpl(
|
|||
val recipeService: RecipeService,
|
||||
val mixService: MixService
|
||||
) :
|
||||
AbstractExternalNamedModelService<Material, MaterialSaveDto, MaterialUpdateDto, MaterialRepository>(
|
||||
materialRepository
|
||||
),
|
||||
AbstractExternalNamedModelService<Material, MaterialSaveDto, MaterialUpdateDto, MaterialRepository>(materialRepository),
|
||||
MaterialService {
|
||||
override fun existsByMaterialType(materialType: MaterialType): Boolean =
|
||||
repository.existsByMaterialType(materialType)
|
||||
|
@ -49,6 +50,11 @@ class MaterialServiceImpl(
|
|||
override fun getSimdut(id: Long): ByteArray = simdutService.read(getById(id))
|
||||
override fun getAllNotMixType(): Collection<Material> = getAll().filter { !it.isMixType }
|
||||
|
||||
override fun getAllIdsWithSimdut(): Collection<Long> =
|
||||
getAllNotMixType()
|
||||
.filter { simdutService.exists(it) }
|
||||
.map { it.id!! }
|
||||
|
||||
override fun save(entity: MaterialSaveDto): Material =
|
||||
save(entity.toMaterial()).apply {
|
||||
if (entity.simdutFile != null && !entity.simdutFile.isEmpty) simdutService.write(this, entity.simdutFile)
|
||||
|
|
|
@ -2,15 +2,12 @@ package dev.fyloz.trial.colorrecipesexplorer.repository
|
|||
|
||||
import dev.fyloz.trial.colorrecipesexplorer.model.material
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.springframework.beans.factory.annotation.Autowired
|
||||
import org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration
|
||||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest
|
||||
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager
|
||||
import org.springframework.test.context.junit4.SpringRunner
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
@RunWith(SpringRunner::class)
|
||||
@DataJpaTest(excludeAutoConfiguration = [LiquibaseAutoConfiguration::class])
|
||||
class MaterialRepositoryTest @Autowired constructor(
|
||||
private val materialRepository: MaterialRepository,
|
||||
|
|
|
@ -93,6 +93,33 @@ class MaterialServiceTest :
|
|||
assertFalse(found.contains(mixTypeMaterial))
|
||||
}
|
||||
|
||||
// getAllIdsWithSimdut()
|
||||
|
||||
@Test
|
||||
fun `getAllIdsWithSimdut() returns a list containing the identifier of every material with a SIMDUT file`() {
|
||||
val materials = listOf(
|
||||
material(id = 0L),
|
||||
material(id = 1L),
|
||||
material(id = 2L),
|
||||
material(id = 3L)
|
||||
)
|
||||
val hasSimdut = mapOf(
|
||||
*materials
|
||||
.map { it.id!! to it.evenId }
|
||||
.toTypedArray()
|
||||
)
|
||||
val expectedIds = hasSimdut
|
||||
.filter { it.value }
|
||||
.map { it.key }
|
||||
|
||||
whenever(simdutService.exists(any())).doAnswer { hasSimdut[(it.arguments[0] as Material).id] }
|
||||
doReturn(materials).whenever(service).getAllNotMixType()
|
||||
|
||||
val found = service.getAllIdsWithSimdut()
|
||||
|
||||
assertEquals(expectedIds, found)
|
||||
}
|
||||
|
||||
// save()
|
||||
|
||||
@Test
|
||||
|
@ -165,9 +192,9 @@ class MaterialServiceTest :
|
|||
|
||||
val found = service.getAllForMixCreation(recipe.id!!)
|
||||
|
||||
assertTrue(found contains normalMaterial)
|
||||
assertTrue(found contains mixTypeMaterial)
|
||||
assertFalse(found contains anotherMixTypeMaterial)
|
||||
assertTrue(normalMaterial in found)
|
||||
assertTrue(mixTypeMaterial in found)
|
||||
assertFalse(anotherMixTypeMaterial in found)
|
||||
}
|
||||
|
||||
// getAllForMixUpdate()
|
||||
|
@ -187,9 +214,9 @@ class MaterialServiceTest :
|
|||
|
||||
val found = service.getAllForMixUpdate(mix.id!!)
|
||||
|
||||
assertTrue(found contains normalMaterial)
|
||||
assertTrue(found contains mixTypeMaterial)
|
||||
assertFalse(found contains anotherMixTypeMaterial)
|
||||
assertTrue(normalMaterial in found)
|
||||
assertTrue(mixTypeMaterial in found)
|
||||
assertFalse(anotherMixTypeMaterial in found)
|
||||
}
|
||||
|
||||
// update()
|
||||
|
@ -208,8 +235,6 @@ class MaterialServiceTest :
|
|||
verify(simdutService).update(eq(mockSimdutFile), any())
|
||||
}
|
||||
|
||||
// updateQuantity()
|
||||
|
||||
|
||||
// delete()
|
||||
|
||||
|
@ -217,7 +242,7 @@ class MaterialServiceTest :
|
|||
super.`deleteById() deletes the entity with the given id in the repository`()
|
||||
}
|
||||
|
||||
/** 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 }
|
||||
/** Utility property to check if the identifier of the given [Material] is even. */
|
||||
val Material.evenId: Boolean
|
||||
get() = this.id!! % 2 == 0L
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue