diff --git a/src/main/kotlin/dev/fyloz/trial/colorrecipesexplorer/model/Recipe.kt b/src/main/kotlin/dev/fyloz/trial/colorrecipesexplorer/model/Recipe.kt index 714f745..7b0354c 100644 --- a/src/main/kotlin/dev/fyloz/trial/colorrecipesexplorer/model/Recipe.kt +++ b/src/main/kotlin/dev/fyloz/trial/colorrecipesexplorer/model/Recipe.kt @@ -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 { 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, diff --git a/src/main/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/RecipeService.kt b/src/main/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/RecipeService.kt index 1cb5b92..cf49079 100644 --- a/src/main/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/RecipeService.kt +++ b/src/main/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/RecipeService.kt @@ -15,12 +15,25 @@ interface RecipeService : ExternalModelService(recipeRepository), RecipeService { override fun existsByCompany(company: Company): Boolean = repository.existsByCompany(company) override fun getAllByCompany(company: Company): Collection = 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) } diff --git a/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/AccountsServiceTest.kt b/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/AccountsServiceTest.kt index 62c0fc5..70c60f8 100644 --- a/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/AccountsServiceTest.kt +++ b/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/AccountsServiceTest.kt @@ -182,7 +182,7 @@ class EmployeeGroupServiceTest : AbstractExternalModelServiceTest() { 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) + } + } }