-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathCommentService.java
More file actions
33 lines (27 loc) · 1.09 KB
/
CommentService.java
File metadata and controls
33 lines (27 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.example.devSns.service;
import com.example.devSns.entity.Comment;
import com.example.devSns.entity.Post;
import com.example.devSns.repository.CommentRepository;
import com.example.devSns.repository.PostRepository;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class CommentService {
private final CommentRepository commentRepository;
private final PostRepository postRepository;
public CommentService(CommentRepository commentRepository, PostRepository postRepository) {
this.commentRepository = commentRepository;
this.postRepository = postRepository;
}
public List<Comment> getCommentByPost(Long postId){
return commentRepository.findByPost_Id(postId);
}
public Comment addComment(Long postId, Comment comment){
Post post = postRepository.findById(postId).orElseThrow(()-> new IllegalArgumentException("게시글 없음"));
comment.setPost(post);
return commentRepository.save(comment);
}
public void deleteComment(Long id){
commentRepository.deleteById(id);
}
}