-
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 all 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
This file was deleted.
Oops, something went wrong.
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 |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| package com.catcher.core.domain.command; | ||
|
|
||
| public interface Command { | ||
| public interface Command<T> { | ||
| T execute(); | ||
| } |
5 changes: 5 additions & 0 deletions
5
src/main/java/com/catcher/core/domain/command/CommandExecutor.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,5 @@ | ||
| package com.catcher.core.domain.command; | ||
|
|
||
| public interface CommandExecutor { | ||
| <T> T run(Command<T> command); | ||
| } |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/catcher/core/domain/command/CommentCommandExecutor.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 org.springframework.stereotype.Component; | ||
|
|
||
| @Component | ||
| public class CommentCommandExecutor implements CommandExecutor { | ||
|
|
||
| @Override | ||
| public <T> T run(Command<T> command) { | ||
| return command.execute(); | ||
| } | ||
|
|
||
| } |
19 changes: 19 additions & 0 deletions
19
src/main/java/com/catcher/core/domain/command/GetCommentCommand.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.command; | ||
|
|
||
| import com.catcher.core.domain.entity.Comment; | ||
| import com.catcher.core.service.CommentService; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.data.domain.Page; | ||
| import org.springframework.data.domain.Pageable; | ||
|
|
||
| @RequiredArgsConstructor | ||
| public class GetCommentCommand implements Command<Page<Comment>> { | ||
|
|
||
| private final CommentService commentService; | ||
|
|
||
| private final Pageable pageable; | ||
| @Override | ||
| public Page<Comment> execute() { | ||
| return commentService.findByParentIsNull(pageable); | ||
| } | ||
| } |
21 changes: 21 additions & 0 deletions
21
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,21 @@ | ||
| package com.catcher.core.domain.command; | ||
|
|
||
| import com.catcher.core.domain.request.PostCommentRequest; | ||
| import com.catcher.core.service.CommentService; | ||
| import lombok.RequiredArgsConstructor; | ||
|
|
||
| @RequiredArgsConstructor | ||
| public class PostCommentCommand implements Command<Void> { | ||
|
|
||
| private final CommentService commentService; | ||
|
|
||
| private final PostCommentRequest postCommentRequest; | ||
|
|
||
| @Override | ||
| public Void execute() { | ||
| commentService.saveSingleComment(postCommentRequest.getUserId(), postCommentRequest.getContents()); | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| } |
25 changes: 25 additions & 0 deletions
25
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,25 @@ | ||
| package com.catcher.core.domain.command; | ||
|
|
||
| import com.catcher.core.domain.request.PostCommentReplyRequest; | ||
| import com.catcher.core.service.CommentService; | ||
| import lombok.RequiredArgsConstructor; | ||
|
|
||
| @RequiredArgsConstructor | ||
| public class PostCommentReplyCommand implements Command<Void> { | ||
|
|
||
| private final CommentService commentService; | ||
|
|
||
| private final PostCommentReplyRequest postCommentReplyRequest; | ||
|
|
||
| @Override | ||
| public Void execute() { | ||
| commentService.saveSingleReply( | ||
| postCommentReplyRequest.getParentId(), | ||
| postCommentReplyRequest.getUserId(), | ||
| postCommentReplyRequest.getContents() | ||
| ); | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| } |
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) { | ||
| 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; | ||
| } | ||
| } | ||
48 changes: 48 additions & 0 deletions
48
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,48 @@ | ||
| package com.catcher.core.service; | ||
|
|
||
| import com.catcher.core.domain.entity.Comment; | ||
| import com.catcher.datasource.CommentRepository; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.data.domain.Page; | ||
| import org.springframework.data.domain.Pageable; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class CommentService { | ||
|
|
||
| private final CommentRepository commentRepository; | ||
|
|
||
| @Transactional | ||
| public Page<Comment> findByParentIsNull(final Pageable pageable) { | ||
| return commentRepository.findByParentIsNull(pageable); | ||
| } | ||
|
|
||
| @Transactional | ||
| public Comment saveSingleComment(final Long userId, final String contents) { | ||
| return commentRepository.save(Comment | ||
| .builder() | ||
| .userId(userId) | ||
| .contents(contents) | ||
| .build()); | ||
| } | ||
|
|
||
| @Transactional | ||
| public Comment saveSingleReply(Long parentId, Long userId, String contents) { | ||
| final Comment parentComment = commentRepository | ||
| .findById(parentId) | ||
| .orElseThrow(); //TODO: fill custom exception | ||
|
|
||
| final Comment reply = Comment | ||
| .builder() | ||
| .userId(userId) | ||
| .parent(parentComment) | ||
| .contents(contents) | ||
| .build(); | ||
|
|
||
| parentComment.getReplies().add(reply); | ||
|
|
||
| return reply; | ||
| } | ||
| } |
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); | ||
|
|
||
| } |
45 changes: 45 additions & 0 deletions
45
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,45 @@ | ||
| package com.catcher.resource; | ||
|
|
||
| import com.catcher.core.domain.command.*; | ||
| import com.catcher.core.domain.entity.Comment; | ||
| 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.Page; | ||
| 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 CommentService commentService; | ||
|
|
||
| private final CommentCommandExecutor commandExecutor; | ||
|
|
||
| @PostMapping | ||
| public void postComment(@RequestBody PostCommentRequest postCommentRequest) { | ||
| Command<Void> command = new PostCommentCommand(commentService, postCommentRequest); | ||
| commandExecutor.run(command); | ||
| } | ||
|
|
||
| @PostMapping("/reply") | ||
| public void replyComment(@RequestBody PostCommentReplyRequest postCommentReplyRequest) { | ||
| Command<Void> command = new PostCommentReplyCommand(commentService, postCommentReplyRequest); | ||
| commandExecutor.run(command); | ||
| } | ||
|
|
||
| @GetMapping | ||
| public List<GetCommentsByPageResponse> getComments(@PageableDefault(size = 20, sort = {"id"}) Pageable pageable) { | ||
| Command<Page<Comment>> command = new GetCommentCommand(commentService, pageable); | ||
| Page<Comment> commentPage = commandExecutor.run(command); | ||
|
|
||
| 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.