-
Notifications
You must be signed in to change notification settings - Fork 1
댓글 작성 기능 - 커맨드 패턴 처리 개선 #7
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
Merged
pingu9
merged 4 commits into
feat-dj-comments-and-reply
from
feat-dj-comments-and-reply-v2
Oct 14, 2023
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 was deleted.
Oops, something went wrong.
34 changes: 0 additions & 34 deletions
34
src/main/java/com/catcher/core/GetCommentCommandExecutor.java
This file was deleted.
Oops, something went wrong.
60 changes: 0 additions & 60 deletions
60
src/main/java/com/catcher/core/PostCommentCommandExecutor.java
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); | ||
| } | ||
| } |
13 changes: 0 additions & 13 deletions
13
src/main/java/com/catcher/core/domain/command/GetParentCommentsByPageCommand.java
This file was deleted.
Oops, something went wrong.
22 changes: 15 additions & 7 deletions
22
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 |
|---|---|---|
| @@ -1,13 +1,21 @@ | ||
| package com.catcher.core.domain.command; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.Data; | ||
| import com.catcher.core.domain.request.PostCommentRequest; | ||
| import com.catcher.core.service.CommentService; | ||
| import lombok.RequiredArgsConstructor; | ||
|
|
||
| @Data | ||
| @AllArgsConstructor | ||
| public class PostCommentCommand implements Command { | ||
| @RequiredArgsConstructor | ||
| public class PostCommentCommand implements Command<Void> { | ||
|
|
||
| Long userId; | ||
| private final CommentService commentService; | ||
|
|
||
| private final PostCommentRequest postCommentRequest; | ||
|
|
||
| @Override | ||
| public Void execute() { | ||
| commentService.saveSingleComment(postCommentRequest.getUserId(), postCommentRequest.getContents()); | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| String contents; | ||
| } |
26 changes: 18 additions & 8 deletions
26
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 |
|---|---|---|
| @@ -1,15 +1,25 @@ | ||
| package com.catcher.core.domain.command; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.Data; | ||
| import com.catcher.core.domain.request.PostCommentReplyRequest; | ||
| import com.catcher.core.service.CommentService; | ||
| import lombok.RequiredArgsConstructor; | ||
|
|
||
| @Data | ||
| @AllArgsConstructor | ||
| public class PostCommentReplyCommand implements Command { | ||
| @RequiredArgsConstructor | ||
| public class PostCommentReplyCommand implements Command<Void> { | ||
|
|
||
| Long userId; | ||
| private final CommentService commentService; | ||
|
|
||
| Long parentId; | ||
| private final PostCommentReplyRequest postCommentReplyRequest; | ||
|
|
||
| @Override | ||
| public Void execute() { | ||
| commentService.saveSingleReply( | ||
| postCommentReplyRequest.getParentId(), | ||
| postCommentReplyRequest.getUserId(), | ||
| postCommentReplyRequest.getContents() | ||
| ); | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| String contents; | ||
| } |
38 changes: 33 additions & 5 deletions
38
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 |
|---|---|---|
| @@ -1,20 +1,48 @@ | ||
| 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 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 GetCommentCommandExecutor getCommentCommandExecutor; | ||
| private final CommentRepository commentRepository; | ||
|
|
||
| public Page<Comment> getCommentsWithSize(final Pageable pageable) { | ||
| return getCommentCommandExecutor.run(new GetParentCommentsByPageCommand(pageable)); | ||
| @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; | ||
| } | ||
| } |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이것도 있었네요ㅋㅋㅋ
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
앗.. 맞네요 ㅋㅋㅋ 감사합니다~!