feat(backend): Ajout complet des fonctionnalités Chat, Social, Story et Notifications
Implémentation complète de toutes les fonctionnalités backend : ## Nouvelles Fonctionnalités ### Chat (Messagerie Instantanée) - Entities : Conversation, Message - DTOs : ConversationResponseDTO, MessageResponseDTO, SendMessageRequestDTO - Resources : MessageResource (endpoints REST) - Services : MessageService (logique métier) - Repositories : ConversationRepository, MessageRepository - WebSocket : ChatWebSocket (temps réel) ### Social (Publications Sociales) - Entities : SocialPost, SocialComment, SocialLike - DTOs : SocialPostResponseDTO, CreateSocialPostRequestDTO - Resources : SocialPostResource - Services : SocialPostService - Repositories : SocialPostRepository ### Story (Stories temporaires) - Entities : Story, StoryView - DTOs : StoryResponseDTO, CreateStoryRequestDTO - Resources : StoryResource - Services : StoryService - Repositories : StoryRepository ### Notifications (Temps Réel) - Entities : Notification - DTOs : NotificationResponseDTO - Resources : NotificationResource - Services : NotificationService, PresenceService - Repositories : NotificationRepository - WebSocket : NotificationWebSocket (temps réel) ## Améliorations ### Users & Friendship - Mise à jour UserResponseDTO avec nouveaux champs - Amélioration FriendshipResource avec séparation demandes envoyées/reçues - FriendSuggestionResponseDTO pour suggestions d'amis - Optimisations dans UsersService et FriendshipService ### Events - Améliorations EventsResource et EventService - Optimisations EventsRepository ### Configuration - Mise à jour application.properties - Configuration docker-compose.yml - Dockerfile pour développement ## Fichiers Modifiés - .dockerignore, .gitignore - README.md - docker-compose.yml - Configuration Maven wrapper
This commit is contained in:
308
src/main/java/com/lions/dev/service/SocialPostService.java
Normal file
308
src/main/java/com/lions/dev/service/SocialPostService.java
Normal file
@@ -0,0 +1,308 @@
|
||||
package com.lions.dev.service;
|
||||
|
||||
import com.lions.dev.entity.friends.Friendship;
|
||||
import com.lions.dev.entity.friends.FriendshipStatus;
|
||||
import com.lions.dev.entity.social.SocialPost;
|
||||
import com.lions.dev.entity.users.Users;
|
||||
import com.lions.dev.exception.UserNotFoundException;
|
||||
import com.lions.dev.repository.FriendshipRepository;
|
||||
import com.lions.dev.repository.SocialPostRepository;
|
||||
import com.lions.dev.repository.UsersRepository;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.transaction.Transactional;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Service de gestion des posts sociaux.
|
||||
*
|
||||
* Ce service contient la logique métier pour la création, récupération,
|
||||
* mise à jour et suppression des posts sociaux.
|
||||
*
|
||||
* Tous les logs nécessaires pour la traçabilité sont intégrés.
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class SocialPostService {
|
||||
|
||||
@Inject
|
||||
SocialPostRepository socialPostRepository;
|
||||
|
||||
@Inject
|
||||
UsersRepository usersRepository;
|
||||
|
||||
@Inject
|
||||
FriendshipRepository friendshipRepository;
|
||||
|
||||
@Inject
|
||||
NotificationService notificationService;
|
||||
|
||||
/**
|
||||
* Récupère tous les posts avec pagination.
|
||||
*
|
||||
* @param page Le numéro de la page (0-indexé)
|
||||
* @param size La taille de la page
|
||||
* @return Liste paginée des posts
|
||||
*/
|
||||
public List<SocialPost> getAllPosts(int page, int size) {
|
||||
System.out.println("[LOG] Récupération de tous les posts (page: " + page + ", size: " + size + ")");
|
||||
return socialPostRepository.findAllWithPagination(page, size);
|
||||
}
|
||||
|
||||
/**
|
||||
* Récupère tous les posts d'un utilisateur.
|
||||
*
|
||||
* @param userId L'ID de l'utilisateur
|
||||
* @return Liste des posts de l'utilisateur
|
||||
* @throws UserNotFoundException Si l'utilisateur n'existe pas
|
||||
*/
|
||||
public List<SocialPost> getPostsByUserId(UUID userId) {
|
||||
System.out.println("[LOG] Récupération des posts pour l'utilisateur ID : " + userId);
|
||||
|
||||
Users user = usersRepository.findById(userId);
|
||||
if (user == null) {
|
||||
System.out.println("[ERROR] Utilisateur non trouvé avec l'ID : " + userId);
|
||||
throw new UserNotFoundException("Utilisateur non trouvé avec l'ID : " + userId);
|
||||
}
|
||||
|
||||
return socialPostRepository.findByUserId(userId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Crée un nouveau post social.
|
||||
*
|
||||
* @param content Le contenu du post
|
||||
* @param userId L'ID de l'utilisateur créateur
|
||||
* @param imageUrl L'URL de l'image (optionnel)
|
||||
* @return Le post créé
|
||||
* @throws UserNotFoundException Si l'utilisateur n'existe pas
|
||||
*/
|
||||
@Transactional
|
||||
public SocialPost createPost(String content, UUID userId, String imageUrl) {
|
||||
System.out.println("[LOG] Création d'un post par l'utilisateur ID : " + userId);
|
||||
|
||||
Users user = usersRepository.findById(userId);
|
||||
if (user == null) {
|
||||
System.out.println("[ERROR] Utilisateur non trouvé avec l'ID : " + userId);
|
||||
throw new UserNotFoundException("Utilisateur non trouvé avec l'ID : " + userId);
|
||||
}
|
||||
|
||||
SocialPost post = new SocialPost(content, user);
|
||||
if (imageUrl != null && !imageUrl.isEmpty()) {
|
||||
post.setImageUrl(imageUrl);
|
||||
}
|
||||
|
||||
socialPostRepository.persist(post);
|
||||
System.out.println("[LOG] Post créé avec succès : " + post.getId());
|
||||
|
||||
// Créer des notifications pour tous les amis
|
||||
try {
|
||||
List<Friendship> friendships = friendshipRepository.findFriendsByUser(user, 0, Integer.MAX_VALUE);
|
||||
String userName = user.getPrenoms() + " " + user.getNom();
|
||||
|
||||
for (Friendship friendship : friendships) {
|
||||
Users friend = friendship.getUser().equals(user)
|
||||
? friendship.getFriend()
|
||||
: friendship.getUser();
|
||||
|
||||
String notificationTitle = "Nouveau post de " + userName;
|
||||
String notificationMessage = userName + " a publié un nouveau post : " +
|
||||
(content.length() > 50 ? content.substring(0, 50) + "..." : content);
|
||||
|
||||
notificationService.createNotification(
|
||||
notificationTitle,
|
||||
notificationMessage,
|
||||
"post",
|
||||
friend.getId(),
|
||||
null
|
||||
);
|
||||
}
|
||||
System.out.println("[LOG] Notifications créées pour " + friendships.size() + " ami(s)");
|
||||
} catch (Exception e) {
|
||||
System.out.println("[ERROR] Erreur lors de la création des notifications : " + e.getMessage());
|
||||
}
|
||||
|
||||
return post;
|
||||
}
|
||||
|
||||
/**
|
||||
* Récupère un post par son ID.
|
||||
*
|
||||
* @param postId L'ID du post
|
||||
* @return Le post trouvé
|
||||
*/
|
||||
public SocialPost getPostById(UUID postId) {
|
||||
System.out.println("[LOG] Récupération du post ID : " + postId);
|
||||
|
||||
SocialPost post = socialPostRepository.findById(postId);
|
||||
if (post == null) {
|
||||
System.out.println("[ERROR] Post non trouvé avec l'ID : " + postId);
|
||||
throw new IllegalArgumentException("Post non trouvé avec l'ID : " + postId);
|
||||
}
|
||||
|
||||
return post;
|
||||
}
|
||||
|
||||
/**
|
||||
* Met à jour un post.
|
||||
*
|
||||
* @param postId L'ID du post
|
||||
* @param content Le nouveau contenu
|
||||
* @param imageUrl La nouvelle URL d'image (optionnel)
|
||||
* @return Le post mis à jour
|
||||
*/
|
||||
@Transactional
|
||||
public SocialPost updatePost(UUID postId, String content, String imageUrl) {
|
||||
System.out.println("[LOG] Mise à jour du post ID : " + postId);
|
||||
|
||||
SocialPost post = socialPostRepository.findById(postId);
|
||||
if (post == null) {
|
||||
System.out.println("[ERROR] Post non trouvé avec l'ID : " + postId);
|
||||
throw new IllegalArgumentException("Post non trouvé avec l'ID : " + postId);
|
||||
}
|
||||
|
||||
post.setContent(content);
|
||||
if (imageUrl != null) {
|
||||
post.setImageUrl(imageUrl);
|
||||
}
|
||||
|
||||
socialPostRepository.persist(post);
|
||||
System.out.println("[LOG] Post mis à jour avec succès");
|
||||
return post;
|
||||
}
|
||||
|
||||
/**
|
||||
* Supprime un post.
|
||||
*
|
||||
* @param postId L'ID du post
|
||||
* @return true si le post a été supprimé, false sinon
|
||||
*/
|
||||
@Transactional
|
||||
public boolean deletePost(UUID postId) {
|
||||
System.out.println("[LOG] Suppression du post ID : " + postId);
|
||||
|
||||
SocialPost post = socialPostRepository.findById(postId);
|
||||
if (post == null) {
|
||||
System.out.println("[ERROR] Post non trouvé avec l'ID : " + postId);
|
||||
return false;
|
||||
}
|
||||
|
||||
socialPostRepository.delete(post);
|
||||
System.out.println("[LOG] Post supprimé avec succès");
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Recherche des posts par contenu.
|
||||
*
|
||||
* @param query Le terme de recherche
|
||||
* @return Liste des posts correspondant à la recherche
|
||||
*/
|
||||
public List<SocialPost> searchPosts(String query) {
|
||||
System.out.println("[LOG] Recherche de posts avec la requête : " + query);
|
||||
return socialPostRepository.searchByContent(query);
|
||||
}
|
||||
|
||||
/**
|
||||
* Like un post (incrémente le compteur de likes).
|
||||
*
|
||||
* @param postId L'ID du post
|
||||
* @return Le post mis à jour
|
||||
*/
|
||||
@Transactional
|
||||
public SocialPost likePost(UUID postId) {
|
||||
System.out.println("[LOG] Like du post ID : " + postId);
|
||||
|
||||
SocialPost post = socialPostRepository.findById(postId);
|
||||
if (post == null) {
|
||||
System.out.println("[ERROR] Post non trouvé avec l'ID : " + postId);
|
||||
throw new IllegalArgumentException("Post non trouvé avec l'ID : " + postId);
|
||||
}
|
||||
|
||||
post.incrementLikes();
|
||||
socialPostRepository.persist(post);
|
||||
return post;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ajoute un commentaire à un post (incrémente le compteur de commentaires).
|
||||
*
|
||||
* @param postId L'ID du post
|
||||
* @return Le post mis à jour
|
||||
*/
|
||||
@Transactional
|
||||
public SocialPost addComment(UUID postId) {
|
||||
System.out.println("[LOG] Ajout de commentaire au post ID : " + postId);
|
||||
|
||||
SocialPost post = socialPostRepository.findById(postId);
|
||||
if (post == null) {
|
||||
System.out.println("[ERROR] Post non trouvé avec l'ID : " + postId);
|
||||
throw new IllegalArgumentException("Post non trouvé avec l'ID : " + postId);
|
||||
}
|
||||
|
||||
post.incrementComments();
|
||||
socialPostRepository.persist(post);
|
||||
return post;
|
||||
}
|
||||
|
||||
/**
|
||||
* Partage un post (incrémente le compteur de partages).
|
||||
*
|
||||
* @param postId L'ID du post
|
||||
* @return Le post mis à jour
|
||||
*/
|
||||
@Transactional
|
||||
public SocialPost sharePost(UUID postId) {
|
||||
System.out.println("[LOG] Partage du post ID : " + postId);
|
||||
|
||||
SocialPost post = socialPostRepository.findById(postId);
|
||||
if (post == null) {
|
||||
System.out.println("[ERROR] Post non trouvé avec l'ID : " + postId);
|
||||
throw new IllegalArgumentException("Post non trouvé avec l'ID : " + postId);
|
||||
}
|
||||
|
||||
post.incrementShares();
|
||||
socialPostRepository.persist(post);
|
||||
return post;
|
||||
}
|
||||
|
||||
/**
|
||||
* Récupère les posts de l'utilisateur et de ses amis (relations d'amitié acceptées).
|
||||
*
|
||||
* @param userId L'ID de l'utilisateur
|
||||
* @param page Le numéro de la page (0-indexé)
|
||||
* @param size La taille de la page
|
||||
* @return Liste paginée des posts de l'utilisateur et de ses amis
|
||||
* @throws UserNotFoundException Si l'utilisateur n'existe pas
|
||||
*/
|
||||
public List<SocialPost> getPostsByFriends(UUID userId, int page, int size) {
|
||||
System.out.println("[LOG] Récupération des posts des amis pour l'utilisateur ID : " + userId);
|
||||
|
||||
Users user = usersRepository.findById(userId);
|
||||
if (user == null) {
|
||||
System.out.println("[ERROR] Utilisateur non trouvé avec l'ID : " + userId);
|
||||
throw new UserNotFoundException("Utilisateur non trouvé avec l'ID : " + userId);
|
||||
}
|
||||
|
||||
// Récupérer toutes les relations d'amitié acceptées
|
||||
List<Friendship> friendships = friendshipRepository.findFriendsByUser(user, 0, Integer.MAX_VALUE);
|
||||
|
||||
// Extraire les IDs des amis
|
||||
List<UUID> friendIds = friendships.stream()
|
||||
.map(friendship -> {
|
||||
// L'ami est soit dans 'user' soit dans 'friend', selon qui a initié la relation
|
||||
return friendship.getUser().equals(user)
|
||||
? friendship.getFriend().getId()
|
||||
: friendship.getUser().getId();
|
||||
})
|
||||
.distinct()
|
||||
.collect(Collectors.toList());
|
||||
|
||||
System.out.println("[LOG] " + friendIds.size() + " ami(s) trouvé(s) pour l'utilisateur ID : " + userId);
|
||||
|
||||
// Récupérer les posts de l'utilisateur et de ses amis
|
||||
return socialPostRepository.findPostsByFriends(userId, friendIds, page, size);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user