feat: add category duplication functionality
This commit is contained in:
@@ -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<Long, com.astalange.core.service.CategorieService.EquipementConfig> 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";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,6 +22,10 @@
|
||||
<form th:action="@{/categories}" th:object="${categorie}" method="post" class="space-y-6">
|
||||
<input type="hidden" th:field="*{id}" />
|
||||
|
||||
<div th:if="${errorMessage}" class="bg-red-50 border-l-4 border-red-500 p-4 mb-6">
|
||||
<p class="text-sm text-red-700" th:text="${errorMessage}"></p>
|
||||
</div>
|
||||
|
||||
<div class="grid grid-cols-2 gap-4">
|
||||
<div>
|
||||
<label class="block text-sm font-medium text-gray-700 mb-1">Nom (ex: U15)</label>
|
||||
|
||||
@@ -56,6 +56,9 @@
|
||||
<td class="py-4 px-6 text-right font-medium text-purple-600" th:text="${cat.tarifExterieur != null ? cat.tarifExterieur + ' €' : '-'}">130.00 €</td>
|
||||
<td class="py-4 px-6 text-right flex justify-end space-x-3 items-center">
|
||||
<a th:href="@{/categories/{id}/edit(id=${cat.id})}" class="text-indigo-600 hover:text-indigo-900 font-medium bg-indigo-50 px-3 py-1 rounded-lg">Modifier</a>
|
||||
<form th:action="@{/categories/{id}/duplicate(id=${cat.id})}" method="post">
|
||||
<button type="submit" class="text-green-600 hover:text-green-800 font-medium bg-green-50 px-3 py-1 rounded-lg">Dupliquer</button>
|
||||
</form>
|
||||
<form th:action="@{/categories/{id}/delete(id=${cat.id})}" method="post" onsubmit="return confirm('Supprimer cette catégorie ?');">
|
||||
<button type="submit" class="text-red-600 hover:text-red-800 font-medium">Supprimer</button>
|
||||
</form>
|
||||
|
||||
Reference in New Issue
Block a user