Add comment module

This commit is contained in:
Tim
2025-06-30 19:46:57 +08:00
parent 04e7aed9b8
commit ff64500ffd
6 changed files with 188 additions and 0 deletions

View File

@@ -0,0 +1,57 @@
package com.openisle.service;
import com.openisle.model.Comment;
import com.openisle.model.Post;
import com.openisle.model.User;
import com.openisle.repository.CommentRepository;
import com.openisle.repository.PostRepository;
import com.openisle.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
@RequiredArgsConstructor
public class CommentService {
private final CommentRepository commentRepository;
private final PostRepository postRepository;
private final UserRepository userRepository;
public Comment addComment(String username, Long postId, String content) {
User author = userRepository.findByUsername(username)
.orElseThrow(() -> new IllegalArgumentException("User not found"));
Post post = postRepository.findById(postId)
.orElseThrow(() -> new IllegalArgumentException("Post not found"));
Comment comment = new Comment();
comment.setAuthor(author);
comment.setPost(post);
comment.setContent(content);
return commentRepository.save(comment);
}
public Comment addReply(String username, Long parentId, String content) {
User author = userRepository.findByUsername(username)
.orElseThrow(() -> new IllegalArgumentException("User not found"));
Comment parent = commentRepository.findById(parentId)
.orElseThrow(() -> new IllegalArgumentException("Comment not found"));
Comment comment = new Comment();
comment.setAuthor(author);
comment.setPost(parent.getPost());
comment.setParent(parent);
comment.setContent(content);
return commentRepository.save(comment);
}
public List<Comment> getCommentsForPost(Long postId) {
Post post = postRepository.findById(postId)
.orElseThrow(() -> new IllegalArgumentException("Post not found"));
return commentRepository.findByPostAndParentIsNullOrderByCreatedAtAsc(post);
}
public List<Comment> getReplies(Long parentId) {
Comment parent = commentRepository.findById(parentId)
.orElseThrow(() -> new IllegalArgumentException("Comment not found"));
return commentRepository.findByParentOrderByCreatedAtAsc(parent);
}
}