Skip to content

Commit

Permalink
feat(NoticeCommentEditingUseCase): NoticeCommentEditingUseCase 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
zbqmgldjfh committed Feb 20, 2025
1 parent c00c9b1 commit b66dea7
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.kustacks.kuring.common.annotation.RestWebAdapter;
import com.kustacks.kuring.common.dto.BaseResponse;
import com.kustacks.kuring.notice.adapter.in.web.dto.NoticeCommentCreateRequest;
import com.kustacks.kuring.notice.adapter.in.web.dto.NoticeCommentEditRequest;
import com.kustacks.kuring.notice.application.port.in.NoticeCommentEditingUseCase;
import com.kustacks.kuring.notice.application.port.in.NoticeCommentWritingUseCase;
import com.kustacks.kuring.notice.application.port.in.NoticeCommentWritingUseCase.WriteCommentCommand;
import com.kustacks.kuring.notice.application.port.in.NoticeCommentWritingUseCase.WriteReplyCommand;
Expand All @@ -28,6 +30,7 @@ public class NoticeCommandApiV2 {
private static final String USER_TOKEN_HEADER_KEY = "User-Token";

private final NoticeCommentWritingUseCase noticeCommentWritingUseCase;
private final NoticeCommentEditingUseCase noticeCommentEditingUseCase;

@Operation(summary = "공지 댓글 추가", description = "공지에 댓글을 추가합니다")
@SecurityRequirement(name = USER_TOKEN_HEADER_KEY)
Expand Down Expand Up @@ -58,4 +61,20 @@ public ResponseEntity<BaseResponse> createComment(

return ResponseEntity.ok().body(new BaseResponse<>(NOTICE_COMMENT_SAVE_SUCCESS, null));
}

@Operation(summary = "공지 댓글 편집", description = "공지에 있는 기존의 댓글을 수정합니다")
@SecurityRequirement(name = USER_TOKEN_HEADER_KEY)
@PostMapping("/{id}/comments/{commentId}")
public ResponseEntity<BaseResponse> getSupportedCategories(
@PathVariable("id") Long id,
@PathVariable("commentId") Long commentId,
@RequestHeader(USER_TOKEN_HEADER_KEY) String userToken,
@RequestBody NoticeCommentEditRequest request
) {
var command = new NoticeCommentEditingUseCase.EditCommentCommand(userToken, id, commentId, request.content());

noticeCommentEditingUseCase.process(command);

return ResponseEntity.ok().body(new BaseResponse<>(NOTICE_COMMENT_SAVE_SUCCESS, null));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.kustacks.kuring.notice.adapter.in.web.dto;

public record NoticeCommentEditRequest(
String content
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public Optional<CommentReadModel> findComment(Long id) {
return commentRepository.findReadModelById(id);
}

@Override
public Optional<Comment> findById(Long id) {
return commentRepository.findById(id);
}

@Override
public List<CommentReadModel> findExcludeSubCommentByCursor(Long noticeId, String cursor, int size) {
return commentRepository.findExcludeSubCommentByCursor(noticeId, cursor, size);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.kustacks.kuring.notice.application.port.in;

public interface NoticeCommentEditingUseCase {

void process(EditCommentCommand command);

record EditCommentCommand(
String userToken,
Long noticeId,
Long commentId,
String content
) {
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.kustacks.kuring.notice.application.port.out;

import com.kustacks.kuring.notice.application.port.out.dto.CommentReadModel;
import com.kustacks.kuring.notice.domain.Comment;

import java.util.List;
import java.util.Optional;
Expand All @@ -10,6 +11,8 @@ public interface CommentQueryPort {

Optional<CommentReadModel> findComment(Long id);

Optional<Comment> findById(Long id);

List<CommentReadModel> findExcludeSubCommentByCursor(Long noticeId, String cursor, int size);

List<CommentReadModel> findSubCommentByIds(Set<Long> parentCommentIds);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,27 @@
import com.kustacks.kuring.common.annotation.UseCase;
import com.kustacks.kuring.common.exception.NotFoundException;
import com.kustacks.kuring.common.exception.code.ErrorCode;
import com.kustacks.kuring.notice.application.port.in.NoticeCommentEditingUseCase;
import com.kustacks.kuring.notice.application.port.in.NoticeCommentWritingUseCase;
import com.kustacks.kuring.notice.application.port.out.CommentCommandPort;
import com.kustacks.kuring.notice.application.port.out.CommentQueryPort;
import com.kustacks.kuring.notice.application.port.out.NoticeQueryPort;
import com.kustacks.kuring.notice.application.port.out.dto.CommentReadModel;
import com.kustacks.kuring.notice.application.port.out.dto.NoticeDto;
import com.kustacks.kuring.notice.domain.Comment;
import com.kustacks.kuring.user.application.port.out.UserQueryPort;
import com.kustacks.kuring.user.domain.User;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

@Slf4j
@UseCase
@Transactional
@RequiredArgsConstructor
public class NoticeCommandService implements NoticeCommentWritingUseCase {
public class NoticeCommandService implements NoticeCommentWritingUseCase, NoticeCommentEditingUseCase {

private final NoticeQueryPort noticeQueryPort;
private final CommentCommandPort commentCommandPort;
Expand Down Expand Up @@ -54,4 +58,24 @@ public void process(WriteReplyCommand command) {

commentCommandPort.createReply(findUser.getId(), findNotice.getId(), command.parentId(), command.content());
}

@Override
public void process(EditCommentCommand command) {
User findUser = userQueryPort.findByToken(command.userToken())
.orElseThrow(() -> new NotFoundException(ErrorCode.USER_NOT_FOUND));

NoticeDto findNotice = noticeQueryPort.findNoticeById(command.noticeId())
.orElseThrow(() -> new NotFoundException(ErrorCode.NOTICE_NOT_FOUND));

Comment findComment = commentQueryPort.findById(command.commentId())
.orElseThrow(() -> new NotFoundException(ErrorCode.COMMENT_NOT_FOUND));

if (!Objects.equals(findComment.getUserId(), findUser.getId())
|| !Objects.equals(findComment.getNoticeId(), findNotice.getId())
|| findComment.getDestroyedAt() != null) {
throw new NotFoundException(ErrorCode.COMMENT_NOT_FOUND);
}

findComment.editContent(command.content());
}
}
4 changes: 4 additions & 0 deletions src/main/java/com/kustacks/kuring/notice/domain/Comment.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ public String getContent() {
return content.getValue();
}

public void editContent(String content) {
this.content = new Content(content);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down

0 comments on commit b66dea7

Please sign in to comment.