Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions src/main/java/com/catcher/core/CommandExecutor.java

This file was deleted.

34 changes: 0 additions & 34 deletions src/main/java/com/catcher/core/GetCommentCommandExecutor.java

This file was deleted.

60 changes: 0 additions & 60 deletions src/main/java/com/catcher/core/PostCommentCommandExecutor.java

This file was deleted.

3 changes: 2 additions & 1 deletion src/main/java/com/catcher/core/domain/command/Command.java
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();
}
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);
}
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();
}

}
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);
}
}

This file was deleted.

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;
}
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 src/main/java/com/catcher/core/service/CommentService.java
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;
}
}
26 changes: 11 additions & 15 deletions src/main/java/com/catcher/resource/CommentAPiController.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
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.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.*;
Expand All @@ -19,30 +19,26 @@
@RequestMapping("/comment")
public class CommentAPiController {

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.

Suggested change
public class CommentAPiController {
public class CommentAPIController {

이것도 있었네요ㅋㅋㅋ

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

앗.. 맞네요 ㅋㅋㅋ 감사합니다~!


private final PostCommentCommandExecutor postCommentCommandExecutor;

private final CommentService commentService;

private final CommentCommandExecutor commandExecutor;

@PostMapping
public void postComment(@RequestBody PostCommentRequest postCommentRequest) {
postCommentCommandExecutor.run(new PostCommentCommand(
postCommentRequest.getUserId(),
postCommentRequest.getContents())
);
Command<Void> command = new PostCommentCommand(commentService, postCommentRequest);
commandExecutor.run(command);
}

@PostMapping("/reply")
public void replyComment(@RequestBody PostCommentReplyRequest postCommentReplyRequest) {
postCommentCommandExecutor.run(new PostCommentReplyCommand(
postCommentReplyRequest.getUserId(),
postCommentReplyRequest.getParentId(),
postCommentReplyRequest.getContents()
));
Command<Void> command = new PostCommentReplyCommand(commentService, postCommentReplyRequest);
commandExecutor.run(command);
}

@GetMapping
public List<GetCommentsByPageResponse> getComments(@PageableDefault(size = 20, sort = {"id"}) Pageable pageable) {
final var commentPage = commentService.getCommentsWithSize(pageable);
Command<Page<Comment>> command = new GetCommentCommand(commentService, pageable);
Page<Comment> commentPage = commandExecutor.run(command);

return GetCommentsByPageResponse.createGetCommentsByPageResponseList(commentPage);
}
Expand Down
Loading