Conversation
sinsehwan
left a comment
There was a problem hiding this comment.
고생하셨습니다! 코드가 전반적으로 읽기 쉽게 구성돼있는 것 같아요! 다만 DTO, validation, 테스트 코드 등을 추가적으로 적용해보면 더 좋을 것 같습니다!
| @ManyToOne(fetch = FetchType.LAZY) // 댓글 조회 시 Post는 필요할 때만 불러옴 | ||
| @JoinColumn(name = "post_id", nullable = false) | ||
| private Post post; |
There was a problem hiding this comment.
@manytoone에도 optional=false 속성을 추가해서 더 안전하게 구성할 수 있습니다!
| @PostMapping | ||
| public Comment createComment(@PathVariable Long postId, @RequestBody Comment comment) { | ||
| return commentService.createComment(postId, comment); | ||
| } |
There was a problem hiding this comment.
PostController처럼 responseEntity를 사용해서 응답 코드와 함께 반환하도록 구성해주시면 좋을 것 같아요!
| public Post updatePost(Long id, Post updatedPost) { | ||
| Post foundPost = postRepository.findById(id) | ||
| .orElseThrow(() -> new RuntimeException("Post not found with id: " + id)); | ||
|
|
||
| foundPost.setTitle(updatedPost.getTitle()); | ||
| foundPost.setContent(updatedPost.getContent()); | ||
| foundPost.setWriter(updatedPost.getWriter()); | ||
|
|
||
| return postRepository.save(foundPost); | ||
| } |
There was a problem hiding this comment.
update로직을 post 엔티티 내부에 구성해서 setter 사용 없이 update 로직의 책임을 post 객체에 위임하면 더 객체지향적으로 코드를 구성할 수 있습니다! 그리고 JPA의 변경 감지 기능 때문에 save를 호출하지 않아도 transaction 커밋 시 업데이트 쿼리가 자동으로 수행됩니다!
|
|
||
| public void deletePost(Long id) { | ||
| if (!postRepository.existsById(id)) { | ||
| throw new RuntimeException("Post not found with id: " + id); |
There was a problem hiding this comment.
RuntimeException의 경우 추상적이라 다른 구체적인 예외 타입(e.g. InvalidInputException)을 사용하거나 필요하다면 커스텀 예외 타입을 사용해보시는 걸 추천드립니다!
| public PostService(PostRepository postRepository) { | ||
| this.postRepository = postRepository; | ||
| } |
There was a problem hiding this comment.
@RequiredArgsConstuctor를 사용하는 걸 추천드립니다!
| @OneToMany(mappedBy = "post", cascade = CascadeType.ALL, orphanRemoval = true) | ||
| private List<Comment> comments = new ArrayList<>(); |
There was a problem hiding this comment.
양방향 연관관계(ManyToOne, OneToMany 모두 적용)의 경우 객체 연관관계 탐색에서 더 유연하다는 장점은 있지만 java 코드상에서 두 연관 객체를 모두 관리해야 한다는 단점이 생깁니다. 그래서 저는 단방향 연관관계만(ManyToOne) 적용하고 추후 필요하다면 양방향 연관관계까지 적용하는 걸 추천드립니다. 어떤 연관관계를 설정하더라도 DB 테이블 구조는 같습니다.
| foundComment.setContent(updatedComment.getContent()); | ||
| foundComment.setWriter(updatedComment.getWriter()); |
There was a problem hiding this comment.
해당 부분들도 setter 없이 엔티티 객체 내부 헬퍼 메소드로 분리할 수 있을 것 같습니다!
변경점 👍
테스트 💻
스크린샷 🖼
비고 ✏
이후 제대로 확인하여 이번 PR에 week 1 + week 2(기본적 댓글 구현까지)를 포함했습니다.