From a47f82462d2ecf112af4886e95b8aeebfb73043f Mon Sep 17 00:00:00 2001 From: FyloZ Date: Sun, 8 Mar 2020 22:39:55 -0400 Subject: [PATCH] =?UTF-8?q?D=C3=A9but=20corrections:=20Impossible=20de=20c?= =?UTF-8?q?r=C3=A9er=20plus=20que=20un=20(deux=3F)=20m=C3=A9langes=20avec?= =?UTF-8?q?=20le=20m=C3=AAme=20nom?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mvnw | 0 pom.xml | 43 +++++++------ .../core/model/Material.java | 18 +++++- .../core/model/MaterialType.java | 2 +- .../core/model/MixType.java | 25 ++++++-- .../core/model/Recipe.java | 2 +- .../core/services/AbstractService.java | 28 ++++----- .../core/services/IGenericService.java | 2 +- .../core/services/model/MaterialService.java | 61 +++++++------------ .../services/model/MaterialTypeService.java | 27 ++++---- .../core/services/model/MixService.java | 4 +- .../core/utils/MixBuilder.java | 16 ++++- .../dao/MaterialTypeDao.java | 6 +- src/main/resources/updates.md | 4 ++ 14 files changed, 135 insertions(+), 103 deletions(-) mode change 100644 => 100755 mvnw diff --git a/mvnw b/mvnw old mode 100644 new mode 100755 diff --git a/pom.xml b/pom.xml index 39e94a5..ab8e425 100644 --- a/pom.xml +++ b/pom.xml @@ -82,10 +82,6 @@ test 2.1.6.RELEASE - - junit - junit - org.springframework spring-test @@ -98,6 +94,16 @@ org.mockito mockito-core + + + + + + + + org.junit.jupiter + junit-jupiter-api + @@ -107,21 +113,20 @@ spring-boot-maven-plugin - org.apache.maven.plugins - maven-antrun-plugin - - - test - - run - - - - - - - - + maven-surefire-plugin + 2.19.1 + + + org.junit.platform + junit-platform-surefire-provider + 1.3.2 + + + org.junit.jupiter + junit-jupiter-engine + 5.6.0 + + diff --git a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/model/Material.java b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/model/Material.java index 24b7986..83a872d 100644 --- a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/model/Material.java +++ b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/model/Material.java @@ -6,12 +6,13 @@ import org.hibernate.annotations.ColumnDefault; import javax.persistence.*; import javax.validation.constraints.NotEmpty; import javax.validation.constraints.NotNull; +import java.util.Objects; @Entity @Data -@EqualsAndHashCode(callSuper = false) -@RequiredArgsConstructor @NoArgsConstructor +@RequiredArgsConstructor +@AllArgsConstructor public class Material implements IModel { @Id @@ -37,4 +38,17 @@ public class Material implements IModel { @ManyToOne private MaterialType materialType; + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Material material = (Material) o; + return Objects.equals(id, material.id) && + Objects.equals(name, material.name); + } + + @Override + public int hashCode() { + return Objects.hash(id, name); + } } diff --git a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/model/MaterialType.java b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/model/MaterialType.java index 7ba9e6b..a7d6a92 100644 --- a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/model/MaterialType.java +++ b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/model/MaterialType.java @@ -10,9 +10,9 @@ import java.util.Objects; @Entity @Data +@NoArgsConstructor @RequiredArgsConstructor @AllArgsConstructor -@NoArgsConstructor public class MaterialType implements IModel { public static final String IDENTIFIER_PREFIX_NAME = "prefix"; diff --git a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/model/MixType.java b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/model/MixType.java index 204c964..6a914ee 100644 --- a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/model/MixType.java +++ b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/model/MixType.java @@ -4,12 +4,13 @@ import lombok.*; import javax.persistence.*; import javax.validation.constraints.NotNull; +import java.util.Objects; @Entity @Data -@EqualsAndHashCode(callSuper = false) -@RequiredArgsConstructor @NoArgsConstructor +@RequiredArgsConstructor +@AllArgsConstructor public class MixType implements IModel { public static final String IDENTIFIER_MATERIAL_NAME = "material"; @@ -24,11 +25,25 @@ public class MixType implements IModel { @NonNull @NotNull - @OneToOne(cascade = CascadeType.ALL) + @ManyToOne(cascade = CascadeType.ALL) private Material material; - public void setName(String name) { + public void setName(String name, boolean editMaterial) { this.name = name; - this.material.setName(name); + if (editMaterial) this.material.setName(name); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + MixType mixType = (MixType) o; + return Objects.equals(id, mixType.id) && + Objects.equals(name, mixType.name); + } + + @Override + public int hashCode() { + return Objects.hash(id, name); } } diff --git a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/model/Recipe.java b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/model/Recipe.java index c30c840..be93791 100644 --- a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/model/Recipe.java +++ b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/model/Recipe.java @@ -48,7 +48,7 @@ public class Recipe implements IModel { private String note; @JsonIgnore - @OneToMany(mappedBy = "recipe", cascade = CascadeType.ALL) + @OneToMany(mappedBy = "recipe", cascade = CascadeType.ALL, fetch = FetchType.EAGER) private List mixes; @OneToMany(mappedBy = "recipe", cascade = CascadeType.ALL) diff --git a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/AbstractService.java b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/AbstractService.java index 65b2eb8..beab218 100644 --- a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/AbstractService.java +++ b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/AbstractService.java @@ -19,18 +19,18 @@ import java.util.List; import java.util.Optional; import java.util.stream.Collectors; -public abstract class AbstractService> implements IGenericService { +public abstract class AbstractService> implements IGenericService { protected Logger logger = Preferences.logger; protected R dao; - protected Class type; + protected Class type; - public AbstractService(Class type) { + public AbstractService(Class type) { this.type = type; } @Override - public boolean exists(T entity) { + public boolean exists(E entity) { return entity != null && entity.getId() != null && existsById(entity.getId()); } @@ -40,20 +40,20 @@ public abstract class AbstractService found = dao.findById(id); + public E getById(Long id) { + Optional found = dao.findById(id); if (found.isEmpty()) throw new EntityNotFoundException(type, EntityNotFoundException.IdentifierType.ID, id); return found.get(); } @Override - public List getAll() { + public List getAll() { return dao.findAll(); } @Override - public T save(@NotNull T entity) { + public E save(@NotNull E entity) { if (entity.getId() != null && existsById(entity.getId())) throw new EntityAlreadyExistsException(type, ModelException.IdentifierType.ID, entity.getId()); @@ -62,7 +62,7 @@ public abstract class AbstractService saveAll(@NotNull Collection entities) { + public Collection saveAll(@NotNull Collection entities) { return entities .stream() .map(this::save) @@ -70,7 +70,7 @@ public abstract class AbstractService entities) { + public void deleteAll(Collection entities) { dao.deleteAll(entities); } @@ -100,7 +100,7 @@ public abstract class AbstractService { * * @param entities Les entités à supprimer */ - void deleteAll(List entities); + void deleteAll(Collection entities); } diff --git a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/model/MaterialService.java b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/model/MaterialService.java index df9940c..c9fb245 100644 --- a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/model/MaterialService.java +++ b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/model/MaterialService.java @@ -43,6 +43,26 @@ public class MaterialService extends AbstractService { this.simdutService = simdutService; } + /** + * 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 + * + * @param materialType Le type de produit + * @return Si des produits sont du type de produit + */ + public boolean existsByMaterialType(MaterialType materialType) { + return dao.existsByMaterialType(materialType); + } + /** * Vérifie si un produit est lié à un ou plusieurs mélanges. * @@ -65,38 +85,6 @@ public class MaterialService extends AbstractService { .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 - * - * @param materialType Le type de produit - * @return Si des produits sont du type de produit - */ - public boolean existsByMaterialType(MaterialType materialType) { - return dao.existsByMaterialType(materialType); - } - - /** - * Récupère tous les produits qui sont d'un type de produit. - * - * @param materialType Le type de produit des produits - * @return Tous les produits qui sont du type de produit - */ - public List getAllByMaterialType(MaterialType materialType) { - if (materialType == null) return new ArrayList<>(); - - return dao.findAllByMaterialType(materialType); - } - /** * Récupère le produit correspondant à un nom. * @@ -110,11 +98,6 @@ public class MaterialService extends AbstractService { return found.get(); } - @Override - public Material save(@NotNull Material material) { - return super.save(material); - } - public Material save(@NotNull Material material, MultipartFile file) { simdutService.write(material, file); @@ -123,8 +106,8 @@ public class MaterialService extends AbstractService { @Override public Material update(Material material) { - Optional materialByCode = dao.findByName(material.getName()); - if (materialByCode.isPresent() && !material.getId().equals(materialByCode.get().getId())) + Optional materialByName = dao.findByName(material.getName()); + if (materialByName.isPresent() && !material.getId().equals(materialByName.get().getId())) throw new EntityAlreadyExistsException(type, ModelException.IdentifierType.NAME, material.getName()); return super.update(material); diff --git a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/model/MaterialTypeService.java b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/model/MaterialTypeService.java index 8dee88e..b799954 100644 --- a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/model/MaterialTypeService.java +++ b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/model/MaterialTypeService.java @@ -14,6 +14,7 @@ import org.springframework.stereotype.Service; import java.util.LinkedList; import java.util.List; +import java.util.Optional; import java.util.stream.Collectors; @Service @@ -37,6 +38,10 @@ public class MaterialTypeService extends AbstractService defaultMaterialTypes) { + this.defaultMaterialTypes = defaultMaterialTypes; + } + /** * Ajoute un type de produit dans les types de produit par défaut. * @@ -83,7 +88,7 @@ public class MaterialTypeService extends AbstractService found = dao.findByName(name); + if(found.isEmpty()) + throw new EntityNotFoundException(type, ModelException.IdentifierType.NAME, name); - /** - * Récupère un type de produit par son préfixe. - * - * @param prefix Le préfixe du type de produit à récupérer - * @return Le type de produit correspondant au préfixe - */ - public MaterialType getByPrefix(String prefix) { - return dao.findByPrefix(prefix); + return found.get(); } public MaterialType update(MaterialTypeEditorDto materialTypeDto) { @@ -129,7 +128,7 @@ public class MaterialTypeService extends AbstractService { MixType mixType = mix.getMixType(); - if (!formDto.getOldMixTypeName().equals(mixType.getName()) && mixTypeService.existsByName(formDto.getMixTypeName()) && mix.getRecipe().hasMixType(mixTypeService.getByName(formDto.getMixTypeName()))) + if (!formDto.getOldMixTypeName().equals(mixType.getName()) && mixTypeService.existsByName(formDto.getMixTypeName()) && mix.getRecipe().hasMixType(mixType)) throw new EntityAlreadyExistsException(type, ModelException.IdentifierType.OTHER, Mix.IDENTIFIER_MIX_TYPE_NAME, mix.getMixType().getName()); - if (materialService.existsByName(mixType.getName()) && !materialService.getByName(mixType.getName()).equals(mixType.getMaterial())) + if (materialService.existsByName(mixType.getName()) && !materialService.getByName(mixType.getName()).isMixType()) throw new EntityAlreadyExistsException(type, ModelException.IdentifierType.OTHER, MixType.IDENTIFIER_MATERIAL_NAME, mixType.getName()); update(mix); diff --git a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/utils/MixBuilder.java b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/utils/MixBuilder.java index 34429d1..5f2e3ee 100644 --- a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/utils/MixBuilder.java +++ b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/utils/MixBuilder.java @@ -37,11 +37,21 @@ public class MixBuilder { } public MixBuilder withDto(MixFormDto dto) { + String mixTypeName = dto.getMixTypeName(); + if (this.mixType == null) { -// this.mixType = mixTypeService.save(dto.getMixTypeName(), dto.getMaterialType()); - this.mixType = mixTypeService.create(dto.getMixTypeName(), dto.getMaterialType()); + this.mixType = mixTypeService.create(mixTypeName, dto.getMaterialType()); } else { - this.mixType.setName(dto.getMixTypeName()); + this.mixType = new MixType(this.mixType.getId(), this.mixType.getName(), this.mixType.getMaterial()); + + // TODO le nom ne change pas + if (materialService.existsByName(mixTypeName)) { + this.mixType.setName(mixTypeName, false); + this.mixType.setMaterial(materialService.getByName(mixTypeName)); + } else { + this.mixType.setName(mixTypeName, true); + } + this.mixType.getMaterial().setMaterialType(dto.getMaterialType()); } diff --git a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/dao/MaterialTypeDao.java b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/dao/MaterialTypeDao.java index 784c52f..479d2a0 100644 --- a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/dao/MaterialTypeDao.java +++ b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/dao/MaterialTypeDao.java @@ -3,14 +3,16 @@ package dev.fyloz.trial.colorrecipesexplorer.dao; import dev.fyloz.trial.colorrecipesexplorer.core.model.MaterialType; import org.springframework.data.jpa.repository.JpaRepository; +import java.util.Optional; + public interface MaterialTypeDao extends JpaRepository { boolean existsByName(String name); boolean existsByPrefix(String prefix); - MaterialType findByPrefix(String prefix); + Optional findByPrefix(String prefix); - MaterialType findByName(String name); + Optional findByName(String name); } diff --git a/src/main/resources/updates.md b/src/main/resources/updates.md index 4b1e01e..cc21989 100644 --- a/src/main/resources/updates.md +++ b/src/main/resources/updates.md @@ -1,3 +1,7 @@ +# v1.3.1 +### Corrections +* Correction d'un bug qui empêchait d'avoir plus que 2 mélanges avec le même nom. + # v1.3.0 (Optimisations back-end) ### Note: Cette mise à jour n'est pas compatible avec les anciennes versions. ### Corrections