diff --git a/src/main/kotlin/dev/fyloz/colorrecipesexplorer/service/RecipeService.kt b/src/main/kotlin/dev/fyloz/colorrecipesexplorer/service/RecipeService.kt index 1173456..3e357ef 100644 --- a/src/main/kotlin/dev/fyloz/colorrecipesexplorer/service/RecipeService.kt +++ b/src/main/kotlin/dev/fyloz/colorrecipesexplorer/service/RecipeService.kt @@ -5,6 +5,7 @@ import dev.fyloz.colorrecipesexplorer.model.* import dev.fyloz.colorrecipesexplorer.model.validation.or import dev.fyloz.colorrecipesexplorer.repository.RecipeRepository import dev.fyloz.colorrecipesexplorer.service.files.FileService +import dev.fyloz.colorrecipesexplorer.service.utils.setAll import org.springframework.context.annotation.Lazy import org.springframework.stereotype.Service import org.springframework.web.multipart.MultipartFile @@ -73,22 +74,22 @@ class RecipeServiceImpl( remark = remark or persistedRecipe.remark, company = persistedRecipe.company, mixes = persistedRecipe.mixes, - groupsInformation = updateGroupsInformationSteps(persistedRecipe, entity.steps) + groupsInformation = updateGroupsInformation(persistedRecipe, entity) ) }) } - private fun updateGroupsInformationSteps(recipe: Recipe, steps: Set?): Set { - if (steps == null) return recipe.groupsInformation + private fun updateGroupsInformation(recipe: Recipe, updateDto: RecipeUpdateDto): Set { + val steps = updateDto.steps ?: return recipe.groupsInformation val updatedGroupsInformation = mutableSetOf() steps.forEach { with(recipe.groupInformationForGroup(it.groupId)) { updatedGroupsInformation.add( + // Set steps for the existing RecipeGroupInformation or create a new one this?.apply { if (this.steps != null) { - this.steps!!.clear() - this.steps!!.addAll(it.steps) + this.steps!!.setAll(it.steps) } else { this.steps = it.steps.toMutableSet() } diff --git a/src/main/kotlin/dev/fyloz/colorrecipesexplorer/service/RecipeStepService.kt b/src/main/kotlin/dev/fyloz/colorrecipesexplorer/service/RecipeStepService.kt index 1128929..b356cc0 100644 --- a/src/main/kotlin/dev/fyloz/colorrecipesexplorer/service/RecipeStepService.kt +++ b/src/main/kotlin/dev/fyloz/colorrecipesexplorer/service/RecipeStepService.kt @@ -4,9 +4,27 @@ import dev.fyloz.colorrecipesexplorer.model.RecipeStep import dev.fyloz.colorrecipesexplorer.repository.RecipeStepRepository import org.springframework.stereotype.Service -interface RecipeStepService : ModelService +interface RecipeStepService : ModelService { + + /** + * Validates if the given [steps] obey the following criteria: + * + * * The position of the steps is greater or equals to 1 + * * Each position is unique in the collection + * * There is no gap between positions + */ + fun validateStepsCollection(steps: Collection): Boolean +} @Service class RecipeStepServiceImpl(recipeStepRepository: RecipeStepRepository) : AbstractModelService(recipeStepRepository), - RecipeStepService + RecipeStepService { +// override fun validateStepsCollection(steps: Collection): Boolean { +// val sortedSteps = steps.sortedBy { it.position } +// +// fun validateStepPosition(step: RecipeStep) = +// step.position >= 1 +// +// } +} diff --git a/src/main/kotlin/dev/fyloz/colorrecipesexplorer/service/utils/Collections.kt b/src/main/kotlin/dev/fyloz/colorrecipesexplorer/service/utils/Collections.kt index b28e138..abdf788 100644 --- a/src/main/kotlin/dev/fyloz/colorrecipesexplorer/service/utils/Collections.kt +++ b/src/main/kotlin/dev/fyloz/colorrecipesexplorer/service/utils/Collections.kt @@ -16,3 +16,9 @@ inline fun Iterable.mapMayThrow( } } } + +/** Clears and fills the given [MutableCollection] with the given [elements]. */ +fun MutableCollection.setAll(elements: Collection) { + this.clear() + this.addAll(elements) +}