package fr.eni.enchere.controllers; import fr.eni.enchere.bo.UserProfil; import org.springframework.ui.Model; import fr.eni.enchere.bll.UserService; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; @Controller() @RequestMapping("/profil") public class ProfilController { private final UserService userService; public ProfilController(UserService userService) { this.userService = userService; } @GetMapping public String viewProfile(Model model) { // Obtenez l'authentification actuelle Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); // Vérifiez si l'utilisateur est authentifié if (!authentication.getName().equals("anonymousUser")) { // Obtenez les détails de l'utilisateur authentifié String username = authentication.getName(); // Utilisez le service approprié pour récupérer les informations de l'utilisateur à partir du nom d'utilisateur UserProfil userProfile = userService.utilisateurByName(username); // Ajoutez les informations du profil à l'objet Model pour les afficher dans la page HTML // model.addAttribute("user", new UserProfil()); model.addAttribute("userProfile", userProfile); return "profil"; }else { return "accueil"; } } @GetMapping("/edit") public String edit(Model model) { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); if (!authentication.getName().equals("anonymousUser")) { String username = authentication.getName(); UserProfil userProfile = userService.utilisateurByName(username); model.addAttribute("userProfile", userProfile); } return "editProfil"; } @PostMapping("/edit") public String editProfile(Model model) { // Obtenez l'authentification actuelle Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); // Vérifiez si l'utilisateur est authentifié if (!authentication.getName().equals("anonymousUser")) { // Obtenez les détails de l'utilisateur authentifié String username = authentication.getName(); // Utilisez le service approprié pour récupérer les informations de l'utilisateur à partir du nom d'utilisateur UserProfil userProfile = userService.utilisateurByName(username); // Ajoutez les informations du profil à l'objet Model pour les afficher dans la page HTML // model.addAttribute("user", new UserProfil()); model.addAttribute("userProfile", userProfile); return "editProfil"; }else { return "accueil"; } } @PostMapping("/delete") public String deleteUser() { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); // Obtenez les détails de l'utilisateur authentifié String username = authentication.getName(); // Utilisez le service approprié pour récupérer les informations de l'utilisateur à partir du nom d'utilisateur UserProfil userProfile = userService.utilisateurByName(username); System.out.println(userProfile.getId()); //Supprimer le compte userService.deleteUtilisateur(userProfile.getId()); //ATTENTION AJOUTER LA DECONNEXION return "redirect:/accueil"; } }