diff --git a/src/main/kotlin/dev/fyloz/trial/colorrecipesexplorer/repository/MixTypeRepository.kt b/src/main/kotlin/dev/fyloz/trial/colorrecipesexplorer/repository/MixTypeRepository.kt index 87430d6..0838acf 100644 --- a/src/main/kotlin/dev/fyloz/trial/colorrecipesexplorer/repository/MixTypeRepository.kt +++ b/src/main/kotlin/dev/fyloz/trial/colorrecipesexplorer/repository/MixTypeRepository.kt @@ -1,15 +1,23 @@ package dev.fyloz.trial.colorrecipesexplorer.repository import dev.fyloz.trial.colorrecipesexplorer.model.Material +import dev.fyloz.trial.colorrecipesexplorer.model.MaterialType import dev.fyloz.trial.colorrecipesexplorer.model.MixType import org.springframework.data.jpa.repository.Query import org.springframework.stereotype.Repository @Repository interface MixTypeRepository : NamedJpaRepository { + @Query("select case when(count(m) > 0) then true else false end from MixType m where m.name = :name and m.material.materialType = :materialType") + fun existsByNameAndMaterialType(name: String, materialType: MaterialType): Boolean + /** Gets the mix type with the given [material]. */ fun findByMaterial(material: Material): MixType? + /** Gets the [MixType] with the given [name] and [materialType]. */ + @Query("select m from MixType m where m.name = :name and m.material.materialType = :materialType") + fun findByNameAndMaterialType(name: String, materialType: MaterialType): MixType? + @Query( """ select case when(count(m.id) > 0) then false else true end diff --git a/src/main/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/MixService.kt b/src/main/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/MixService.kt index 390674a..4745815 100644 --- a/src/main/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/MixService.kt +++ b/src/main/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/MixService.kt @@ -34,7 +34,7 @@ class MixServiceImpl( override fun save(entity: MixSaveDto): Mix { val recipe = recipeService.getById(entity.recipeId) val materialType = materialTypeService.getById(entity.materialTypeId) - val mixType = mixTypeService.createForNameAndMaterialType(entity.name, materialType) + val mixType = mixTypeService.getOrCreateForNameAndMaterialType(entity.name, materialType) var mix = save(mix(recipe = recipe, mixType = mixType)) val mixMaterials = @@ -84,7 +84,7 @@ class MixServiceImpl( val mix = getById(entity.id) if (entity.name != null || entity.materialTypeId != null) { mix.mixType = if (mixTypeIsShared(mix.mixType)) { - mixTypeService.createForNameAndMaterialType( + mixTypeService.saveForNameAndMaterialType( entity.name ?: mix.mixType.name, if (entity.materialTypeId != null) materialTypeService.getById(entity.materialTypeId) else mix.mixType.material.materialType!! ) diff --git a/src/main/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/MixTypeService.kt b/src/main/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/MixTypeService.kt index 8911b23..f9890a5 100644 --- a/src/main/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/MixTypeService.kt +++ b/src/main/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/MixTypeService.kt @@ -9,11 +9,20 @@ import org.springframework.context.annotation.Lazy import org.springframework.stereotype.Service interface MixTypeService : NamedModelService { + /** Checks if a [MixType] with the given [name] and [materialType] exists. */ + fun existsByNameAndMaterialType(name: String, materialType: MaterialType): Boolean + /** Gets the mix type with the given [material]. */ fun getByMaterial(material: Material): MixType + /** Gets the [MixType] with the given [name] and [materialType]. */ + fun getByNameAndMaterialType(name: String, materialType: MaterialType): MixType + + /** Returns a [MixType] for the given [name] and [materialType]. If a mix type with these does not already exists, it will be created. */ + fun getOrCreateForNameAndMaterialType(name: String, materialType: MaterialType): MixType + /** Returns a new and persisted [MixType] with the given [name] and [materialType]. */ - fun createForNameAndMaterialType(name: String, materialType: MaterialType): MixType + fun saveForNameAndMaterialType(name: String, materialType: MaterialType): MixType /** Returns the given [mixType] updated with the given [name] and [materialType]. */ fun updateForNameAndMaterialType(mixType: MixType, name: String, materialType: MaterialType): MixType @@ -26,16 +35,29 @@ class MixTypeServiceImpl( @Lazy val mixService: MixService ) : AbstractNamedModelService(mixTypeRepository), MixTypeService { + override fun existsByNameAndMaterialType(name: String, materialType: MaterialType): Boolean = + repository.existsByNameAndMaterialType(name, materialType) + override fun getByMaterial(material: Material): MixType = repository.findByMaterial(material) ?: throw EntityNotFoundRestException(material.name) + override fun getByNameAndMaterialType(name: String, materialType: MaterialType): MixType = + repository.findByNameAndMaterialType(name, materialType) + ?: throw EntityNotFoundRestException("$name/${materialType.name}") + + override fun getOrCreateForNameAndMaterialType(name: String, materialType: MaterialType): MixType = + if (existsByNameAndMaterialType(name, materialType)) + getByNameAndMaterialType(name, materialType) + else + saveForNameAndMaterialType(name, materialType) + override fun save(entity: MixType): MixType { if (materialService.existsByName(entity.name)) throw EntityAlreadyExistsRestException(entity.name) return super.save(entity) } - override fun createForNameAndMaterialType(name: String, materialType: MaterialType): MixType = + override fun saveForNameAndMaterialType(name: String, materialType: MaterialType): MixType = save( mixType( name = name, diff --git a/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/MixServiceTest.kt b/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/MixServiceTest.kt index 43c1d13..dd5dd4c 100644 --- a/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/MixServiceTest.kt +++ b/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/MixServiceTest.kt @@ -36,6 +36,7 @@ class MixServiceTest : AbstractExternalModelServiceTest() { @@ -17,7 +18,8 @@ class MixTypeServiceTest : AbstractNamedModelServiceTest