From b13d8c8e63ecbe447aaeb67130daf9daf8942152 Mon Sep 17 00:00:00 2001 From: FyloZ Date: Wed, 19 Feb 2020 23:36:08 -0500 Subject: [PATCH] =?UTF-8?q?Am=C3=A9liorations=20cr=C3=A9ation/=C3=A9dition?= =?UTF-8?q?=20des=20m=C3=A9langes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/model/Material.java | 2 - .../colorrecipesexplorer/core/model/Mix.java | 2 +- .../core/model/Recipe.java | 7 +- ...ixCreationFormDto.java => MixFormDto.java} | 4 +- .../core/services/model/MaterialService.java | 14 +++ .../core/services/model/MixService.java | 60 +++------- .../core/services/model/MixTypeService.java | 15 +++ .../core/utils/MixBuilder.java | 111 ++++++++++++++++++ .../creators/MixCreatorController.java | 27 +++-- .../editors/MixEditorController.java | 5 +- .../editors/RecipeEditorController.java | 4 +- .../resources/templates/recipe/explore.html | 2 +- 12 files changed, 188 insertions(+), 65 deletions(-) rename src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/model/dto/{MixCreationFormDto.java => MixFormDto.java} (86%) create mode 100644 src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/utils/MixBuilder.java 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 9f4eab6..dd277c8 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 @@ -26,12 +26,10 @@ public class Material implements IModel { @NonNull @NotNull - @ColumnDefault("0") private Float inventoryQuantity; @NonNull @NotNull - @ColumnDefault("false") private Boolean isMixType; @NonNull diff --git a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/model/Mix.java b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/model/Mix.java index ac1d7a9..e291432 100644 --- a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/model/Mix.java +++ b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/model/Mix.java @@ -29,7 +29,7 @@ public class Mix implements IModel { @ManyToOne private MixType mixType; - @OneToMany(cascade = CascadeType.ALL) + @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL) @JoinColumn(name = "mix") private List mixQuantities; 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 63b9e6e..210458d 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 @@ -53,10 +53,13 @@ public class Recipe implements IModel { private List recipeSteps; public Collection getMixTypes() { - return mixes - .stream() + return mixes.stream() .map(Mix::getMixType) .collect(Collectors.toList()); } + public boolean hasMixType(MixType mixType) { + return getMixTypes().contains(mixType); + } + } diff --git a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/model/dto/MixCreationFormDto.java b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/model/dto/MixFormDto.java similarity index 86% rename from src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/model/dto/MixCreationFormDto.java rename to src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/model/dto/MixFormDto.java index c8ae70d..bbdf555 100644 --- a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/model/dto/MixCreationFormDto.java +++ b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/model/dto/MixFormDto.java @@ -7,7 +7,7 @@ import lombok.Data; import java.util.List; @Data -public class MixCreationFormDto { +public class MixFormDto { private Recipe recipe; @@ -19,6 +19,6 @@ public class MixCreationFormDto { private List quantities; - public MixCreationFormDto() { + public MixFormDto() { } } 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 0c4b8c7..13432fb 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 @@ -3,6 +3,7 @@ package dev.fyloz.trial.colorrecipesexplorer.core.services.model; import dev.fyloz.trial.colorrecipesexplorer.ColorRecipesExplorerApplication; import dev.fyloz.trial.colorrecipesexplorer.core.exception.SimdutException; 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.file.FileHandler; import dev.fyloz.trial.colorrecipesexplorer.core.model.Material; @@ -58,6 +59,19 @@ public class MaterialService extends GenericService { .collect(Collectors.toList()); } + /** + * Récupère le produit correspondant à un nom. + * + * @param name Le nom du produit à récupérer + * @return Le produit correspondant au nom + */ + public Material getByName(String name) { + Optional found = dao.findByName(name); + if (found.isEmpty()) throw new EntityNotFoundException(type, ModelException.IdentifierType.NAME, name); + + return found.get(); + } + public Material save(@NotNull Material material, MultipartFile file) { addSimdut(file, material); diff --git a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/model/MixService.java b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/model/MixService.java index 3e8d1dc..ef08c7f 100644 --- a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/model/MixService.java +++ b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/model/MixService.java @@ -1,9 +1,12 @@ 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.ModelException; import dev.fyloz.trial.colorrecipesexplorer.core.io.response.ModelResponseBuilder; import dev.fyloz.trial.colorrecipesexplorer.core.model.*; -import dev.fyloz.trial.colorrecipesexplorer.core.model.dto.MixCreationFormDto; +import dev.fyloz.trial.colorrecipesexplorer.core.model.dto.MixFormDto; import dev.fyloz.trial.colorrecipesexplorer.core.services.GenericService; +import dev.fyloz.trial.colorrecipesexplorer.core.utils.MixBuilder; import dev.fyloz.trial.colorrecipesexplorer.dao.MixDao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -75,50 +78,25 @@ public class MixService extends GenericService { } @Transactional - public ModelResponseBuilder create(MixCreationFormDto formDto, @NotNull Recipe recipe) { - throw new UnsupportedOperationException("TODO"); + public void create(MixFormDto formDto) { + Mix mix = new MixBuilder(mixTypeService, materialService) + .withDto(formDto) + .build(); -// ModelResponseBuilder modelResponseBuilder = new ModelResponseBuilder(); -// -// List materials = new ArrayList<>(); -// for (String materialCode : formDto.getMaterials()) { -// Optional found = materialService.getByName(materialCode); -// if (found.isEmpty()) { -// return modelResponseBuilder.addResponseCode(ResponseCode.MATERIAL_NOT_FOUND_BY_NAME, materialCode); -// } -// -// materials.add(found.get()); -// } -// -// Optional optionalMixType = mixTypeService.createByName(formDto.getMixTypeName(), formDto.getMaterialType()); -// if (optionalMixType.isEmpty()) return modelResponseBuilder.addResponseCode(ResponseCode.ERROR_SAVING); -// -// MixType mixType = optionalMixType.get(); -// if (recipeService.hasMixType(recipe, mixType)) -// return modelResponseBuilder.addResponseCode(ResponseCode.MIX_TYPE_ALREADY_USED, mixType.getName()); -// -// // Crée le mélange en premier pour avoir accès à son ID pour les autres éléments -// Mix mix = new Mix(recipe, mixType); -// -// Optional savedMix = save(mix); -// if (savedMix.isPresent()) { -// mix = savedMix.get(); -// -// List mixQuantities = createMixQuantities(savedMix.get(), materials, formDto.getQuantities()); -// mix.setMixQuantities(mixQuantities); -// -// // Retourne aucune erreur s'il la mise à jour à lieu -// if (update(mix).isPresent()) return null; -// -// deleteMix(mix); -// } -// -// return modelResponseBuilder.addResponseCode(ResponseCode.ERROR_SAVING); + if (mix.getRecipe().hasMixType(mix.getMixType())) + throw new EntityAlreadyExistsException(type, ModelException.IdentifierType.OTHER, "mixType", mix.getMixType()); + + save(mix); } @Transactional - public ModelResponseBuilder edit(Mix mix, MixCreationFormDto formDto) { - throw new UnsupportedOperationException("TODO"); + public void edit(Mix mix, MixFormDto formDto) { + mix = new MixBuilder(mixTypeService, materialService) + .withMix(mix) + .withDto(formDto) + .build(); + + update(mix); // ModelResponseBuilder modelResponseBuilder = new ModelResponseBuilder(); // Material material = mix.getMixType().getMaterial(); diff --git a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/model/MixTypeService.java b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/model/MixTypeService.java index 8cafbc6..f82d28e 100644 --- a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/model/MixTypeService.java +++ b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/model/MixTypeService.java @@ -3,6 +3,7 @@ package dev.fyloz.trial.colorrecipesexplorer.core.services.model; 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; +import dev.fyloz.trial.colorrecipesexplorer.core.model.MaterialType; import dev.fyloz.trial.colorrecipesexplorer.core.model.MixType; import dev.fyloz.trial.colorrecipesexplorer.core.services.GenericService; import dev.fyloz.trial.colorrecipesexplorer.dao.MixTypeDao; @@ -45,4 +46,18 @@ public class MixTypeService extends GenericService { return found.get(); } + + /** + * Crée un type de mélange. + * + * @param name Le nom du type de mélange + * @param materialType Le type de produit du type de mélange + * @return Le type de mélange créé + */ + public MixType createByName(String name, MaterialType materialType) { + Material mixTypeMaterial = new Material(name, 0f, true, materialType); + MixType mixType = new MixType(name, mixTypeMaterial); + + return save(mixType); + } } 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 new file mode 100644 index 0000000..e0b4b64 --- /dev/null +++ b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/utils/MixBuilder.java @@ -0,0 +1,111 @@ +package dev.fyloz.trial.colorrecipesexplorer.core.utils; + +import dev.fyloz.trial.colorrecipesexplorer.core.model.*; +import dev.fyloz.trial.colorrecipesexplorer.core.model.dto.MixFormDto; +import dev.fyloz.trial.colorrecipesexplorer.core.services.model.MaterialService; +import dev.fyloz.trial.colorrecipesexplorer.core.services.model.MixTypeService; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class MixBuilder { + + private MixTypeService mixTypeService; + private MaterialService materialService; + + private Long id; + private Recipe recipe; + private MixType mixType; + private String location; + private List mixQuantities = new ArrayList<>(); + + private Map quantities = new HashMap<>(); + + public MixBuilder(MixTypeService mixTypeService, MaterialService materialService) { + this.mixTypeService = mixTypeService; + this.materialService = materialService; + } + + public MixBuilder withMix(Mix mix) { + this.id = mix.getId(); + this.recipe = mix.getRecipe(); + this.mixType = mix.getMixType(); + this.location = mix.getLocation(); + this.mixQuantities = mix.getMixQuantities(); + + return this; + } + + public MixBuilder withDto(MixFormDto dto) { + if (this.mixType == null) { + mixTypeService.createByName(dto.getMixTypeName(), dto.getMaterialType()); + } else { + this.mixType.setName(dto.getMixTypeName()); + this.mixType.getMaterial().setMaterialType(dto.getMaterialType()); + } + + this.recipe = dto.getRecipe(); + + for (int i = 0; i < dto.getMaterials().size(); i++) + quantities.put(dto.getMaterials().get(i), dto.getQuantities().get(i)); + + return this; + } + + public MixBuilder withId(Long id) { + this.id = id; + + return this; + } + + public MixBuilder withRecipe(Recipe recipe) { + this.recipe = recipe; + + return this; + } + + public MixBuilder withMixType(MixType mixType) { + this.mixType = mixType; + + return this; + } + + public MixBuilder withMixQuantity(MixQuantity mixQuantity) { + this.mixQuantities.add(mixQuantity); + + return this; + } + + public MixBuilder withMixQuantities(List mixQuantities) { + this.mixQuantities = mixQuantities; + + return this; + } + + public Mix build() { + Mix mix = new Mix(this.recipe, this.mixType); + + createMixQuantities(mix); + + mix.setId(this.id); + mix.setLocation(this.location); + mix.setMixQuantities(this.mixQuantities); + + return mix; + } + + private void createMixQuantities(Mix mix) { + List mixQuantities = new ArrayList<>(); + + for (Map.Entry quantityEntry : quantities.entrySet()) { + Material material = materialService.getByName(quantityEntry.getKey()); + Float quantity = quantityEntry.getValue(); + + mixQuantities.add(new MixQuantity(mix, material, quantity)); + } + + this.mixQuantities = mixQuantities; + } +} diff --git a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/creators/MixCreatorController.java b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/creators/MixCreatorController.java index 7188f3c..8bb0ed0 100644 --- a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/creators/MixCreatorController.java +++ b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/creators/MixCreatorController.java @@ -1,11 +1,12 @@ 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.io.response.ModelResponseBuilder; import dev.fyloz.trial.colorrecipesexplorer.core.io.response.ResponseCode; import dev.fyloz.trial.colorrecipesexplorer.core.io.response.ResponseDataType; import dev.fyloz.trial.colorrecipesexplorer.core.model.Recipe; -import dev.fyloz.trial.colorrecipesexplorer.core.model.dto.MixCreationFormDto; +import dev.fyloz.trial.colorrecipesexplorer.core.model.dto.MixFormDto; import dev.fyloz.trial.colorrecipesexplorer.core.services.model.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; @@ -60,21 +61,21 @@ public class MixCreatorController { } @PostMapping(value = CREATOR_MIX, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE) - public ModelAndView createMix(@ModelAttribute @Valid MixCreationFormDto formDto) { + public ModelAndView createMix(@ModelAttribute @Valid MixFormDto formDto) { ModelResponseBuilder modelResponseBuilder = new ModelResponseBuilder(); - Recipe recipe = formDto.getRecipe(); - if (recipe == null) return modelResponseBuilder - .withRedirect(EDITOR_RECIPE) - .addResponseCode(ResponseCode.RECIPE_NOT_FOUND_NO_PARAMS) - .build(); + try { + mixService.create(formDto); - ModelResponseBuilder mixCreationResponse = mixService.create(formDto, recipe); - if (mixCreationResponse != null) - return getPage(mixCreationResponse.build(), formDto.getRecipe().getId()); + return modelResponseBuilder + .withRedirect(EDITOR_RECIPE_SPECIFIC, formDto.getRecipe().getId()) + .build(); + } catch (EntityNotFoundException ex) { + modelResponseBuilder.addResponseCode(ResponseCode.MATERIAL_NOT_FOUND, ex.getRequestedId()); + } catch (EntityAlreadyExistsException ex) { + modelResponseBuilder.addResponseCode(ResponseCode.MIX_TYPE_ALREADY_USED, formDto.getMixTypeName()); + } - return modelResponseBuilder - .withRedirect(EDITOR_RECIPE_SPECIFIC, recipe.getId()) - .build(); + return getPage(modelResponseBuilder.build(), formDto.getRecipe().getId()); } } diff --git a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/editors/MixEditorController.java b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/editors/MixEditorController.java index ec40b91..feaf9a6 100644 --- a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/editors/MixEditorController.java +++ b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/editors/MixEditorController.java @@ -5,7 +5,7 @@ import dev.fyloz.trial.colorrecipesexplorer.core.io.response.ModelResponseBuilde import dev.fyloz.trial.colorrecipesexplorer.core.io.response.ResponseCode; import dev.fyloz.trial.colorrecipesexplorer.core.io.response.ResponseDataType; import dev.fyloz.trial.colorrecipesexplorer.core.model.Mix; -import dev.fyloz.trial.colorrecipesexplorer.core.model.dto.MixCreationFormDto; +import dev.fyloz.trial.colorrecipesexplorer.core.model.dto.MixFormDto; import dev.fyloz.trial.colorrecipesexplorer.core.services.model.MaterialService; import dev.fyloz.trial.colorrecipesexplorer.core.services.model.MaterialTypeService; import dev.fyloz.trial.colorrecipesexplorer.core.services.model.MixService; @@ -42,6 +42,7 @@ public class MixEditorController { return modelResponseBuilder .addResponseData(ResponseDataType.MIX, mix) + .addResponseData(ResponseDataType.MATERIAL_TYPE, mix.getMixType().getMaterial().getMaterialType()) .addResponseData(ResponseDataType.MATERIAL_TYPES, materialTypeService.getAll()) .addResponseData(ResponseDataType.MIX_JSON, mixService.asJson(mix)) .addResponseData(ResponseDataType.MATERIALS_JSON, materialService.asJson(mixService.getAvailableMaterialsForMix(mix))) @@ -55,7 +56,7 @@ public class MixEditorController { } @PostMapping(value = EDITOR_MIX, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE) - public ModelAndView updateMix(@ModelAttribute @Valid MixCreationFormDto formDto, @RequestParam("mixId") Long id) { + public ModelAndView updateMix(@ModelAttribute @Valid MixFormDto formDto, @RequestParam("mixId") Long id) { ModelResponseBuilder modelResponseBuilder = new ModelResponseBuilder().withRedirect(EDITOR_RECIPE_SPECIFIC, formDto.getRecipe().getId()); try { diff --git a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/editors/RecipeEditorController.java b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/editors/RecipeEditorController.java index 1575a87..fbef517 100644 --- a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/editors/RecipeEditorController.java +++ b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/editors/RecipeEditorController.java @@ -10,6 +10,7 @@ import dev.fyloz.trial.colorrecipesexplorer.core.services.model.CompanyService; import dev.fyloz.trial.colorrecipesexplorer.core.services.model.RecipeService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; +import org.springframework.lang.Nullable; import org.springframework.stereotype.Controller; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.GetMapping; @@ -43,8 +44,9 @@ public class RecipeEditorController { public ModelAndView getEditPage(ModelAndView model, @PathVariable Long id, Recipe recipe) { ModelResponseBuilder modelResponseBuilder = new ModelResponseBuilder(model).withView(EDITOR_RECIPE_EDITOR); + // TODO les produits dans les mélanges ne sont pas dans l'ordre de création try { - if (recipe == null) recipe = recipeService.getById(id); + if (recipe.getName() == null) recipe = recipeService.getById(id); modelResponseBuilder .addResponseData(ResponseDataType.RECIPE, recipe) diff --git a/src/main/resources/templates/recipe/explore.html b/src/main/resources/templates/recipe/explore.html index ea0d637..5a9b6b7 100644 --- a/src/main/resources/templates/recipe/explore.html +++ b/src/main/resources/templates/recipe/explore.html @@ -213,7 +213,7 @@ $(() => { $("#modifyRecipe").on({ click: function () { - window.location.href = `/recipe/editor/${$("#recipeID").val()}`; + window.location.href = `/recipe/editor/${$("#recipeId").val()}`; } });