FEAT : ajout partage simple

This commit is contained in:
2025-12-01 22:30:22 +01:00
parent 4a35c375ec
commit 109cc8065e
14 changed files with 169 additions and 29 deletions

View File

@@ -1,8 +1,11 @@
package local.epul4a.fotosharing.controller;
import local.epul4a.fotosharing.model.Partage;
import local.epul4a.fotosharing.model.Photo;
import local.epul4a.fotosharing.model.Utilisateur;
import local.epul4a.fotosharing.repository.PartageRepository;
import local.epul4a.fotosharing.repository.PhotoRepository;
import local.epul4a.fotosharing.repository.UtilisateurRepository;
import local.epul4a.fotosharing.security.CustomUserDetails;
import local.epul4a.fotosharing.service.CommentaireService;
import local.epul4a.fotosharing.service.PhotoService;
@@ -11,6 +14,7 @@ import org.springframework.core.io.PathResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.stereotype.Controller;
@@ -20,6 +24,7 @@ import org.springframework.web.multipart.MultipartFile;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
@Controller
public class PhotoController {
@@ -27,11 +32,15 @@ public class PhotoController {
private final PhotoService photoService;
private final PhotoRepository photoRepository;
private final CommentaireService commentaireService;
private final UtilisateurRepository utilisateurRepository;
private final PartageRepository partageRepository;
public PhotoController(PhotoService photoService, PhotoRepository photoRepository, CommentaireService commentaireService) {
public PhotoController(PhotoService photoService, PhotoRepository photoRepository, CommentaireService commentaireService, UtilisateurRepository utilisateurRepository, PartageRepository partageRepository) {
this.photoService = photoService;
this.photoRepository = photoRepository;
this.commentaireService = commentaireService;
this.utilisateurRepository = utilisateurRepository;
this.partageRepository = partageRepository;
}
@GetMapping("/upload")
@@ -84,7 +93,13 @@ public class PhotoController {
@GetMapping("/mes-photos")
public String mesPhotos(Model model, Authentication authentication) {
String email = authentication.getName();
model.addAttribute("photos", photoService.listByOwner(email));
// photos que je possède
List<Photo> mesPhotos = photoService.listByOwner(email);
// photos partagées avec moi
List<Photo> photosPartagees = photoService.listSharedWith(email);
model.addAttribute("mesPhotos", mesPhotos);
model.addAttribute("photosPartagees", photosPartagees);
return "mes-photos";
}
@@ -95,6 +110,7 @@ public class PhotoController {
}
@GetMapping("/photo/{id}")
@PreAuthorize("@securityService.canAccessPhoto(authentication, #id)")
public String viewPhoto(@PathVariable Long id,
Model model,
Authentication auth) {
@@ -126,5 +142,30 @@ public class PhotoController {
return "redirect:/photo/" + id;
}
@PostMapping("/photo/{id}/share")
@PreAuthorize("@securityService.canAccessPhoto(authentication, #id)")
public String share(@PathVariable Long id,
@RequestParam String email,
Authentication auth) {
// Vérifier que c'est le propriétaire
Photo photo = photoRepository.findById(id).orElse(null);
if (photo == null) return "redirect:/galerie";
if (!photo.getProprietaire().getEmail().equals(auth.getName())) {
return "redirect:/photo/" + id + "?error=notowner";
}
// Trouver utilisateur
Utilisateur user = utilisateurRepository.findByEmail(email).orElse(null);
if (user == null) {
return "redirect:/photo/" + id + "?error=usernotfound";
}
// Ajouter partage
Partage p = new Partage();
p.setPhoto(photo);
p.setUtilisateur(user);
partageRepository.save(p);
return "redirect:/photo/" + id + "?shared=ok";
}
}