-
Notifications
You must be signed in to change notification settings - Fork 1
댓글/대댓글 작성, 조회 API #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pingu9
wants to merge
14
commits into
main
Choose a base branch
from
feat-dj-comments-and-reply
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 9 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
61310ba
feat: 댓글 관련 entity, repository 관련 코드 추가
4ce9d04
feat: 댓글 작성 기능 추가
a89710a
feat: 댓글 작성 테스트 추가
dd8987c
feat: 댓글 작성 시 userId도 받도록 수정
797f3a1
test: 댓글 작성 시 userId 추가함에 따라 테스트 수정
ca4f5c5
feat: 대댓글 작성 기능 추가
01180f6
test: 대댓글 작성 기능 테스트 추가
8ed33b7
refactor: CommentCommandExecutor -> PostCommentCommandExecutor로 이름 변경
e773538
feat: 댓글 조회 API 추가
9d93dda
refactor: 패키지 이동
efbbdd3
refactor: 커맨드 패턴 처리 방식 변경
bdf974d
refactor: 반환 타입 추가
21b40dc
test: 댓글 조회 기능 테스트 추가
f276bbc
Merge pull request #7 from Project-Catcher/feat-dj-comments-and-reply-v2
pingu9 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/main/java/com/catcher/core/GetCommentCommandExecutor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package com.catcher.core; | ||
|
|
||
| import com.catcher.core.domain.command.Command; | ||
| import com.catcher.core.domain.command.GetParentCommentsByPageCommand; | ||
| import com.catcher.core.domain.entity.Comment; | ||
| import com.catcher.datasource.CommentRepository; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.data.domain.Page; | ||
| import org.springframework.stereotype.Component; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class GetCommentCommandExecutor implements CommandExecutor<Page<Comment>> { | ||
|
|
||
| private final CommentRepository commentRepository; | ||
|
|
||
| @Override | ||
| @Transactional | ||
| public Page<Comment> run(final Command command) { | ||
| //1. datasource layer 호출(DB) | ||
| //2. 가공해서 넘겨줘야 한다 | ||
| return switch (command.getClass().getSimpleName()) { | ||
| case "GetParentCommentsByPageCommand" -> getParentCommentsByPage(command); | ||
| default -> null; | ||
| }; | ||
| } | ||
|
|
||
| private Page<Comment> getParentCommentsByPage(final Command command) { | ||
|
|
||
| final GetParentCommentsByPageCommand getParentCommentsByPageCommand = (GetParentCommentsByPageCommand) command; | ||
| return commentRepository.findByParentIsNull(getParentCommentsByPageCommand.getPageable()); | ||
| } | ||
| } | ||
60 changes: 60 additions & 0 deletions
60
src/main/java/com/catcher/core/PostCommentCommandExecutor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| package com.catcher.core; | ||
|
|
||
| import com.catcher.core.domain.command.Command; | ||
| import com.catcher.core.domain.command.PostCommentCommand; | ||
| import com.catcher.core.domain.command.PostCommentReplyCommand; | ||
| import com.catcher.core.domain.entity.Comment; | ||
| import com.catcher.datasource.CommentRepository; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Component; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class PostCommentCommandExecutor implements CommandExecutor<Comment> { | ||
|
|
||
| private final CommentRepository commentRepository; | ||
|
|
||
| @Override | ||
| @Transactional | ||
| public Comment run(final Command command) { | ||
| //1. datasource layer 호출(DB) | ||
| //2. 가공해서 넘겨줘야 한다 | ||
| return switch (command.getClass().getSimpleName()) { | ||
| case "PostCommentCommand" -> postComment(command); | ||
| case "PostCommentReplyCommand" -> postCommentReply(command); | ||
| default -> null; | ||
| }; | ||
| } | ||
|
|
||
| private Comment postComment(final Command command) { | ||
|
|
||
| final PostCommentCommand postCommentCommand = (PostCommentCommand)command; | ||
| return commentRepository.save(Comment | ||
| .builder() | ||
| .userId(postCommentCommand.getUserId()) | ||
| .contents(postCommentCommand.getContents()) | ||
| .build()); | ||
| } | ||
|
|
||
| private Comment postCommentReply(final Command command) { | ||
|
|
||
| final PostCommentReplyCommand postCommentReplyCommand = (PostCommentReplyCommand) command; | ||
|
|
||
| final Comment parentComment = commentRepository | ||
| .findById(postCommentReplyCommand.getParentId()) | ||
| .orElseThrow(); //TODO: fill custom exception | ||
|
|
||
| final Comment reply = Comment | ||
| .builder() | ||
| .userId(postCommentReplyCommand.getUserId()) | ||
| .parent(parentComment) | ||
| .contents(postCommentReplyCommand.getContents()) | ||
| .build(); | ||
|
|
||
| parentComment.getReplies().add(reply); | ||
|
|
||
| return reply; | ||
| } | ||
|
|
||
| } |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/catcher/core/domain/command/GetParentCommentsByPageCommand.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package com.catcher.core.domain.command; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.Data; | ||
| import org.springframework.data.domain.Pageable; | ||
|
|
||
| @Data | ||
| @AllArgsConstructor | ||
| public class GetParentCommentsByPageCommand implements Command { | ||
|
|
||
| private Pageable pageable; | ||
|
|
||
| } |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/catcher/core/domain/command/PostCommentCommand.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package com.catcher.core.domain.command; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.Data; | ||
|
|
||
| @Data | ||
| @AllArgsConstructor | ||
| public class PostCommentCommand implements Command { | ||
|
|
||
| Long userId; | ||
|
|
||
| String contents; | ||
| } |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/catcher/core/domain/command/PostCommentReplyCommand.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package com.catcher.core.domain.command; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.Data; | ||
|
|
||
| @Data | ||
| @AllArgsConstructor | ||
| public class PostCommentReplyCommand implements Command { | ||
|
|
||
| Long userId; | ||
|
|
||
| Long parentId; | ||
|
|
||
| String contents; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package com.catcher.core.domain.entity; | ||
|
|
||
| import jakarta.persistence.*; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| @Entity | ||
| @Getter | ||
| @Builder | ||
| @NoArgsConstructor | ||
| @AllArgsConstructor | ||
| public class Comment { | ||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| private Long id; | ||
|
|
||
| @ManyToOne | ||
| @JoinColumn(name = "parent_id") | ||
| private Comment parent; | ||
|
|
||
| @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, fetch = FetchType.LAZY) | ||
| @Builder.Default | ||
| private List<Comment> replies = new ArrayList<>(); | ||
|
|
||
| private Long userId; | ||
|
|
||
| private String contents; | ||
| } |
19 changes: 19 additions & 0 deletions
19
src/main/java/com/catcher/core/domain/request/PostCommentReplyRequest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package com.catcher.core.domain.request; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| @Getter | ||
| @Builder // TODO: 테스트를 위해서만 필요한 경우? | ||
| @NoArgsConstructor | ||
| @AllArgsConstructor | ||
| public class PostCommentReplyRequest { | ||
|
|
||
| private Long userId; | ||
|
|
||
| private Long parentId; | ||
|
|
||
| private String contents; | ||
| } |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/catcher/core/domain/request/PostCommentRequest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package com.catcher.core.domain.request; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| @Getter | ||
| @Builder // TODO: 테스트를 위해서만 필요한 경우? | ||
| @NoArgsConstructor | ||
| @AllArgsConstructor | ||
| public class PostCommentRequest { | ||
|
|
||
| private Long userId; | ||
|
|
||
| private String contents; | ||
| } |
43 changes: 43 additions & 0 deletions
43
src/main/java/com/catcher/core/domain/response/GetCommentsByPageResponse.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package com.catcher.core.domain.response; | ||
|
|
||
| import com.catcher.core.domain.entity.Comment; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import org.springframework.data.domain.Page; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| @Getter | ||
| @Builder | ||
| public class GetCommentsByPageResponse { | ||
|
|
||
| private Long id; | ||
|
|
||
| private String contents; | ||
|
|
||
| private List<GetCommentsByPageResponse> childComments; | ||
|
|
||
| public static List<GetCommentsByPageResponse> createGetCommentsByPageResponseList(Page<Comment> commentPage) { | ||
|
pingu9 marked this conversation as resolved.
|
||
| return commentPage | ||
| .stream() | ||
| .map(GetCommentsByPageResponse::buildRecursiveCommentResponse) | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| private static GetCommentsByPageResponse buildRecursiveCommentResponse(Comment comment) { | ||
| GetCommentsByPageResponse response = GetCommentsByPageResponse | ||
| .builder() | ||
| .id(comment.getId()) | ||
| .contents(comment.getContents()) | ||
| .childComments(new ArrayList<>()) | ||
| .build(); | ||
|
|
||
| for (Comment reply : comment.getReplies()) { | ||
| response.getChildComments().add(buildRecursiveCommentResponse(reply)); | ||
| } | ||
|
|
||
| return response; | ||
| } | ||
| } | ||
20 changes: 20 additions & 0 deletions
20
src/main/java/com/catcher/core/service/CommentService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package com.catcher.core.service; | ||
|
|
||
| import com.catcher.core.GetCommentCommandExecutor; | ||
| import com.catcher.core.domain.command.GetParentCommentsByPageCommand; | ||
| import com.catcher.core.domain.entity.Comment; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.data.domain.Page; | ||
| import org.springframework.data.domain.Pageable; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class CommentService { | ||
|
|
||
| private final GetCommentCommandExecutor getCommentCommandExecutor; | ||
|
|
||
| public Page<Comment> getCommentsWithSize(final Pageable pageable) { | ||
| return getCommentCommandExecutor.run(new GetParentCommentsByPageCommand(pageable)); | ||
| } | ||
| } |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/catcher/datasource/CommentRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package com.catcher.datasource; | ||
|
|
||
| import com.catcher.core.domain.entity.Comment; | ||
| import org.springframework.data.domain.Page; | ||
| import org.springframework.data.domain.Pageable; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
|
|
||
| public interface CommentRepository extends JpaRepository<Comment, Long> { | ||
|
|
||
| // TODO: N+1 join | ||
| // @Query(value = "SELECT DISTINCT c FROM Comment c LEFT OUTER JOIN FETCH c.replies WHERE c.parent IS NULL") | ||
| Page<Comment> findByParentIsNull(Pageable pageable); | ||
|
|
||
| } |
49 changes: 49 additions & 0 deletions
49
src/main/java/com/catcher/resource/CommentAPiController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package com.catcher.resource; | ||
|
|
||
| import com.catcher.core.PostCommentCommandExecutor; | ||
| import com.catcher.core.domain.command.PostCommentCommand; | ||
| import com.catcher.core.domain.command.PostCommentReplyCommand; | ||
| import com.catcher.core.domain.request.PostCommentReplyRequest; | ||
| import com.catcher.core.domain.request.PostCommentRequest; | ||
| import com.catcher.core.domain.response.GetCommentsByPageResponse; | ||
| import com.catcher.core.service.CommentService; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.data.domain.Pageable; | ||
| import org.springframework.data.web.PageableDefault; | ||
| import org.springframework.web.bind.annotation.*; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @RequiredArgsConstructor | ||
| @RestController | ||
| @RequestMapping("/comment") | ||
| public class CommentAPiController { | ||
|
|
||
| private final PostCommentCommandExecutor postCommentCommandExecutor; | ||
|
|
||
| private final CommentService commentService; | ||
|
|
||
| @PostMapping | ||
| public void postComment(@RequestBody PostCommentRequest postCommentRequest) { | ||
| postCommentCommandExecutor.run(new PostCommentCommand( | ||
| postCommentRequest.getUserId(), | ||
| postCommentRequest.getContents()) | ||
| ); | ||
| } | ||
|
|
||
| @PostMapping("/reply") | ||
| public void replyComment(@RequestBody PostCommentReplyRequest postCommentReplyRequest) { | ||
| postCommentCommandExecutor.run(new PostCommentReplyCommand( | ||
| postCommentReplyRequest.getUserId(), | ||
| postCommentReplyRequest.getParentId(), | ||
| postCommentReplyRequest.getContents() | ||
| )); | ||
| } | ||
|
|
||
| @GetMapping | ||
| public List<GetCommentsByPageResponse> getComments(@PageableDefault(size = 20, sort = {"id"}) Pageable pageable) { | ||
| final var commentPage = commentService.getCommentsWithSize(pageable); | ||
|
|
||
| return GetCommentsByPageResponse.createGetCommentsByPageResponseList(commentPage); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/test/java/com/catcher/datasource/TestCommentRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package com.catcher.datasource; | ||
|
|
||
| import com.catcher.core.domain.entity.Comment; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
|
|
||
| // TODO: 프로덕션 코드에는 없는 기능이 필요한 경우? | ||
|
pingu9 marked this conversation as resolved.
|
||
| public interface TestCommentRepository extends JpaRepository<Comment, Long> { | ||
|
|
||
| Comment findFirstByUserIdAndContentsOrderByIdDesc(Long userId, String contents); | ||
|
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.