diff --git a/src/main/java/com/projet/projetIndu/controllers/PatientController.java b/src/main/java/com/projet/projetIndu/controllers/PatientController.java index e683cf2aec6cf6d9950c20c59b6b7bf5bc80be52..53746e3ad41dfe6d24c9356e50075b2e0010adc4 100644 --- a/src/main/java/com/projet/projetIndu/controllers/PatientController.java +++ b/src/main/java/com/projet/projetIndu/controllers/PatientController.java @@ -1,4 +1,68 @@ package com.projet.projetIndu.controllers; +import com.projet.projetIndu.entities.Patient; +import com.projet.projetIndu.services.PatientService; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.*; + +import java.util.List; +import java.util.Optional; + +@RequestMapping("/patients") +@Controller public class PatientController { + + private final PatientService patientService; + + public PatientController(PatientService patientService) { + this.patientService = patientService; + } + + // Afficher tous les patients + @GetMapping + public String listPatients(Model model) { + List<Patient> patients = patientService.getAllPatients(); + model.addAttribute("patients", patients); + return "patients"; // Page HTML contenant la liste des patients + } + + //Afficher le formulaire de création d'un patient + @GetMapping("/create") + public String showCreateForm(Model model) { + model.addAttribute("patient", new Patient()); + return "create-patient"; // Page HTML pour ajouter un patient + } + + // Enregistrer un nouveau patient + @PostMapping + public String createPatient(@ModelAttribute Patient patient) { + patientService.savePatient(patient); + return "redirect:/patients"; + } + + // Rechercher un patient par ID + @GetMapping("/{id}") + public String getPatientById(@PathVariable Long id, Model model) { + Optional<Patient> patient = patientService.getPatientById(id); + if (patient.isPresent()) { + model.addAttribute("patient", patient.get()); + return "patient-details"; // Page HTML avec les détails du patient + } else { + return "redirect:/patients?error=notfound"; + } + } + + //Supprimer un patient par ID + @PostMapping("/{id}/delete") + public String deletePatient(@PathVariable Long id) { + patientService.deletePatientById(id); + return "redirect:/patients"; + } + + // Afficher le tableau de bord du patient + @GetMapping("/dashboard") + public String showPatientDashboard(Model model) { + return "patient-dashboard"; // Page HTML du tableau de bord patient + } }