Files
as-talange/as-talange-web/src/main/java/com/astalange/web/controller/AdherentController.java
T

93 lines
4.0 KiB
Java

package com.astalange.web.controller;
import com.astalange.core.entity.Adherent;
import com.astalange.core.entity.Saison;
import com.astalange.core.repository.AdherentRepository;
import com.astalange.core.repository.CategorieRepository;
import com.astalange.core.repository.SaisonRepository;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller
@RequestMapping("/adherents")
public class AdherentController {
private final AdherentRepository adherentRepository;
private final CategorieRepository categorieRepository;
private final com.astalange.core.repository.LicenceRepository licenceRepository;
private final com.astalange.core.repository.ModePaiementRepository modePaiementRepository;
private final SaisonRepository saisonRepository;
public AdherentController(AdherentRepository adherentRepository, CategorieRepository categorieRepository, com.astalange.core.repository.LicenceRepository licenceRepository, com.astalange.core.repository.ModePaiementRepository modePaiementRepository, SaisonRepository saisonRepository) {
this.adherentRepository = adherentRepository;
this.categorieRepository = categorieRepository;
this.licenceRepository = licenceRepository;
this.modePaiementRepository = modePaiementRepository;
this.saisonRepository = saisonRepository;
}
@GetMapping
public String listAdherents(@org.springframework.web.bind.annotation.RequestParam(required = false) String query, Model model) {
List<Adherent> adherents;
if (query != null && !query.trim().isEmpty()) {
adherents = adherentRepository.searchByQuery(query.trim());
model.addAttribute("searchQuery", query.trim());
} else {
adherents = adherentRepository.findAll();
}
model.addAttribute("adherents", adherents);
return "adherents/list";
}
@GetMapping("/new")
public String showCreateForm(Model model) {
model.addAttribute("adherent", new Adherent());
return "adherents/form";
}
@org.springframework.web.bind.annotation.PostMapping
public String saveAdherent(
@jakarta.validation.Valid @org.springframework.web.bind.annotation.ModelAttribute("adherent") Adherent adherent,
org.springframework.validation.BindingResult result,
Model model) {
if (result.hasErrors()) {
return "adherents/form";
}
adherentRepository.save(adherent);
return "redirect:/adherents";
}
@GetMapping("/{id}/edit")
public String editAdherent(@org.springframework.web.bind.annotation.PathVariable Long id, Model model) {
Adherent adherent = adherentRepository.findById(id)
.orElseThrow(() -> new IllegalArgumentException("Adhérent invalide : " + id));
Saison saisonActive = saisonRepository.findByEstActiveTrue().orElse(null);
if (saisonActive != null) {
model.addAttribute("categories", categorieRepository.findBySaison(saisonActive));
model.addAttribute("saisonActive", saisonActive);
} else {
model.addAttribute("categories", categorieRepository.findAll());
}
model.addAttribute("adherent", adherent);
model.addAttribute("licences", licenceRepository.findByAdherentId(id));
model.addAttribute("modesPaiement", modePaiementRepository.findAll());
return "adherents/form";
}
@org.springframework.web.bind.annotation.PostMapping("/{id}/delete")
public String deleteAdherent(@org.springframework.web.bind.annotation.PathVariable Long id) {
Adherent adherent = adherentRepository.findById(id)
.orElseThrow(() -> new IllegalArgumentException("Adhérent invalide : " + id));
adherentRepository.delete(adherent);
return "redirect:/adherents";
}
}