Continue transition vers exceptions + DTO
This commit is contained in:
parent
5fde4078f7
commit
ddcb5c9629
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,13 @@
|
|||
package dev.fyloz.trial.colorrecipesexplorer.core.exception.model;
|
||||
|
||||
import dev.fyloz.trial.colorrecipesexplorer.core.model.IModel;
|
||||
|
||||
/**
|
||||
* Représente une exception qui sera lancée lorsqu'un objet du modèle qui est lié à un autre entité doit être supprimé
|
||||
*/
|
||||
public class EntityLinkedException extends ModelException {
|
||||
|
||||
public EntityLinkedException(Class<? extends IModel> type) {
|
||||
super(type);
|
||||
}
|
||||
}
|
|
@ -23,8 +23,7 @@ public class GenericService<T extends IModel, R extends JpaRepository<T, Long>>
|
|||
|
||||
protected Logger logger = ColorRecipesExplorerApplication.LOGGER;
|
||||
protected R dao;
|
||||
|
||||
private Class<T> type;
|
||||
protected Class<T> type;
|
||||
|
||||
public GenericService(R dao, Class<T> type) {
|
||||
this.dao = dao;
|
||||
|
@ -79,6 +78,14 @@ public class GenericService<T extends IModel, R extends JpaRepository<T, Long>>
|
|||
dao.delete(entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteById(Long id) {
|
||||
if (!existsById(id))
|
||||
throw new EntityNotFoundException(type, ModelException.IdentifierType.ID, id);
|
||||
|
||||
delete(getById(id));
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void deleteAll(@NonNull List<T> entities) {
|
||||
|
|
|
@ -53,6 +53,13 @@ public interface IGenericService<T extends IModel> {
|
|||
*/
|
||||
void delete(T entity);
|
||||
|
||||
/**
|
||||
* Supprime une entité correspondant à l'identifiant.
|
||||
*
|
||||
* @param id L'identifiant de l'entité à supprimer
|
||||
*/
|
||||
void deleteById(Long id);
|
||||
|
||||
/**
|
||||
* Supprime plusieurs entités.
|
||||
*
|
||||
|
|
|
@ -0,0 +1,50 @@
|
|||
package dev.fyloz.trial.colorrecipesexplorer.core.services.files;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
@Service
|
||||
public class FilesService {
|
||||
|
||||
private ResourceLoader resources;
|
||||
|
||||
@Autowired
|
||||
public FilesService(ResourceLoader resources) {
|
||||
this.resources = resources;
|
||||
}
|
||||
|
||||
/**
|
||||
* Lit un fichier dans le dossier resources.
|
||||
*
|
||||
* @param path Le chemin vers la fichier (sans classpath:, depuis le dossier resources)
|
||||
* @return Le contenu du fichier
|
||||
*/
|
||||
public String readResourceFile(String path) {
|
||||
String fullPath = String.format("classpath:%s", path);
|
||||
try (InputStream stream = resources.getResource(fullPath).getInputStream()) {
|
||||
return readInputStreamAsString(stream);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Lit un InputStream et le transforme en String
|
||||
*
|
||||
* @param input L'InputStream
|
||||
* @return Le InputStream transformé en String
|
||||
* @throws IOException Si la transformation vers un String échoue
|
||||
*/
|
||||
public String readInputStreamAsString(InputStream input) throws IOException {
|
||||
byte[] data = FileCopyUtils.copyToByteArray(input);
|
||||
return new String(data, StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
package dev.fyloz.trial.colorrecipesexplorer.core.services.files;
|
||||
|
||||
import org.commonmark.node.Node;
|
||||
import org.commonmark.parser.Parser;
|
||||
import org.commonmark.renderer.html.HtmlRenderer;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class MarkdownFilesService {
|
||||
|
||||
private FilesService filesService;
|
||||
|
||||
@Autowired
|
||||
public MarkdownFilesService(FilesService filesService) {
|
||||
this.filesService = filesService;
|
||||
}
|
||||
|
||||
/**
|
||||
* Lit et fait le rendu en HTML d'un fichier MarkDown.
|
||||
*
|
||||
* @param path Le chemin vers la fichier (sans classpath:, depuis le dossier resources)
|
||||
* @return Le MarkDown rendu en HTML
|
||||
*/
|
||||
public String renderMarkdown(String path) {
|
||||
String fileContent = filesService.readResourceFile(path);
|
||||
|
||||
Parser parser = Parser.builder().build();
|
||||
Node document = parser.parse(fileContent);
|
||||
HtmlRenderer renderer = HtmlRenderer.builder().build();
|
||||
|
||||
return renderer.render(document);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
package dev.fyloz.trial.colorrecipesexplorer.core.services.model;
|
||||
|
||||
import dev.fyloz.trial.colorrecipesexplorer.core.exception.model.EntityLinkedException;
|
||||
import dev.fyloz.trial.colorrecipesexplorer.core.model.Company;
|
||||
import dev.fyloz.trial.colorrecipesexplorer.core.services.GenericService;
|
||||
import dev.fyloz.trial.colorrecipesexplorer.dao.CompanyDao;
|
||||
|
@ -14,27 +15,44 @@ public class CompanyService extends GenericService<Company, CompanyDao> {
|
|||
|
||||
@Autowired
|
||||
public CompanyService(CompanyDao companyDao, RecipeDao recipeDao) {
|
||||
super(companyDao);
|
||||
super(companyDao, Company.class);
|
||||
this.recipeDao = recipeDao;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(Company entity) {
|
||||
if (isLinkedToRecipes(entity)) throw new EntityLinkedException(type);
|
||||
|
||||
super.delete(entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValidForCreation(Company entity) {
|
||||
return super.isValidForCreation(entity) && !existsByName(entity.getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Vérifie si une bannière correspondant à un nom existe.
|
||||
*
|
||||
* @param name Le nom de la bannière
|
||||
* @return Si une bannière correspondant au nome existe
|
||||
*/
|
||||
public boolean existsByName(String name) {
|
||||
return dao.existsByName(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Vérifie si une bannière est liée à une recette.
|
||||
*
|
||||
* @param company La bannière
|
||||
* @return Si la bannière est liée à une recette
|
||||
*/
|
||||
public boolean isLinkedToRecipes(Company company) {
|
||||
return !recipeDao.findAllByCompany(company).isEmpty();
|
||||
}
|
||||
|
||||
public boolean deleteIfNotLinked(Company company) {
|
||||
if (company == null) return false;
|
||||
if (isLinkedToRecipes(company)) return false;
|
||||
|
||||
return delete(company);
|
||||
@Deprecated(since = "1.3.0", forRemoval = true)
|
||||
public void deleteIfNotLinked(Company company) {
|
||||
if (!isLinkedToRecipes(company)) delete(company);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,6 +14,8 @@ import javax.validation.constraints.NotNull;
|
|||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static dev.fyloz.trial.colorrecipesexplorer.web.StringBank.MATERIALS;
|
||||
|
||||
@Service
|
||||
public class MixService extends GenericService<Mix, MixDao> {
|
||||
|
||||
|
@ -31,6 +33,39 @@ public class MixService extends GenericService<Mix, MixDao> {
|
|||
this.recipeService = recipeService;
|
||||
}
|
||||
|
||||
/**
|
||||
* Récupère les produits disponibles pour un mélange.
|
||||
* Le mélange peut ne pas exister.
|
||||
*
|
||||
* @param recipeId L'identifiant de la recette dans laquelle se trouve le mélange
|
||||
* @param mixId L'identifiant du mélange (-1 si le mélange n'existe pas)
|
||||
* @return Les produits disponibles pour ce mélange
|
||||
*/
|
||||
public Collection<Material> getAvailableMaterialsForMixId(Long recipeId, Long mixId) {
|
||||
Recipe recipe = recipeService.getById(recipeId);
|
||||
return existsById(mixId) ? getAvailableMaterialsForMix(recipe, getById(mixId)) : getAvailableMaterialsForNewMix(recipe);
|
||||
}
|
||||
|
||||
/**
|
||||
* Récupère les produits disponibles pour un mélange existant.
|
||||
*
|
||||
* @param recipe La recette dans laquelle se trouve le mélange
|
||||
* @param mix Le mélange
|
||||
* @return Les produits disponibles pour ce mélange
|
||||
*/
|
||||
public Collection<Material> getAvailableMaterialsForMix(Recipe recipe, Mix mix) {
|
||||
return getAvailableMaterialsForNewMix(recipe)
|
||||
.stream()
|
||||
.filter(m -> !m.equals(mix.getMixType().getMaterial()))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* Récupère les produits disponibles pour un mélange inexistant.
|
||||
*
|
||||
* @param recipe La recette dans laquelle se trouve le mélange
|
||||
* @return Les produits disponibles pour le nouveau mélange
|
||||
*/
|
||||
public Collection<Material> getAvailableMaterialsForNewMix(Recipe recipe) {
|
||||
Collection<MixType> recipeMixTypes = recipeService.getAssociatedMixesTypes(recipe);
|
||||
|
||||
|
@ -43,13 +78,6 @@ public class MixService extends GenericService<Mix, MixDao> {
|
|||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public Collection<Material> getAvailableMaterialsForMix(Recipe recipe, Mix mix) {
|
||||
return getAvailableMaterialsForNewMix(recipe)
|
||||
.stream()
|
||||
.filter(m -> !m.equals(mix.getMixType().getMaterial()))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public ModelResponseBuilder create(MixCreationFormDto formDto, @NotNull Recipe recipe) {
|
||||
ModelResponseBuilder modelResponseBuilder = new ModelResponseBuilder();
|
||||
|
|
|
@ -73,6 +73,12 @@ public class RecipeService extends GenericService<Recipe, RecipeDao> {
|
|||
return convertAndCreateSteps(storedRecipe, form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Met à jour les informations d'une recette trouvées dans l'explorateur de recette.
|
||||
*
|
||||
* @param form Le formulaire contenant les données mises à jour
|
||||
*/
|
||||
@Transactional
|
||||
public void updateRecipeExplorerInfos(RecipeExplorerFormDto form) {
|
||||
long recipeId = form.getRecipeId();
|
||||
Map<Long, String> locations = form.getLocations();
|
||||
|
@ -117,6 +123,31 @@ public class RecipeService extends GenericService<Recipe, RecipeDao> {
|
|||
return Arrays.stream(result).map(File::getName).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* Récupère les mélanges d'une recette triés selon leur identifiant.
|
||||
*
|
||||
* @param recipe La recette
|
||||
* @return Les mélanges de la recette triés selon leur identifiant
|
||||
*/
|
||||
public Collection<Mix> getMixesSortedById(Recipe recipe) {
|
||||
return getMixesSorted(recipe, Comparator.comparing(Mix::getId));
|
||||
}
|
||||
|
||||
/**
|
||||
* Récupère les mélanges d'une recette triés selon un comparateur.
|
||||
*
|
||||
* @param recipe La recette
|
||||
* @param comparator Le comparateur, ou la méthode de triage
|
||||
* @return Les mélanges de la recette triés selon le comparateur
|
||||
*/
|
||||
public Collection<Mix> getMixesSorted(Recipe recipe, Comparator<? super Mix> comparator) {
|
||||
return recipe
|
||||
.getMixes()
|
||||
.parallelStream()
|
||||
.sorted(comparator)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* Récupère les types de mélanges associés à la recette.
|
||||
*
|
||||
|
|
|
@ -7,6 +7,7 @@ import java.io.IOException;
|
|||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
@Deprecated(since = "1.3.0", forRemoval = true)
|
||||
public class FileUtils {
|
||||
|
||||
public static String readClasspathFile(ResourceLoader loader, String path) {
|
||||
|
|
|
@ -137,15 +137,4 @@ public class InventoryController {
|
|||
.addResponseCode(ResponseCode.SUCCESS_USING_MATERIALS)
|
||||
.build();
|
||||
}
|
||||
|
||||
// @GetMapping(value = SEARCH_INVENTORY, produces = MediaType.APPLICATION_JSON_VALUE)
|
||||
// @ResponseBody
|
||||
public Map<String, Object> searchWordInventory(@RequestParam String searchString) {
|
||||
List<Material> searchResult = materialService.getAllBySearchString(searchString);
|
||||
List<Long> outputResult = searchResult.stream().map(Material::getId).collect(Collectors.toList());
|
||||
|
||||
return new JSONResponseBuilder()
|
||||
.addAttribute("result", outputResult)
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,19 +1,11 @@
|
|||
package dev.fyloz.trial.colorrecipesexplorer.web.controller;
|
||||
|
||||
import dev.fyloz.trial.colorrecipesexplorer.core.exception.model.EntityNotFoundException;
|
||||
import dev.fyloz.trial.colorrecipesexplorer.core.io.response.ModelResponseBuilder;
|
||||
import dev.fyloz.trial.colorrecipesexplorer.core.model.Material;
|
||||
import dev.fyloz.trial.colorrecipesexplorer.core.model.Mix;
|
||||
import dev.fyloz.trial.colorrecipesexplorer.core.model.Recipe;
|
||||
import dev.fyloz.trial.colorrecipesexplorer.core.services.model.MaterialService;
|
||||
import dev.fyloz.trial.colorrecipesexplorer.core.io.response.ResponseCode;
|
||||
import dev.fyloz.trial.colorrecipesexplorer.core.services.files.MarkdownFilesService;
|
||||
import dev.fyloz.trial.colorrecipesexplorer.core.services.model.MixService;
|
||||
import dev.fyloz.trial.colorrecipesexplorer.core.services.model.MixTypeService;
|
||||
import dev.fyloz.trial.colorrecipesexplorer.core.services.model.RecipeService;
|
||||
import dev.fyloz.trial.colorrecipesexplorer.core.utils.FileUtils;
|
||||
import org.commonmark.node.Node;
|
||||
import org.commonmark.parser.Parser;
|
||||
import org.commonmark.renderer.html.HtmlRenderer;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
|
@ -23,32 +15,23 @@ import org.springframework.web.bind.annotation.PathVariable;
|
|||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Optional;
|
||||
|
||||
import static dev.fyloz.trial.colorrecipesexplorer.web.StringBank.MATERIALS;
|
||||
import static dev.fyloz.trial.colorrecipesexplorer.web.WebsitePaths.*;
|
||||
|
||||
@Controller
|
||||
public class OthersController {
|
||||
|
||||
private RecipeService recipeService;
|
||||
private MaterialService materialService;
|
||||
private MixTypeService mixTypeService;
|
||||
private MixService mixService;
|
||||
private ResourceLoader resourceLoader;
|
||||
private MarkdownFilesService markdownService;
|
||||
|
||||
@Autowired
|
||||
public OthersController(RecipeService recipeService, MaterialService materialService, MixTypeService mixTypeService, MixService mixService, ResourceLoader resourceLoader) {
|
||||
this.recipeService = recipeService;
|
||||
this.materialService = materialService;
|
||||
this.mixTypeService = mixTypeService;
|
||||
public OthersController(MixService mixService, MarkdownFilesService markdownService) {
|
||||
this.mixService = mixService;
|
||||
this.resourceLoader = resourceLoader;
|
||||
this.markdownService = markdownService;
|
||||
}
|
||||
|
||||
@GetMapping(CLOSE_TAB)
|
||||
public ModelAndView closeTab() {
|
||||
public ModelAndView getCloseTab() {
|
||||
return new ModelResponseBuilder(CLOSE_TAB).build();
|
||||
}
|
||||
|
||||
|
@ -56,38 +39,23 @@ public class OthersController {
|
|||
public ModelAndView getMaterialSelectorFragment(@PathVariable Long recipeId, @PathVariable Long mixId) {
|
||||
ModelResponseBuilder modelResponseBuilder = new ModelResponseBuilder(MATERIAL_SELECTOR_FRAGMENT);
|
||||
|
||||
Optional<Recipe> optionalRecipe = recipeService.getById(recipeId);
|
||||
if (optionalRecipe.isEmpty()) {
|
||||
return modelResponseBuilder
|
||||
.withView("")
|
||||
.build();
|
||||
try {
|
||||
modelResponseBuilder.addAttribute(MATERIALS, mixService.getAvailableMaterialsForMixId(recipeId, mixId));
|
||||
} catch (EntityNotFoundException ex) {
|
||||
modelResponseBuilder.addResponseCode(ResponseCode.RECIPE_NOT_FOUND, recipeId);
|
||||
}
|
||||
|
||||
Optional<Mix> optionalMix = mixService.getById(mixId);
|
||||
boolean mixExist = optionalMix.isPresent();
|
||||
|
||||
Collection<Material> materials = mixExist ? mixService.getAvailableMaterialsForMix(optionalRecipe.get(), optionalMix.get()) : mixService.getAvailableMaterialsForNewMix(optionalRecipe.get());
|
||||
|
||||
return modelResponseBuilder
|
||||
.addAttribute(MATERIALS, materials)
|
||||
.build();
|
||||
return modelResponseBuilder.build();
|
||||
}
|
||||
|
||||
@GetMapping(value = UPDATES)
|
||||
public ModelAndView showUpdates() {
|
||||
public ModelAndView getUpdatesPage() {
|
||||
return new ModelResponseBuilder(UPDATES).build();
|
||||
}
|
||||
|
||||
@GetMapping(value = UPDATES_GET, produces = MediaType.TEXT_HTML_VALUE)
|
||||
@ResponseBody
|
||||
public ResponseEntity<String> getUpdates() {
|
||||
String fileContent = FileUtils.readClasspathFile(resourceLoader, "classpath:updates.md");
|
||||
if (fileContent == null) return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
|
||||
|
||||
Parser parser = Parser.builder().build();
|
||||
Node document = parser.parse(fileContent);
|
||||
HtmlRenderer renderer = HtmlRenderer.builder().build();
|
||||
|
||||
return new ResponseEntity<>(renderer.render(document), HttpStatus.OK);
|
||||
return new ResponseEntity<>(markdownService.renderMarkdown("updates.md"), HttpStatus.OK);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,18 +5,19 @@ import dev.fyloz.trial.colorrecipesexplorer.core.io.response.JSONResponseBuilder
|
|||
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.Mix;
|
||||
import dev.fyloz.trial.colorrecipesexplorer.core.model.Recipe;
|
||||
import dev.fyloz.trial.colorrecipesexplorer.core.model.dto.RecipeExplorerFormDto;
|
||||
import dev.fyloz.trial.colorrecipesexplorer.core.services.model.MixService;
|
||||
import dev.fyloz.trial.colorrecipesexplorer.core.services.model.RecipeService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.Map;
|
||||
|
||||
import static dev.fyloz.trial.colorrecipesexplorer.web.WebsitePaths.*;
|
||||
|
||||
|
@ -24,27 +25,12 @@ import static dev.fyloz.trial.colorrecipesexplorer.web.WebsitePaths.*;
|
|||
public class RecipeExplorerController {
|
||||
|
||||
private RecipeService recipeService;
|
||||
private MixService mixService;
|
||||
|
||||
@Autowired
|
||||
public RecipeExplorerController(RecipeService recipeService, MixService mixService) {
|
||||
public RecipeExplorerController(RecipeService recipeService) {
|
||||
this.recipeService = recipeService;
|
||||
this.mixService = mixService;
|
||||
}
|
||||
|
||||
/**
|
||||
* Affiche la recette.
|
||||
* Cette méthode requiert l'identifiant de la recette à affiche dans l'URL.
|
||||
* <p>
|
||||
* Modèle de la page:
|
||||
* - error: Contient le message d'erreur, s'il y a lieu
|
||||
* - recipe: Contient la recette
|
||||
* - mixes: Contient les mélanges associées à la recette
|
||||
* - images: Contient la liste des noms des images associées à la recette
|
||||
*
|
||||
* @param id L'identifiant de la recette
|
||||
* @return La page à afficher.
|
||||
*/
|
||||
@GetMapping(EXPLORER_RECIPE_SPECIFIC)
|
||||
public ModelAndView getPage(@PathVariable Long id) {
|
||||
ModelResponseBuilder modelResponseBuilder = new ModelResponseBuilder(EXPLORER_RECIPE);
|
||||
|
@ -52,14 +38,9 @@ public class RecipeExplorerController {
|
|||
try {
|
||||
Recipe recipe = recipeService.getById(id);
|
||||
|
||||
modelResponseBuilder.addResponseData(ResponseDataType.RECIPE, recipe);
|
||||
|
||||
List<Mix> mixes = new ArrayList<>(recipe.getMixes()); // Convertit le PersistentBag en ArrayList
|
||||
mixes.sort(Comparator.comparing(Mix::getId));
|
||||
|
||||
return modelResponseBuilder
|
||||
.addResponseData(ResponseDataType.RECIPE, recipe)
|
||||
.addResponseData(ResponseDataType.MIXES, mixes)
|
||||
.addResponseData(ResponseDataType.MIXES, recipeService.getMixesSortedById(recipe))
|
||||
.addResponseData(ResponseDataType.IMAGES, recipeService.getImageFiles(recipe))
|
||||
.build();
|
||||
} catch (EntityNotFoundException e) {
|
||||
|
@ -76,20 +57,16 @@ public class RecipeExplorerController {
|
|||
JSONResponseBuilder responseBuilder = new JSONResponseBuilder();
|
||||
|
||||
try {
|
||||
recipeService.updateRecipeExplorerInfos(form);
|
||||
|
||||
responseBuilder.addResponseCode(ResponseCode.SUCCESS_SAVING_RECIPE_INFORMATIONS);
|
||||
} catch (EntityNotFoundException e) {
|
||||
ResponseCode responseCode = null;
|
||||
switch (e.getType().getSimpleName()) {
|
||||
case "Recipe":
|
||||
responseCode = ResponseCode.RECIPE_NOT_FOUND;
|
||||
break;
|
||||
case "Mix":
|
||||
responseCode = ResponseCode.MIX_NOT_FOUND;
|
||||
break;
|
||||
}
|
||||
|
||||
responseBuilder.addResponseCode(responseCode, e.getRequestedId());
|
||||
responseBuilder.addResponseCode(
|
||||
e.getType().equals(Recipe.class) ?
|
||||
ResponseCode.RECIPE_NOT_FOUND :
|
||||
ResponseCode.MIX_NOT_FOUND,
|
||||
e.getRequestedId()
|
||||
);
|
||||
}
|
||||
|
||||
return responseBuilder.build();
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package dev.fyloz.trial.colorrecipesexplorer.web.controller.creators;
|
||||
|
||||
import dev.fyloz.trial.colorrecipesexplorer.core.exception.model.EntityAlreadyExistsException;
|
||||
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;
|
||||
|
@ -14,10 +15,8 @@ import org.springframework.web.bind.annotation.PostMapping;
|
|||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import java.util.Optional;
|
||||
|
||||
import static dev.fyloz.trial.colorrecipesexplorer.web.WebsitePaths.CREATOR_COMPANY;
|
||||
import static dev.fyloz.trial.colorrecipesexplorer.web.WebsitePaths.INDEX;
|
||||
|
||||
@Controller
|
||||
public class CompanyCreatorController {
|
||||
|
@ -37,38 +36,15 @@ public class CompanyCreatorController {
|
|||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Permet à l'utilisateur de créer un bannière.
|
||||
* <p>
|
||||
* La création échouera si:
|
||||
* - L'utilisateur n'est pas autorisé à effectuer cette action
|
||||
* - La bannière existe déjà
|
||||
* - Une erreur est survenue lors de la sauvegarde dans la base de données
|
||||
* <p>
|
||||
* Modèle de la page:
|
||||
* - error: Contient le message d'erreur, s'il y a lieu
|
||||
* - companyName: Contient le nom de la bannière
|
||||
* <p>
|
||||
* REQUIERT UNE AUTORISATION
|
||||
*
|
||||
* @param company La bannière à créer
|
||||
* @return La page à afficher.
|
||||
*/
|
||||
@PostMapping(value = CREATOR_COMPANY, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
|
||||
public ModelAndView createCompany(@ModelAttribute @Valid Company company) {
|
||||
ModelResponseBuilder modelResponseBuilder = new ModelResponseBuilder().withRedirect(INDEX);
|
||||
ModelResponseBuilder modelResponseBuilder = new ModelResponseBuilder();
|
||||
|
||||
if (companyService.isValidForCreation(company)) {
|
||||
Optional<Company> savedCompany = companyService.save(company);
|
||||
try {
|
||||
company = companyService.save(company);
|
||||
|
||||
if (savedCompany.isPresent()) {
|
||||
return modelResponseBuilder
|
||||
.addResponseData(ResponseDataType.COMPANY_NAME, savedCompany.get().getName())
|
||||
.build();
|
||||
} else {
|
||||
modelResponseBuilder.addResponseCode(ResponseCode.ERROR_SAVING);
|
||||
}
|
||||
} else {
|
||||
modelResponseBuilder.addResponseData(ResponseDataType.COMPANY_NAME, company.getName());
|
||||
} catch (EntityAlreadyExistsException ex) {
|
||||
modelResponseBuilder.addResponseCode(ResponseCode.COMPANY_ALREADY_EXIST, company.getName());
|
||||
}
|
||||
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
package dev.fyloz.trial.colorrecipesexplorer.web.controller.removers;
|
||||
|
||||
import dev.fyloz.trial.colorrecipesexplorer.core.exception.model.EntityLinkedException;
|
||||
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.Company;
|
||||
import dev.fyloz.trial.colorrecipesexplorer.core.services.model.CompanyService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
|
@ -12,8 +13,6 @@ import org.springframework.web.bind.annotation.PathVariable;
|
|||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import static dev.fyloz.trial.colorrecipesexplorer.web.WebsitePaths.REMOVER_COMPANY;
|
||||
import static dev.fyloz.trial.colorrecipesexplorer.web.WebsitePaths.REMOVER_COMPANY_SPECIFIC;
|
||||
|
||||
|
@ -27,15 +26,6 @@ public class CompanyRemoverController {
|
|||
this.companyService = companyService;
|
||||
}
|
||||
|
||||
/**
|
||||
* Affiche la page listant toutes les bannières.
|
||||
* <p>
|
||||
* Modèle de la page:
|
||||
* - companies: Contient la liste de toutes les bannières
|
||||
*
|
||||
* @param model Le Model injecté par Thymeleaf
|
||||
* @return La page à afficher
|
||||
*/
|
||||
@GetMapping(REMOVER_COMPANY)
|
||||
public ModelAndView getPage(ModelAndView model) {
|
||||
return new ModelResponseBuilder(model)
|
||||
|
@ -44,37 +34,17 @@ public class CompanyRemoverController {
|
|||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Permet à l'utilisateur de supprimer une bannière.
|
||||
* La méthode requiert l'identifiant de la bannière à supprimer dans l'URL.
|
||||
* <p>
|
||||
* La suppression échouera si:
|
||||
* - L'utilisateur n'est pas autorisé à exécuter cette action
|
||||
* - Il n'y a aucune bannière avec cet identifiant
|
||||
* - La bannière est associée à une ou plusieurs recettes
|
||||
* <p>
|
||||
* Modèle de la page:
|
||||
* - error: Contient le message d'erreur, s'il y a lieu
|
||||
* - successCompanyName: Contient le nom de la bannière supprimée
|
||||
* <p>
|
||||
* REQUIERT UNE AUTORISATION
|
||||
*
|
||||
* @param id L'identifiant de la bannière
|
||||
* @return La page à afficher
|
||||
*/
|
||||
|
||||
@PostMapping(REMOVER_COMPANY_SPECIFIC)
|
||||
public ModelAndView removeCompany(@PathVariable Long id) {
|
||||
ModelResponseBuilder modelResponseBuilder = new ModelResponseBuilder("");
|
||||
|
||||
Optional<Company> optionalCompany = companyService.getById(id);
|
||||
if (optionalCompany.isPresent()) {
|
||||
Company company = optionalCompany.get();
|
||||
try {
|
||||
companyService.deleteById(id);
|
||||
|
||||
if (companyService.deleteIfNotLinked(company))
|
||||
modelResponseBuilder.addResponseCode(ResponseCode.SUCCESS_DELETING_COMPANY, company.getName());
|
||||
else modelResponseBuilder.addResponseCode(ResponseCode.COMPANY_LINKED, company.getName());
|
||||
} else {
|
||||
modelResponseBuilder.addResponseCode(ResponseCode.SUCCESS_DELETING_COMPANY, companyService.getById(id));
|
||||
} catch (EntityLinkedException ex) {
|
||||
modelResponseBuilder.addResponseCode(ResponseCode.COMPANY_LINKED, companyService.getById(id));
|
||||
} catch (EntityNotFoundException ex) {
|
||||
modelResponseBuilder.addResponseCode(ResponseCode.COMPANY_NOT_FOUND, id);
|
||||
}
|
||||
|
||||
|
|
Binary file not shown.
Loading…
Reference in New Issue