diff --git a/src/main/java/local/epul4a/fotosharing/controller/PhotoController.java b/src/main/java/local/epul4a/fotosharing/controller/PhotoController.java index 1fab429..b69d74c 100644 --- a/src/main/java/local/epul4a/fotosharing/controller/PhotoController.java +++ b/src/main/java/local/epul4a/fotosharing/controller/PhotoController.java @@ -4,6 +4,7 @@ import local.epul4a.fotosharing.model.Photo; import local.epul4a.fotosharing.model.Utilisateur; import local.epul4a.fotosharing.repository.PhotoRepository; import local.epul4a.fotosharing.security.CustomUserDetails; +import local.epul4a.fotosharing.service.CommentaireService; import local.epul4a.fotosharing.service.PhotoService; import org.springframework.core.io.Resource; import org.springframework.core.io.PathResource; @@ -25,10 +26,12 @@ public class PhotoController { private final PhotoService photoService; private final PhotoRepository photoRepository; + private final CommentaireService commentaireService; - public PhotoController(PhotoService photoService, PhotoRepository photoRepository) { + public PhotoController(PhotoService photoService, PhotoRepository photoRepository, CommentaireService commentaireService) { this.photoService = photoService; this.photoRepository = photoRepository; + this.commentaireService = commentaireService; } @GetMapping("/upload") @@ -91,6 +94,37 @@ public class PhotoController { return "galerie"; } + @GetMapping("/photo/{id}") + public String viewPhoto(@PathVariable Long id, + Model model, + Authentication auth) { + + Photo photo = photoRepository.findById(id).orElse(null); + if (photo == null) { + return "redirect:/galerie"; + } + + model.addAttribute("photo", photo); + model.addAttribute("commentaires", commentaireService.listByPhoto(id)); + + // utilisateur connecté (peut être null si visiteur) + String currentUser = (auth != null ? auth.getName() : null); + model.addAttribute("currentUser", currentUser); + + return "photo-detail"; + } + + @PostMapping("/photo/{id}/comment") + public String addComment(@PathVariable Long id, + @RequestParam String contenu, + Authentication auth) { + if (auth == null) { + return "redirect:/login"; + } + String email = auth.getName(); + commentaireService.addComment(id, email, contenu); + return "redirect:/photo/" + id; + } } diff --git a/src/main/java/local/epul4a/fotosharing/repository/CommentaireRepository.java b/src/main/java/local/epul4a/fotosharing/repository/CommentaireRepository.java new file mode 100644 index 0000000..bd75032 --- /dev/null +++ b/src/main/java/local/epul4a/fotosharing/repository/CommentaireRepository.java @@ -0,0 +1,11 @@ +package local.epul4a.fotosharing.repository; + +import local.epul4a.fotosharing.model.Commentaire; +import org.springframework.data.jpa.repository.JpaRepository; +import java.util.List; + +public interface CommentaireRepository extends JpaRepository { + + List findByPhoto_IdOrderByDateCommentaireAsc(Long photoId); + +} diff --git a/src/main/java/local/epul4a/fotosharing/security/SecurityConfig.java b/src/main/java/local/epul4a/fotosharing/security/SecurityConfig.java index 24174e9..eeb7f43 100644 --- a/src/main/java/local/epul4a/fotosharing/security/SecurityConfig.java +++ b/src/main/java/local/epul4a/fotosharing/security/SecurityConfig.java @@ -39,7 +39,7 @@ public class SecurityConfig { http .userDetailsService(customUserDetailsService) // Utiliser directement le UserDetailsService .authorizeHttpRequests(auth -> auth - .requestMatchers("/register", "/login", "/css/**", "/js/**", "/photo/*/raw", "/galerie").permitAll() + .requestMatchers("/register", "/login", "/css/**", "/js/**", "/galerie", "/photo/*", "/photo/*/raw").permitAll() .anyRequest().authenticated() ) .formLogin(form -> form diff --git a/src/main/java/local/epul4a/fotosharing/service/CommentaireService.java b/src/main/java/local/epul4a/fotosharing/service/CommentaireService.java new file mode 100644 index 0000000..b186355 --- /dev/null +++ b/src/main/java/local/epul4a/fotosharing/service/CommentaireService.java @@ -0,0 +1,9 @@ +package local.epul4a.fotosharing.service; + +import local.epul4a.fotosharing.model.Commentaire; +import java.util.List; + +public interface CommentaireService { + List listByPhoto(Long photoId); + void addComment(Long photoId, String email, String contenu); +} diff --git a/src/main/java/local/epul4a/fotosharing/service/impl/CommentaireServiceImpl.java b/src/main/java/local/epul4a/fotosharing/service/impl/CommentaireServiceImpl.java new file mode 100644 index 0000000..12c21d7 --- /dev/null +++ b/src/main/java/local/epul4a/fotosharing/service/impl/CommentaireServiceImpl.java @@ -0,0 +1,50 @@ +package local.epul4a.fotosharing.service.impl; + +import local.epul4a.fotosharing.model.Commentaire; +import local.epul4a.fotosharing.model.Photo; +import local.epul4a.fotosharing.model.Utilisateur; +import local.epul4a.fotosharing.repository.CommentaireRepository; +import local.epul4a.fotosharing.repository.PhotoRepository; +import local.epul4a.fotosharing.repository.UtilisateurRepository; +import local.epul4a.fotosharing.service.CommentaireService; +import org.springframework.stereotype.Service; + +import java.time.LocalDateTime; +import java.util.List; + +@Service +public class CommentaireServiceImpl implements CommentaireService { + + private final CommentaireRepository commentaireRepository; + private final PhotoRepository photoRepository; + private final UtilisateurRepository utilisateurRepository; + + public CommentaireServiceImpl(CommentaireRepository commentaireRepository, + PhotoRepository photoRepository, + UtilisateurRepository utilisateurRepository) { + this.commentaireRepository = commentaireRepository; + this.photoRepository = photoRepository; + this.utilisateurRepository = utilisateurRepository; + } + + @Override + public List listByPhoto(Long photoId) { + return commentaireRepository.findByPhoto_IdOrderByDateCommentaireAsc(photoId); + } + + @Override + public void addComment(Long photoId, String email, String contenu) { + Photo photo = photoRepository.findById(photoId) + .orElseThrow(() -> new RuntimeException("Photo introuvable")); + Utilisateur user = utilisateurRepository.findByEmail(email) + .orElseThrow(() -> new RuntimeException("Utilisateur introuvable")); + + Commentaire c = new Commentaire(); + c.setPhoto(photo); + c.setAuteur(user); + c.setContenu(contenu); + c.setDateCommentaire(LocalDateTime.now()); + + commentaireRepository.save(c); + } +} diff --git a/src/main/resources/templates/galerie.html b/src/main/resources/templates/galerie.html index c1cf7ad..ca224d8 100644 --- a/src/main/resources/templates/galerie.html +++ b/src/main/resources/templates/galerie.html @@ -10,7 +10,7 @@
  • nom — - Voir + Voir
  • diff --git a/src/main/resources/templates/mes-photos.html b/src/main/resources/templates/mes-photos.html index ebaac9a..860c13c 100644 --- a/src/main/resources/templates/mes-photos.html +++ b/src/main/resources/templates/mes-photos.html @@ -18,7 +18,7 @@
  • Nom du fichier — - Voir + Voir
  • diff --git a/src/main/resources/templates/photo-detail.html b/src/main/resources/templates/photo-detail.html new file mode 100644 index 0000000..cf951ff --- /dev/null +++ b/src/main/resources/templates/photo-detail.html @@ -0,0 +1,62 @@ + + + + + Détail de la photo + + + +

    Détail de la photo

    + +

    + Accueil | + Galerie publique | + Mes photos +

    + + +
    + Photo +
    + + +
      +
    • Nom original :
    • +
    • Date upload :
    • +
    • Visibilité :
    • +
    • Propriétaire :
    • +
    + + +

    Commentaires

    +
    +

    Aucun commentaire pour l'instant.

    +
    +
      +
    • + Auteur : + + () +
    • +
    + +
    +

    Ajouter un commentaire

    +
    +
    + +
    +
    + +
    +

    Connectez-vous pour commenter.

    +
    + +

    + Voir en grande taille +

    + + + diff --git a/target/classes/local/epul4a/fotosharing/controller/PhotoController.class b/target/classes/local/epul4a/fotosharing/controller/PhotoController.class index d703464..dcde4f9 100644 Binary files a/target/classes/local/epul4a/fotosharing/controller/PhotoController.class and b/target/classes/local/epul4a/fotosharing/controller/PhotoController.class differ diff --git a/target/classes/local/epul4a/fotosharing/repository/CommentaireRepository.class b/target/classes/local/epul4a/fotosharing/repository/CommentaireRepository.class new file mode 100644 index 0000000..da552e8 Binary files /dev/null and b/target/classes/local/epul4a/fotosharing/repository/CommentaireRepository.class differ diff --git a/target/classes/local/epul4a/fotosharing/security/SecurityConfig.class b/target/classes/local/epul4a/fotosharing/security/SecurityConfig.class index 2c8357c..7813d21 100644 Binary files a/target/classes/local/epul4a/fotosharing/security/SecurityConfig.class and b/target/classes/local/epul4a/fotosharing/security/SecurityConfig.class differ diff --git a/target/classes/local/epul4a/fotosharing/service/CommentaireService.class b/target/classes/local/epul4a/fotosharing/service/CommentaireService.class new file mode 100644 index 0000000..86c900f Binary files /dev/null and b/target/classes/local/epul4a/fotosharing/service/CommentaireService.class differ diff --git a/target/classes/local/epul4a/fotosharing/service/impl/CommentaireServiceImpl.class b/target/classes/local/epul4a/fotosharing/service/impl/CommentaireServiceImpl.class new file mode 100644 index 0000000..47c9dbf Binary files /dev/null and b/target/classes/local/epul4a/fotosharing/service/impl/CommentaireServiceImpl.class differ diff --git a/target/classes/templates/galerie.html b/target/classes/templates/galerie.html index c1cf7ad..ca224d8 100644 --- a/target/classes/templates/galerie.html +++ b/target/classes/templates/galerie.html @@ -10,7 +10,7 @@
  • nom — - Voir + Voir
  • diff --git a/target/classes/templates/mes-photos.html b/target/classes/templates/mes-photos.html index ebaac9a..860c13c 100644 --- a/target/classes/templates/mes-photos.html +++ b/target/classes/templates/mes-photos.html @@ -18,7 +18,7 @@
  • Nom du fichier — - Voir + Voir
  • diff --git a/target/classes/templates/photo-detail.html b/target/classes/templates/photo-detail.html new file mode 100644 index 0000000..cf951ff --- /dev/null +++ b/target/classes/templates/photo-detail.html @@ -0,0 +1,62 @@ + + + + + Détail de la photo + + + +

    Détail de la photo

    + +

    + Accueil | + Galerie publique | + Mes photos +

    + + +
    + Photo +
    + + +
      +
    • Nom original :
    • +
    • Date upload :
    • +
    • Visibilité :
    • +
    • Propriétaire :
    • +
    + + +

    Commentaires

    +
    +

    Aucun commentaire pour l'instant.

    +
    +
      +
    • + Auteur : + + () +
    • +
    + +
    +

    Ajouter un commentaire

    +
    +
    + +
    +
    + +
    +

    Connectez-vous pour commenter.

    +
    + +

    + Voir en grande taille +

    + + +