From a7c5b23198d1a683cd0e5a0a746f9f3ec4d48b52 Mon Sep 17 00:00:00 2001 From: Youssef Date: Tue, 7 Jul 2026 21:54:38 +0200 Subject: [PATCH] feat: add category duplication functionality --- .../core/service/CategorieService.java | 46 ++++++++++++++++++- .../web/controller/CategorieController.java | 31 ++++++++++++- .../parametrage/categories_form.html | 4 ++ .../parametrage/categories_list.html | 3 ++ 4 files changed, 81 insertions(+), 3 deletions(-) diff --git a/as-talange-core/src/main/java/com/astalange/core/service/CategorieService.java b/as-talange-core/src/main/java/com/astalange/core/service/CategorieService.java index 9110bb5..2f91684 100644 --- a/as-talange-core/src/main/java/com/astalange/core/service/CategorieService.java +++ b/as-talange-core/src/main/java/com/astalange/core/service/CategorieService.java @@ -149,7 +149,18 @@ public class CategorieService { newDotation.setTaille(""); newDotation.setNumero(""); if (licence.getAdherent() != null) { - newDotation.setFlocage(licence.getAdherent().getNom().toUpperCase() + " " + licence.getAdherent().getPrenom()); + String defaultFlocage = ""; + if (ce.getEquipement().isHasFlocagePrenom() && ce.getEquipement().isHasFlocageInitiales()) { + String firstInitial = licence.getAdherent().getNom() != null && !licence.getAdherent().getNom().isEmpty() ? licence.getAdherent().getNom().substring(0, 1).toUpperCase() + "." : ""; + defaultFlocage = (licence.getAdherent().getPrenom() != null ? licence.getAdherent().getPrenom() + " " : "") + firstInitial; + } else if (ce.getEquipement().isHasFlocagePrenom()) { + defaultFlocage = licence.getAdherent().getPrenom() != null ? licence.getAdherent().getPrenom() : ""; + } else if (ce.getEquipement().isHasFlocageInitiales()) { + String firstInitP = licence.getAdherent().getPrenom() != null && !licence.getAdherent().getPrenom().isEmpty() ? licence.getAdherent().getPrenom().substring(0, 1).toUpperCase() : ""; + String firstInitN = licence.getAdherent().getNom() != null && !licence.getAdherent().getNom().isEmpty() ? licence.getAdherent().getNom().substring(0, 1).toUpperCase() : ""; + defaultFlocage = firstInitP + firstInitN; + } + newDotation.setFlocage(defaultFlocage.trim()); } currentDotations.add(newDotation); } @@ -157,4 +168,37 @@ public class CategorieService { licenceRepository.save(licence); } + + @Transactional + public Categorie duplicateCategorie(Long id) { + Categorie existing = categorieRepository.findById(id) + .orElseThrow(() -> new IllegalArgumentException("Invalid category ID: " + id)); + + Categorie duplicated = new Categorie(); + // Appending ' (dupliqué)' and truncating if necessary since nom is max 20 chars + String newNom = existing.getNom() + " (dupliqué)"; + if (newNom.length() > 20) { + newNom = newNom.substring(0, 20); + } + duplicated.setNom(newNom); + duplicated.setSexe(existing.getSexe()); + duplicated.setAnneeMin(existing.getAnneeMin()); + duplicated.setAnneeMax(existing.getAnneeMax()); + duplicated.setTarifBase(existing.getTarifBase()); + duplicated.setTarifExterieur(existing.getTarifExterieur()); + duplicated.setSaison(existing.getSaison()); + + List nouveauxEquipements = new ArrayList<>(); + for (CategorieEquipement ce : existing.getCategorieEquipements()) { + CategorieEquipement newCe = new CategorieEquipement(); + newCe.setCategorie(duplicated); + newCe.setEquipement(ce.getEquipement()); + newCe.setObligatoire(ce.getObligatoire()); + newCe.setPrix(ce.getPrix() != null ? ce.getPrix() : java.math.BigDecimal.ZERO); + nouveauxEquipements.add(newCe); + } + duplicated.getCategorieEquipements().addAll(nouveauxEquipements); + + return categorieRepository.save(duplicated); + } } diff --git a/as-talange-web/src/main/java/com/astalange/web/controller/CategorieController.java b/as-talange-web/src/main/java/com/astalange/web/controller/CategorieController.java index 8a95518..2d3c244 100644 --- a/as-talange-web/src/main/java/com/astalange/web/controller/CategorieController.java +++ b/as-talange-web/src/main/java/com/astalange/web/controller/CategorieController.java @@ -36,7 +36,9 @@ public class CategorieController { @GetMapping("/new") public String showCreateForm(Model model) { - model.addAttribute("categorie", new Categorie()); + if (!model.containsAttribute("categorie")) { + model.addAttribute("categorie", new Categorie()); + } Saison activeSaison = saisonRepository.findByEstActiveTrue().orElse(null); model.addAttribute("allEquipements", activeSaison != null ? equipementRepository.findBySaison(activeSaison) : List.of()); return "parametrage/categories_form"; @@ -59,7 +61,26 @@ public class CategorieController { @PostMapping public String saveCategorie(@ModelAttribute("categorie") Categorie categorie, - jakarta.servlet.http.HttpServletRequest request) { + jakarta.servlet.http.HttpServletRequest request, + org.springframework.web.servlet.mvc.support.RedirectAttributes redirectAttributes) { + + Saison activeSaison; + if (categorie.getId() == null) { + activeSaison = saisonRepository.findByEstActiveTrue().orElse(null); + } else { + Categorie existing = categorieRepository.findById(categorie.getId()).orElse(null); + activeSaison = existing != null ? existing.getSaison() : null; + } + + if (activeSaison != null) { + for (Categorie c : categorieRepository.findBySaison(activeSaison)) { + if (c.getNom().equalsIgnoreCase(categorie.getNom()) && (categorie.getId() == null || !c.getId().equals(categorie.getId()))) { + redirectAttributes.addFlashAttribute("errorMessage", "Erreur : La catégorie '" + categorie.getNom() + "' existe déjà pour cette saison."); + redirectAttributes.addFlashAttribute("categorie", categorie); + return categorie.getId() == null ? "redirect:/categories/new" : "redirect:/categories/" + categorie.getId() + "/edit"; + } + } + } java.util.Map configs = new java.util.HashMap<>(); @@ -93,4 +114,10 @@ public class CategorieController { categorieRepository.delete(categorie); return "redirect:/categories"; } + + @PostMapping("/{id}/duplicate") + public String duplicateCategorie(@PathVariable Long id) { + categorieService.duplicateCategorie(id); + return "redirect:/categories"; + } } diff --git a/as-talange-web/src/main/resources/templates/parametrage/categories_form.html b/as-talange-web/src/main/resources/templates/parametrage/categories_form.html index f3ce961..793ff62 100644 --- a/as-talange-web/src/main/resources/templates/parametrage/categories_form.html +++ b/as-talange-web/src/main/resources/templates/parametrage/categories_form.html @@ -22,6 +22,10 @@
+
+

+
+
diff --git a/as-talange-web/src/main/resources/templates/parametrage/categories_list.html b/as-talange-web/src/main/resources/templates/parametrage/categories_list.html index 22882cf..10545e7 100644 --- a/as-talange-web/src/main/resources/templates/parametrage/categories_list.html +++ b/as-talange-web/src/main/resources/templates/parametrage/categories_list.html @@ -56,6 +56,9 @@ 130.00 € Modifier + + +