Skip to content

Feat/week-1-2#13

Open
sgyun02 wants to merge 4 commits intoApptiveDev:윤서경from
sgyun02:main
Open

Feat/week-1-2#13
sgyun02 wants to merge 4 commits intoApptiveDev:윤서경from
sgyun02:main

Conversation

@sgyun02
Copy link
Copy Markdown

@sgyun02 sgyun02 commented Nov 6, 2025

변경점 👍

  • PostController, CommentController 구현
  • Post, Comment Entity 구현
  • PostRepository, CommentRepository 구현
  • PostService, CommentService 구현
  • 기본 CRUD 기능을 갖춘 게시글 및 댓글 기능 완성

테스트 💻

  • PostController, CommentController의 CRUD API Postman 테스트 완료
    • 게시글 생성, 조회, 수정, 삭제 확인
    • 댓글 생성, 조회, 수정, 삭제 확인

스크린샷 🖼

image image image

비고 ✏

  • week1 과제 당시, backend-study-sns-r repo인 줄 알고 어리둥절..하여 코드를 공부하는 방향으로 진행했습니다.
    이후 제대로 확인하여 이번 PR에 week 1 + week 2(기본적 댓글 구현까지)를 포함했습니다.
  • 아직 부족한 부분이 많지만, CRUD 기능 구현과 기본 테스트까지 완료했습니다.

Copy link
Copy Markdown
Contributor

@sinsehwan sinsehwan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

고생하셨습니다! 코드가 전반적으로 읽기 쉽게 구성돼있는 것 같아요! 다만 DTO, validation, 테스트 코드 등을 추가적으로 적용해보면 더 좋을 것 같습니다!

Comment on lines +25 to +27
@ManyToOne(fetch = FetchType.LAZY) // 댓글 조회 시 Post는 필요할 때만 불러옴
@JoinColumn(name = "post_id", nullable = false)
private Post post;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@manytoone에도 optional=false 속성을 추가해서 더 안전하게 구성할 수 있습니다!

Comment on lines +19 to +22
@PostMapping
public Comment createComment(@PathVariable Long postId, @RequestBody Comment comment) {
return commentService.createComment(postId, comment);
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PostController처럼 responseEntity를 사용해서 응답 코드와 함께 반환하도록 구성해주시면 좋을 것 같아요!

Comment on lines +34 to +43
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);
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RuntimeException의 경우 추상적이라 다른 구체적인 예외 타입(e.g. InvalidInputException)을 사용하거나 필요하다면 커스텀 예외 타입을 사용해보시는 걸 추천드립니다!

Comment on lines +15 to +17
public PostService(PostRepository postRepository) {
this.postRepository = postRepository;
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RequiredArgsConstuctor를 사용하는 걸 추천드립니다!

Comment on lines +37 to +38
@OneToMany(mappedBy = "post", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Comment> comments = new ArrayList<>();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

양방향 연관관계(ManyToOne, OneToMany 모두 적용)의 경우 객체 연관관계 탐색에서 더 유연하다는 장점은 있지만 java 코드상에서 두 연관 객체를 모두 관리해야 한다는 단점이 생깁니다. 그래서 저는 단방향 연관관계만(ManyToOne) 적용하고 추후 필요하다면 양방향 연관관계까지 적용하는 걸 추천드립니다. 어떤 연관관계를 설정하더라도 DB 테이블 구조는 같습니다.

Comment on lines +41 to +42
foundComment.setContent(updatedComment.getContent());
foundComment.setWriter(updatedComment.getWriter());
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해당 부분들도 setter 없이 엔티티 객체 내부 헬퍼 메소드로 분리할 수 있을 것 같습니다!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants