Mise à jour de RecipeSaveDto pour permettre la création d'une recette depuis l'API REST.

This commit is contained in:
FyloZ 2021-01-18 23:13:52 -05:00
parent 59c5369030
commit f80064811a
4 changed files with 35 additions and 13 deletions

View File

@ -6,6 +6,7 @@ import dev.fyloz.trial.colorrecipesexplorer.model.validation.NullOrSize
import java.time.LocalDate
import java.util.*
import javax.persistence.*
import javax.validation.constraints.Min
import javax.validation.constraints.NotBlank
import javax.validation.constraints.NotNull
import javax.validation.constraints.Size
@ -83,17 +84,15 @@ open class RecipeSaveDto(
val description: String,
@field:NotNull(message = RECIPE_SAMPLE_NULL_MESSAGE)
@field:Size(min = 0, message = RECIPE_SAMPLE_TOO_SMALL_MESSAGE)
@field:Min(value = 0, message = RECIPE_SAMPLE_TOO_SMALL_MESSAGE)
val sample: Int,
val approbationDate: LocalDate,
val remark: String,
@field:NotNull(message = RECIPE_COMPANY_NULL_MESSAGE)
val company: Company,
// TODO when frontend while be done -> add mixes ?
// TODO when frontend while be done -> add steps ?
@field:Min(value = 0, message = RECIPE_COMPANY_NULL_MESSAGE)
val companyId: Long = -1L,
) : EntityDto<Recipe> {
override fun toEntity(): Recipe = recipe(
name = name,
@ -101,7 +100,7 @@ open class RecipeSaveDto(
sample = sample,
approbationDate = approbationDate,
remark = remark,
company = company
company = company(id = companyId)
)
}
@ -162,9 +161,9 @@ fun recipeSaveDto(
sample: Int = -1,
approbationDate: LocalDate = LocalDate.MIN,
remark: String = "remark",
company: Company = company(),
companyId: Long = 0L,
op: RecipeSaveDto.() -> Unit = {}
) = RecipeSaveDto(name, description, sample, approbationDate, remark, company).apply(op)
) = RecipeSaveDto(name, description, sample, approbationDate, remark, companyId).apply(op)
fun recipeUpdateDto(
id: Long = 0L,

View File

@ -15,12 +15,25 @@ interface RecipeService : ExternalModelService<Recipe, RecipeSaveDto, RecipeUpda
}
@Service
class RecipeServiceImpl(recipeRepository: RecipeRepository) :
class RecipeServiceImpl(recipeRepository: RecipeRepository, val companyService: CompanyService) :
AbstractExternalModelService<Recipe, RecipeSaveDto, RecipeUpdateDto, RecipeRepository>(recipeRepository),
RecipeService {
override fun existsByCompany(company: Company): Boolean = repository.existsByCompany(company)
override fun getAllByCompany(company: Company): Collection<Recipe> = repository.findAllByCompany(company)
override fun save(entity: RecipeSaveDto): Recipe {
return save(with(entity) {
recipe(
name = name,
description = description,
sample = sample,
approbationDate = approbationDate,
remark = remark,
company = companyService.getById(companyId)
)
})
}
@ExperimentalContracts
override fun update(entity: RecipeUpdateDto): Recipe {
val persistedRecipe by lazy { getById(entity.id) }

View File

@ -182,7 +182,7 @@ class EmployeeGroupServiceTest : AbstractExternalModelServiceTest<EmployeeGroup,
@BeforeEach
override fun afterEach() {
reset(employeeService, entitySaveDto, entityUpdateDto)
reset(employeeService)
super.afterEach()
}

View File

@ -15,12 +15,13 @@ import kotlin.test.assertTrue
class RecipeServiceTest :
AbstractExternalModelServiceTest<Recipe, RecipeSaveDto, RecipeUpdateDto, RecipeService, RecipeRepository>() {
override val repository: RecipeRepository = mock()
override val service: RecipeService = spy(RecipeServiceImpl(repository))
private val companyService: CompanyService = mock()
override val service: RecipeService = spy(RecipeServiceImpl(repository, companyService))
private val company: Company = company()
private val company: Company = company(id = 0L)
override val entity: Recipe = recipe(id = 0L, name = "recipe", company = company)
override val anotherEntity: Recipe = recipe(id = 1L, name = "another recipe", company = company)
override val entitySaveDto: RecipeSaveDto = spy(recipeSaveDto(name = entity.name, company = entity.company))
override val entitySaveDto: RecipeSaveDto = spy(recipeSaveDto(name = entity.name, companyId = entity.company.id!!))
override val entityUpdateDto: RecipeUpdateDto = spy(recipeUpdateDto(id = entity.id!!, name = entity.name, company = entity.company))
@Nested
@ -56,4 +57,13 @@ class RecipeServiceTest :
assertEquals(companies, found)
}
}
@Nested
inner class SaveDto {
@Test
fun `calls and returns save() with the created entity`() {
whenever(companyService.getById(company.id!!)).doReturn(company)
saveDtoTest(entity, entitySaveDto, service)
}
}
}