| package com.example.myproject.service; |
| |
| import com.example.myproject.entity.Comments; |
| import com.example.myproject.entity.Post; |
| import com.example.myproject.entity.Users; |
| import com.example.myproject.repository.CommentRepository; |
| import com.example.myproject.repository.PostRepository; |
| import com.example.myproject.repository.UserRepository; |
| import org.springframework.beans.factory.annotation.Autowired; |
| import org.springframework.stereotype.Service; |
| |
| import java.util.ArrayList; |
| import java.util.LinkedHashMap; |
| import java.util.List; |
| import java.util.Map; |
| import java.util.stream.Collectors; |
| |
| @Service |
| public class CommentService { |
| |
| @Autowired |
| private CommentRepository commentRepository; |
| |
| @Autowired |
| private PostRepository postRepository; |
| |
| @Autowired |
| private UserRepository userRepository; |
| |
| // 添加评论 |
| public void addComment(Long postId, Comments comment) { |
| try { |
| // 查找帖子 |
| Post post = postRepository.findById(postId).orElseThrow(() -> { |
| throw new RuntimeException("Post with ID " + postId + " not found"); |
| }); |
| |
| // 查找用户 |
| Users user = userRepository.findById(comment.getUserId()).orElseThrow(() -> { |
| throw new RuntimeException("User with ID " + comment.getUserId() + " not found"); |
| }); |
| |
| // 设置评论的相关属性 |
| comment.setPost(post); |
| comment.setUserId(user.getUserId()); |
| comment.setCommentTime(new java.util.Date()); |
| |
| if (comment.getParentComment() != null && comment.getParentComment() != 0) { |
| // 查找父评论 |
| Comments parentComment = commentRepository.findById(comment.getParentComment()) |
| .orElseThrow(() -> new RuntimeException("Parent comment with ID " + comment.getParentComment() + " not found")); |
| comment.setParentComment(parentComment.getCommentId()); // 设置父评论 ID |
| } else { |
| comment.setParentComment(0L); // 设置为 0,表示没有父评论 |
| } |
| |
| // 保存评论 |
| commentRepository.save(comment); |
| } catch (Exception e) { |
| // 捕获任何异常并打印堆栈信息 |
| e.printStackTrace(); |
| throw new RuntimeException("An error occurred while adding the comment: " + e.getMessage()); |
| } |
| } |
| |
| // 获取指定帖子的所有评论 |
| public List<Map<String, Object>> getCommentsByPostId(Long postId) { |
| List<Comments> comments = commentRepository.findAll() |
| .stream() |
| .filter(comment -> comment.getPost().getPostNo().equals(postId)) // 通过 post 的 postNo 过滤评论 |
| .collect(Collectors.toList()); |
| |
| if (comments.isEmpty()) { |
| return new ArrayList<>(); |
| } |
| |
| List<Map<String, Object>> result = new ArrayList<>(); |
| for (Comments comment : comments) { |
| Map<String, Object> commentData = new LinkedHashMap<>(); |
| commentData.put("commentId", comment.getCommentId()); |
| commentData.put("post_id", comment.getPost().getPostNo()); |
| commentData.put("userId", comment.getUserId()); |
| |
| Users user = userRepository.findById(comment.getUserId()).orElse(null); |
| commentData.put("nickname", user != null ? user.getUsername() : null); |
| commentData.put("avatar_url", user != null ? user.getAvatarUrl() : null); |
| |
| commentData.put("content", comment.getContent()); |
| commentData.put("isAnonymous", comment.getIsAnonymous()); |
| commentData.put("likesCount", comment.getLikesCount()); |
| commentData.put("replyCount", comment.getReplyCount()); |
| commentData.put("commentTime", comment.getCommentTime()); |
| commentData.put("parentComment", comment.getParentComment()); |
| |
| Map<String, Object> parentCommentData = getParentCommentData(comment.getParentComment()); |
| commentData.put("parentCommentData", parentCommentData); |
| |
| result.add(commentData); |
| } |
| |
| return result; |
| } |
| |
| // 递归查找父评论的内容 |
| private Map<String, Object> getParentCommentData(Long parentCommentId) { |
| Map<String, Object> parentCommentData = new LinkedHashMap<>(); |
| if (parentCommentId != null && parentCommentId != 0) { |
| // 查找父评论 |
| Comments parentComment = commentRepository.findById(parentCommentId).orElse(null); |
| if (parentComment != null) { |
| parentCommentData.put("commentId", parentComment.getCommentId()); |
| parentCommentData.put("content", parentComment.getContent()); |
| parentCommentData.put("userId", parentComment.getUserId()); |
| |
| Users user = userRepository.findById(parentComment.getUserId()).orElse(null); |
| parentCommentData.put("nickname", user != null ? user.getUsername() : null); |
| parentCommentData.put("avatar_url", user != null ? user.getAvatarUrl() : null); |
| |
| Map<String, Object> grandParentData = getParentCommentData(parentComment.getParentComment()); |
| parentCommentData.put("parentCommentData", grandParentData); |
| } |
| } |
| return parentCommentData; |
| } |
| } |