Refactoring
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
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;
|
||||
@@ -14,6 +13,7 @@ import org.eclipse.microprofile.reactive.messaging.Emitter;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.transaction.Transactional;
|
||||
import org.jboss.logging.Logger;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -22,15 +22,15 @@ 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 {
|
||||
|
||||
private static final Logger logger = Logger.getLogger(SocialPostService.class);
|
||||
|
||||
@Inject
|
||||
SocialPostRepository socialPostRepository;
|
||||
|
||||
@@ -55,7 +55,7 @@ public class SocialPostService {
|
||||
* @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 + ")");
|
||||
logger.info("[SocialPostService] Récupération de tous les posts (page: " + page + ", size: " + size + ")");
|
||||
return socialPostRepository.findAllWithPagination(page, size);
|
||||
}
|
||||
|
||||
@@ -67,11 +67,11 @@ public class SocialPostService {
|
||||
* @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);
|
||||
logger.info("[SocialPostService] 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);
|
||||
logger.error("[SocialPostService] Utilisateur non trouvé avec l'ID : " + userId);
|
||||
throw new UserNotFoundException("Utilisateur non trouvé avec l'ID : " + userId);
|
||||
}
|
||||
|
||||
@@ -89,11 +89,11 @@ public class SocialPostService {
|
||||
*/
|
||||
@Transactional
|
||||
public SocialPost createPost(String content, UUID userId, String imageUrl) {
|
||||
System.out.println("[LOG] Création d'un post par l'utilisateur ID : " + userId);
|
||||
logger.info("[SocialPostService] 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);
|
||||
logger.error("[SocialPostService] Utilisateur non trouvé avec l'ID : " + userId);
|
||||
throw new UserNotFoundException("Utilisateur non trouvé avec l'ID : " + userId);
|
||||
}
|
||||
|
||||
@@ -103,7 +103,7 @@ public class SocialPostService {
|
||||
}
|
||||
|
||||
socialPostRepository.persist(post);
|
||||
System.out.println("[LOG] Post créé avec succès : " + post.getId());
|
||||
logger.info("[SocialPostService] Post créé avec succès : " + post.getId());
|
||||
|
||||
// Créer des notifications pour tous les amis
|
||||
try {
|
||||
@@ -128,9 +128,9 @@ public class SocialPostService {
|
||||
null
|
||||
);
|
||||
}
|
||||
System.out.println("[LOG] Notifications créées pour " + friendships.size() + " ami(s)");
|
||||
logger.info("[SocialPostService] 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());
|
||||
logger.error("[SocialPostService] Erreur lors de la création des notifications : " + e.getMessage());
|
||||
}
|
||||
|
||||
return post;
|
||||
@@ -143,11 +143,11 @@ public class SocialPostService {
|
||||
* @return Le post trouvé
|
||||
*/
|
||||
public SocialPost getPostById(UUID postId) {
|
||||
System.out.println("[LOG] Récupération du post ID : " + postId);
|
||||
logger.info("[SocialPostService] 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);
|
||||
logger.error("[SocialPostService] Post non trouvé avec l'ID : " + postId);
|
||||
throw new IllegalArgumentException("Post non trouvé avec l'ID : " + postId);
|
||||
}
|
||||
|
||||
@@ -164,11 +164,11 @@ public class SocialPostService {
|
||||
*/
|
||||
@Transactional
|
||||
public SocialPost updatePost(UUID postId, String content, String imageUrl) {
|
||||
System.out.println("[LOG] Mise à jour du post ID : " + postId);
|
||||
logger.info("[SocialPostService] 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);
|
||||
logger.error("[SocialPostService] Post non trouvé avec l'ID : " + postId);
|
||||
throw new IllegalArgumentException("Post non trouvé avec l'ID : " + postId);
|
||||
}
|
||||
|
||||
@@ -178,7 +178,7 @@ public class SocialPostService {
|
||||
}
|
||||
|
||||
socialPostRepository.persist(post);
|
||||
System.out.println("[LOG] Post mis à jour avec succès");
|
||||
logger.info("[SocialPostService] Post mis à jour avec succès");
|
||||
return post;
|
||||
}
|
||||
|
||||
@@ -190,16 +190,16 @@ public class SocialPostService {
|
||||
*/
|
||||
@Transactional
|
||||
public boolean deletePost(UUID postId) {
|
||||
System.out.println("[LOG] Suppression du post ID : " + postId);
|
||||
logger.info("[SocialPostService] Suppression du post ID : " + postId);
|
||||
|
||||
SocialPost post = socialPostRepository.findById(postId);
|
||||
if (post == null) {
|
||||
System.out.println("[ERROR] Post non trouvé avec l'ID : " + postId);
|
||||
logger.error("[SocialPostService] Post non trouvé avec l'ID : " + postId);
|
||||
return false;
|
||||
}
|
||||
|
||||
socialPostRepository.delete(post);
|
||||
System.out.println("[LOG] Post supprimé avec succès");
|
||||
logger.info("[SocialPostService] Post supprimé avec succès");
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -210,7 +210,7 @@ public class SocialPostService {
|
||||
* @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);
|
||||
logger.info("[SocialPostService] Recherche de posts avec la requête : " + query);
|
||||
return socialPostRepository.searchByContent(query);
|
||||
}
|
||||
|
||||
@@ -223,17 +223,35 @@ public class SocialPostService {
|
||||
*/
|
||||
@Transactional
|
||||
public SocialPost likePost(UUID postId, UUID userId) {
|
||||
System.out.println("[LOG] Like du post ID : " + postId + " par utilisateur : " + userId);
|
||||
logger.info("[SocialPostService] Like du post ID : " + postId + " par utilisateur : " + userId);
|
||||
|
||||
SocialPost post = socialPostRepository.findById(postId);
|
||||
if (post == null) {
|
||||
System.out.println("[ERROR] Post non trouvé avec l'ID : " + postId);
|
||||
logger.error("[SocialPostService] Post non trouvé avec l'ID : " + postId);
|
||||
throw new IllegalArgumentException("Post non trouvé avec l'ID : " + postId);
|
||||
}
|
||||
|
||||
post.incrementLikes();
|
||||
socialPostRepository.persist(post);
|
||||
|
||||
// Notification pour l'auteur du post (sauf auto-like)
|
||||
try {
|
||||
Users author = post.getUser();
|
||||
if (author != null && !author.getId().equals(userId)) {
|
||||
Users liker = usersRepository.findById(userId);
|
||||
String likerName = liker != null ? liker.getFirstName() + " " + liker.getLastName() : "Quelqu'un";
|
||||
notificationService.createNotification(
|
||||
"Nouveau like",
|
||||
likerName + " a aimé votre post",
|
||||
"post",
|
||||
author.getId(),
|
||||
null
|
||||
);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("[SocialPostService] Erreur création notification like : " + e.getMessage());
|
||||
}
|
||||
|
||||
// TEMPS RÉEL: Publier dans Kafka (v2.0)
|
||||
try {
|
||||
Map<String, Object> reactionData = new HashMap<>();
|
||||
@@ -252,9 +270,9 @@ public class SocialPostService {
|
||||
);
|
||||
|
||||
reactionEmitter.send(event);
|
||||
System.out.println("[LOG] Réaction like publiée dans Kafka pour post: " + postId);
|
||||
logger.info("[SocialPostService] Réaction like publiée dans Kafka pour post: " + postId);
|
||||
} catch (Exception e) {
|
||||
System.out.println("[ERROR] Erreur publication Kafka: " + e.getMessage());
|
||||
logger.error("[SocialPostService] Erreur publication Kafka: " + e.getMessage());
|
||||
// Ne pas bloquer le like si Kafka échoue
|
||||
}
|
||||
|
||||
@@ -271,17 +289,38 @@ public class SocialPostService {
|
||||
*/
|
||||
@Transactional
|
||||
public SocialPost addComment(UUID postId, UUID userId, String commentContent) {
|
||||
System.out.println("[LOG] Ajout de commentaire au post ID : " + postId + " par utilisateur : " + userId);
|
||||
logger.info("[SocialPostService] Ajout de commentaire au post ID : " + postId + " par utilisateur : " + userId);
|
||||
|
||||
SocialPost post = socialPostRepository.findById(postId);
|
||||
if (post == null) {
|
||||
System.out.println("[ERROR] Post non trouvé avec l'ID : " + postId);
|
||||
logger.error("[SocialPostService] Post non trouvé avec l'ID : " + postId);
|
||||
throw new IllegalArgumentException("Post non trouvé avec l'ID : " + postId);
|
||||
}
|
||||
|
||||
post.incrementComments();
|
||||
socialPostRepository.persist(post);
|
||||
|
||||
// Notification pour l'auteur du post (sauf auto-commentaire)
|
||||
try {
|
||||
Users author = post.getUser();
|
||||
if (author != null && !author.getId().equals(userId)) {
|
||||
Users commenter = usersRepository.findById(userId);
|
||||
String commenterName = commenter != null ? commenter.getFirstName() + " " + commenter.getLastName() : "Quelqu'un";
|
||||
String preview = commentContent != null && commentContent.length() > 60
|
||||
? commentContent.substring(0, 60) + "..."
|
||||
: (commentContent != null ? commentContent : "");
|
||||
notificationService.createNotification(
|
||||
"Nouveau commentaire",
|
||||
commenterName + " a commenté votre post : " + preview,
|
||||
"post",
|
||||
author.getId(),
|
||||
null
|
||||
);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("[SocialPostService] Erreur création notification commentaire : " + e.getMessage());
|
||||
}
|
||||
|
||||
// TEMPS RÉEL: Publier dans Kafka (v2.0)
|
||||
try {
|
||||
Users commenter = usersRepository.findById(userId);
|
||||
@@ -304,9 +343,9 @@ public class SocialPostService {
|
||||
);
|
||||
|
||||
reactionEmitter.send(event);
|
||||
System.out.println("[LOG] Réaction comment publiée dans Kafka pour post: " + postId);
|
||||
logger.info("[SocialPostService] Réaction comment publiée dans Kafka pour post: " + postId);
|
||||
} catch (Exception e) {
|
||||
System.out.println("[ERROR] Erreur publication Kafka: " + e.getMessage());
|
||||
logger.error("[SocialPostService] Erreur publication Kafka: " + e.getMessage());
|
||||
// Ne pas bloquer le commentaire si Kafka échoue
|
||||
}
|
||||
|
||||
@@ -322,11 +361,11 @@ public class SocialPostService {
|
||||
*/
|
||||
@Transactional
|
||||
public SocialPost sharePost(UUID postId, UUID userId) {
|
||||
System.out.println("[LOG] Partage du post ID : " + postId + " par utilisateur : " + userId);
|
||||
logger.info("[SocialPostService] Partage du post ID : " + postId + " par utilisateur : " + userId);
|
||||
|
||||
SocialPost post = socialPostRepository.findById(postId);
|
||||
if (post == null) {
|
||||
System.out.println("[ERROR] Post non trouvé avec l'ID : " + postId);
|
||||
logger.error("[SocialPostService] Post non trouvé avec l'ID : " + postId);
|
||||
throw new IllegalArgumentException("Post non trouvé avec l'ID : " + postId);
|
||||
}
|
||||
|
||||
@@ -348,9 +387,9 @@ public class SocialPostService {
|
||||
);
|
||||
|
||||
reactionEmitter.send(event);
|
||||
System.out.println("[LOG] Réaction share publiée dans Kafka pour post: " + postId);
|
||||
logger.info("[SocialPostService] Réaction share publiée dans Kafka pour post: " + postId);
|
||||
} catch (Exception e) {
|
||||
System.out.println("[ERROR] Erreur publication Kafka: " + e.getMessage());
|
||||
logger.error("[SocialPostService] Erreur publication Kafka: " + e.getMessage());
|
||||
// Ne pas bloquer le partage si Kafka échoue
|
||||
}
|
||||
|
||||
@@ -367,11 +406,11 @@ public class SocialPostService {
|
||||
* @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);
|
||||
logger.info("[SocialPostService] 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);
|
||||
logger.error("[SocialPostService] Utilisateur non trouvé avec l'ID : " + userId);
|
||||
throw new UserNotFoundException("Utilisateur non trouvé avec l'ID : " + userId);
|
||||
}
|
||||
|
||||
@@ -389,7 +428,7 @@ public class SocialPostService {
|
||||
.distinct()
|
||||
.collect(Collectors.toList());
|
||||
|
||||
System.out.println("[LOG] " + friendIds.size() + " ami(s) trouvé(s) pour l'utilisateur ID : " + userId);
|
||||
logger.info("[SocialPostService] " + 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