Conversation
Neibce
left a comment
There was a problem hiding this comment.
수고하셨습니다 다 잘 구현해주셨네요! 코멘트 몇 개 달아두었으니 확인해보시면 될 거 같습니다
제가 달아드린 내용들은 필수사항은 아니고, 단순 참고 정도로 보시면 될 거 같습니다..!
| this.likeCount = post.getLikes().size(); // 리스트 -> size() | ||
| this.commentCount = post.getComments().size(); |
There was a problem hiding this comment.
이 부분에서 N+1 문제가 발생할 거 같은데, N+1 문제와 해결방법들에 대해서 찾아보시면 좋을 거 같습니다!
물론 지금처럼 작은 규모의 프로젝트에서는 크게 문제없고 무시하고 진행하기도 합니다..!
- 게시글 목록을 조회할 때마다 각 게시글의 likes, comments를 조회하는 추가 쿼리 발생
- 100개의 게시글이면 201개의 SQL 쿼리 발생 (1 + 100 + 100)
| @OneToMany(mappedBy = "member") | ||
| private List<Post> posts = new ArrayList<>(); | ||
|
|
||
| @OneToMany(mappedBy = "member") | ||
| private List<Comment> comments = new ArrayList<>(); | ||
|
|
||
| @OneToMany(mappedBy = "member") | ||
| private List<Like> likes = new ArrayList<>(); |
There was a problem hiding this comment.
Member 엔티티에 Post, Comment, Like가 꼭 필요한지에 대해서 고민해보시면 좋을 거 같습니다!
| Member member = memberRepository.findById(memberId) | ||
| .orElseThrow(() -> new RuntimeException("member not found")); | ||
| Post post = postRepository.findById(postId) | ||
| .orElseThrow(() -> new RuntimeException("post not found")); |
There was a problem hiding this comment.
지금도 충분하지만, 커스텀 예외를 적용해서 예외에 대해 더 명확히 명시하는 것도 좋을 거 같습니다!
| @JoinColumn(name = "post_id",nullable = false) | ||
| private Post post; | ||
|
|
||
| private LocalDateTime CreatedAt; |
| @OneToMany(mappedBy = "post", cascade = CascadeType.ALL,orphanRemoval = true) | ||
| private List<Comment> comments = new ArrayList<>(); |
There was a problem hiding this comment.
현재 Post->Comment, Comment->Post로 양방향 참조가 걸려있는 거 같은데, 보통은 단방향만으로도 충분한 경우가 대부분이니 한번 고민해보셔도 좋을 거 같습니다
| import java.util.List; | ||
|
|
||
| @Service | ||
| public class CommentService { |
There was a problem hiding this comment.
여기에도 Transactional 적용하면 좋을 거 같습니다
| import java.util.Optional; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/post") |
There was a problem hiding this comment.
posts로 복수형으로 변경해서 통일시키는게 나을 거 같습니다!
| private Member (String username, String email, String password){ | ||
| this.username = username; | ||
| this.email = email; | ||
| this.password = password; |
| public List<Post> findAll(){ | ||
| return postRepository.findAll(); | ||
| } |
| private String content; | ||
| private String username; | ||
| private Long memberId; |
There was a problem hiding this comment.
DTO 내에 Validation을 적용해보는 것도 좋을 거 같습니다 음수나 빈 제목 등을 사전에 검증해 입력되는 걸 방지할 수 있습니다..!

변경점 👍
DTO 구현, 테스트 코드 수정 및 추가, member & like Entity 추가
테스트 💻
SpringBootTest를 활용한 테스트
비고 ✏
TDD 방식이 아직 미숙하여 테스트 코드 작성이 어렵게만 느껴집니다..