Améliorations création/édition des mélanges
This commit is contained in:
parent
1b5480e800
commit
b13d8c8e63
|
@ -26,12 +26,10 @@ public class Material implements IModel {
|
|||
|
||||
@NonNull
|
||||
@NotNull
|
||||
@ColumnDefault("0")
|
||||
private Float inventoryQuantity;
|
||||
|
||||
@NonNull
|
||||
@NotNull
|
||||
@ColumnDefault("false")
|
||||
private Boolean isMixType;
|
||||
|
||||
@NonNull
|
||||
|
|
|
@ -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<MixQuantity> mixQuantities;
|
||||
|
||||
|
|
|
@ -53,10 +53,13 @@ public class Recipe implements IModel {
|
|||
private List<RecipeStep> recipeSteps;
|
||||
|
||||
public Collection<MixType> getMixTypes() {
|
||||
return mixes
|
||||
.stream()
|
||||
return mixes.stream()
|
||||
.map(Mix::getMixType)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public boolean hasMixType(MixType mixType) {
|
||||
return getMixTypes().contains(mixType);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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<Float> quantities;
|
||||
|
||||
public MixCreationFormDto() {
|
||||
public MixFormDto() {
|
||||
}
|
||||
}
|
|
@ -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<Material, MaterialDao> {
|
|||
.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<Material> 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);
|
||||
|
||||
|
|
|
@ -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<Mix, MixDao> {
|
|||
}
|
||||
|
||||
@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<Material> materials = new ArrayList<>();
|
||||
// for (String materialCode : formDto.getMaterials()) {
|
||||
// Optional<Material> found = materialService.getByName(materialCode);
|
||||
// if (found.isEmpty()) {
|
||||
// return modelResponseBuilder.addResponseCode(ResponseCode.MATERIAL_NOT_FOUND_BY_NAME, materialCode);
|
||||
// }
|
||||
//
|
||||
// materials.add(found.get());
|
||||
// }
|
||||
//
|
||||
// Optional<MixType> 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<Mix> savedMix = save(mix);
|
||||
// if (savedMix.isPresent()) {
|
||||
// mix = savedMix.get();
|
||||
//
|
||||
// List<MixQuantity> 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();
|
||||
|
|
|
@ -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<MixType, MixTypeDao> {
|
|||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<MixQuantity> mixQuantities = new ArrayList<>();
|
||||
|
||||
private Map<String, Float> 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<MixQuantity> 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<MixQuantity> mixQuantities = new ArrayList<>();
|
||||
|
||||
for (Map.Entry<String, Float> quantityEntry : quantities.entrySet()) {
|
||||
Material material = materialService.getByName(quantityEntry.getKey());
|
||||
Float quantity = quantityEntry.getValue();
|
||||
|
||||
mixQuantities.add(new MixQuantity(mix, material, quantity));
|
||||
}
|
||||
|
||||
this.mixQuantities = mixQuantities;
|
||||
}
|
||||
}
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -213,7 +213,7 @@
|
|||
$(() => {
|
||||
$("#modifyRecipe").on({
|
||||
click: function () {
|
||||
window.location.href = `/recipe/editor/${$("#recipeID").val()}`;
|
||||
window.location.href = `/recipe/editor/${$("#recipeId").val()}`;
|
||||
}
|
||||
});
|
||||
|
||||
|
|
Loading…
Reference in New Issue