INDEV - Empêcher la création de mélanges avec le même nom qu'un produit

This commit is contained in:
FyloZ 2020-02-24 19:23:45 -05:00
parent b1a4c4af40
commit 58a60645b4
17 changed files with 2908 additions and 3642 deletions

File diff suppressed because it is too large Load Diff

View File

@ -38,6 +38,7 @@ public enum ResponseCode {
RECIPE_ALREADY_EXIST(35, ResponseCodeType.ERROR, 1),
CANNOT_REMOVE_DEFAULT_MATERIAL_TYPE(36, ResponseCodeType.ERROR, 0),
CANNOT_EDIT_DEFAULT_MATERIAL_TYPE(37, ResponseCodeType.ERROR, 0),
MATERIAL_AND_MIX_TYPE_CANNOT_HAVE_SAME_NAME(38, ResponseCodeType.ERROR, 0),
// HTTP Errors
_500(100, ResponseCodeType.ERROR, 0),

View File

@ -26,4 +26,9 @@ public class MixType implements IModel {
@NotNull
@OneToOne(cascade = CascadeType.ALL)
private Material material;
public void setName(String name) {
this.name = name;
this.material.setName(name);
}
}

View File

@ -79,6 +79,7 @@ public class Recipe implements IModel {
}
/**
* Vérifie si la recette contient un type de mélange.
*
* @param mixType Le type de mélange

View File

@ -14,10 +14,6 @@ import java.util.List;
@Setter
public class MaterialTypeProperties {
public MaterialTypeProperties() {
System.out.println("TEST");
}
private List<MaterialType> defaults;
}

View File

@ -11,6 +11,8 @@ public class MixFormDto {
private Recipe recipe;
private String oldMixTypeName;
private String mixTypeName;
private MaterialType materialType;

View File

@ -53,6 +53,16 @@ public class MaterialService extends GenericService<Material, MaterialDao> {
.collect(Collectors.toList());
}
/**
* Vérifie si un produit correspondant à un nom existe.
*
* @param name Le nom du produit
* @return Si un produit correspondant au nom existe
*/
public boolean existsByName(String name) {
return dao.existsByName(name);
}
/**
* Vérifie si des produits sont d'un type de produit
*
@ -89,14 +99,14 @@ public class MaterialService extends GenericService<Material, MaterialDao> {
}
@Override
public Material save(@NotNull Material entity) {
throw new UnsupportedOperationException("Cette méthode n'est pas supportée pour les produits. Utiliser MaterialService::save(Material, MultipartFile)");
public Material save(@NotNull Material material) {
return super.save(material);
}
public Material save(@NotNull Material material, MultipartFile file) {
simdutService.write(material, file);
return super.save(material);
return save(material);
}
@Override
@ -127,4 +137,5 @@ public class MaterialService extends GenericService<Material, MaterialDao> {
delete(material);
}
}
}

View File

@ -123,7 +123,7 @@ public class MaterialTypeService extends GenericService<MaterialType, MaterialTy
if (!materialTypeDto.getOldPrefix().equals(materialType.getPrefix()) && existsByPrefix(materialType.getPrefix()))
throw new EntityAlreadyExistsException(type, ModelException.IdentifierType.OTHER, MaterialType.IDENTIFIER_PREFIX_NAME, materialType.getPrefix());
return dao.save(materialType);
return super.save(materialType);
}
@Override

View File

@ -82,19 +82,28 @@ public class MixService extends GenericService<Mix, MixDao> {
.withDto(formDto)
.build();
if (materialService.existsByName(formDto.getMixTypeName()))
throw new EntityAlreadyExistsException(type, ModelException.IdentifierType.OTHER, "materialName", formDto.getMixTypeName());
if (mix.getRecipe().hasMixType(mix.getMixType()))
throw new EntityAlreadyExistsException(type, ModelException.IdentifierType.OTHER, "mixType", mix.getMixType());
mixTypeService.save(mix.getMixType());
save(mix);
}
@Transactional
public void update(Mix mix, MixFormDto formDto) {
mix = new MixBuilder(mixTypeService, materialService)
.withMix(mix)
.withDto(formDto)
.build();
MixType mixType = mix.getMixType();
if (materialService.existsByName(mixType.getName()) && !materialService.getByName(mixType.getName()).equals(mixType.getMaterial()))
throw new EntityAlreadyExistsException(type, ModelException.IdentifierType.OTHER, "materialName", mixType.getName());
if (!formDto.getOldMixTypeName().equals(mixType.getName()) && mix.getRecipe().hasMixType(mix.getMixType()))
throw new EntityAlreadyExistsException(type, ModelException.IdentifierType.OTHER, "mixType", mix.getMixType());
update(mix);
}

View File

@ -1,5 +1,6 @@
package dev.fyloz.trial.colorrecipesexplorer.core.services.model;
import dev.fyloz.trial.colorrecipesexplorer.core.exception.model.EntityAlreadyExistsException;
import dev.fyloz.trial.colorrecipesexplorer.core.exception.model.EntityNotFoundException;
import dev.fyloz.trial.colorrecipesexplorer.core.exception.model.ModelException;
import dev.fyloz.trial.colorrecipesexplorer.core.model.Material;
@ -11,13 +12,18 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@Service
public class MixTypeService extends GenericService<MixType, MixTypeDao> {
private MaterialService materialService;
@Autowired
public MixTypeService(MixTypeDao mixTypeDao) {
public MixTypeService(MixTypeDao mixTypeDao, MaterialService materialService) {
super(mixTypeDao, MixType.class);
this.materialService = materialService;
}
/**
@ -54,10 +60,14 @@ public class MixTypeService extends GenericService<MixType, MixTypeDao> {
* @param materialType Le type de produit du type de mélange
* @return Le type de mélange créé
*/
public MixType save(String name, MaterialType materialType) {
public MixType create(String name, MaterialType materialType) {
Material mixTypeMaterial = new Material(name, 0f, true, materialType);
MixType mixType = new MixType(name, mixTypeMaterial);
return new MixType(name, mixTypeMaterial);
}
return save(mixType);
@Override
public MixType update(MixType mixType) {
return super.update(mixType);
}
}

View File

@ -6,6 +6,7 @@ import dev.fyloz.trial.colorrecipesexplorer.core.services.model.MaterialService;
import dev.fyloz.trial.colorrecipesexplorer.core.services.model.MixTypeService;
import java.util.*;
import java.util.stream.Stream;
public class MixBuilder {
@ -37,7 +38,8 @@ public class MixBuilder {
public MixBuilder withDto(MixFormDto dto) {
if (this.mixType == null) {
this.mixType = mixTypeService.save(dto.getMixTypeName(), dto.getMaterialType());
// this.mixType = mixTypeService.save(dto.getMixTypeName(), dto.getMaterialType());
this.mixType = mixTypeService.create(dto.getMixTypeName(), dto.getMaterialType());
} else {
this.mixType.setName(dto.getMixTypeName());
this.mixType.getMaterial().setMaterialType(dto.getMaterialType());

View File

@ -14,6 +14,8 @@ public interface MaterialDao extends JpaRepository<Material, Long> {
List<Material> findAllByMaterialType(MaterialType materialType);
boolean existsByName(String name);
boolean existsByMaterialType(MaterialType materialType);
}

View File

@ -2,6 +2,7 @@ package dev.fyloz.trial.colorrecipesexplorer.web.controller.creators;
import dev.fyloz.trial.colorrecipesexplorer.core.exception.model.EntityAlreadyExistsException;
import dev.fyloz.trial.colorrecipesexplorer.core.exception.model.EntityNotFoundException;
import dev.fyloz.trial.colorrecipesexplorer.core.exception.model.ModelException;
import dev.fyloz.trial.colorrecipesexplorer.core.io.response.ModelResponseBuilder;
import dev.fyloz.trial.colorrecipesexplorer.core.io.response.ResponseCode;
import dev.fyloz.trial.colorrecipesexplorer.core.io.response.ResponseDataType;
@ -73,7 +74,10 @@ public class MixCreatorController {
} catch (EntityNotFoundException ex) {
modelResponseBuilder.addResponseCode(ResponseCode.MATERIAL_NOT_FOUND, ex.getRequestedId());
} catch (EntityAlreadyExistsException ex) {
modelResponseBuilder.addResponseCode(ResponseCode.MIX_TYPE_ALREADY_USED, formDto.getMixTypeName());
if (ex.getIdentifierName().equals(ModelException.IdentifierType.NAME.getName()))
modelResponseBuilder.addResponseCode(ResponseCode.MIX_TYPE_ALREADY_USED, ex.getRequestedId());
else if (ex.getIdentifierName().equals("materialName"))
modelResponseBuilder.addResponseCode(ResponseCode.MATERIAL_AND_MIX_TYPE_CANNOT_HAVE_SAME_NAME);
}
return getPage(modelResponseBuilder.build(), formDto.getRecipe().getId());

View File

@ -1,5 +1,6 @@
package dev.fyloz.trial.colorrecipesexplorer.web.controller.editors;
import dev.fyloz.trial.colorrecipesexplorer.core.exception.model.EntityAlreadyExistsException;
import dev.fyloz.trial.colorrecipesexplorer.core.exception.model.EntityNotFoundException;
import dev.fyloz.trial.colorrecipesexplorer.core.io.response.ModelResponseBuilder;
import dev.fyloz.trial.colorrecipesexplorer.core.io.response.ResponseCode;
@ -65,6 +66,11 @@ public class MixEditorController {
mixService.update(mix, formDto);
} catch (EntityNotFoundException ex) {
return getPage(modelResponseBuilder.addResponseCode(ResponseCode.MIX_NOT_FOUND, id).build(), id);
} catch (EntityAlreadyExistsException ex) {
if (ex.getIdentifierName().equals("materialName"))
return getPage(modelResponseBuilder.addResponseCode(ResponseCode.MATERIAL_AND_MIX_TYPE_CANNOT_HAVE_SAME_NAME).build(), id);
if (ex.getIdentifierName().equals("mixType"))
modelResponseBuilder.addResponseCode(ResponseCode.MIX_TYPE_ALREADY_USED, ex.getRequestedId());
}
return modelResponseBuilder.build();

View File

@ -38,3 +38,4 @@ response.34=The material type {0} has been deleted
response.35=There is already a recipe with the ID {0}
response.36=You can't remove a default material type
response.37=You can't edit a default material type
response.38=A mix can't have the same name as a material

View File

@ -38,3 +38,4 @@ response.34=Le type de produit {0} a bien été supprimé
response.35=Il y a déjà une recette avec l''ID {0}
response.37=Vous ne pouvez pas modifier un type de produit par défaut
response.36=Vous ne pouvez pas supprimer un type de produit par défaut
response.38=Un mélange ne peut pas avoir le même nom qu''un produit

Binary file not shown.