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
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public enum ErrorStatus implements BaseErrorCode {
_NOT_USER_POST(HttpStatus.NOT_FOUND, "POST402", "해당 유저의 게시글이 아닙니다."),

//댓글 관련 에러
_NOT_FOUND_COMMENT(HttpStatus.NOT_FOUND, "COMMENT404", "해당 댓글을 찾을 수 없습니다.");
_NOT_FOUND_COMMENT(HttpStatus.NOT_FOUND, "COMMENT401", "해당 댓글을 찾을 수 없습니다."),
_NOT_USER_COMMENT(HttpStatus.NOT_FOUND, "COMMENT402", "해당 유저의 댓글이 아닙니다.");

private final HttpStatus httpStatus;
private final String code;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import com.example.be.domain.Post;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface CommentRepository extends JpaRepository<Comment, Long> {

Optional<Comment> findByIdAndUserId(Long id, Long userId);
}
13 changes: 13 additions & 0 deletions src/main/java/com/example/be/service/CommentServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,17 @@ public CommonDTO.IsSuccessDTO createComment(CommentDTO.CommentRequestDTO request
commentRepository.save(comment);
return CommonDTO.IsSuccessDTO.builder().isSuccess(true).build();
}

public CommonDTO.IsSuccessDTO deleteComment(Long commentId, HttpServletRequest request) {
User user= getUserFromRequest(request);

Comment comment = commentRepository.findById(commentId).orElseThrow(()->
new CommentHandler(ErrorStatus._NOT_FOUND_COMMENT));

commentRepository.findByIdAndUserId(commentId, user.getId()).orElseThrow(() ->
new CommentHandler(ErrorStatus._NOT_USER_COMMENT));

commentRepository.delete(comment);
return CommonDTO.IsSuccessDTO.builder().isSuccess(true).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,12 @@ public ApiResponse<CommentDTO.CommentLikeResponseDTO> toggleCommentLike(
HttpServletRequest request) {
return ApiResponse.onSuccess(commentLikeService.toggleCommentLike(commentId, request));
}

@PostMapping("/{commentId}/delete")
@Operation(summary = "댓글 삭제 API", description = "댓글을 삭제합니다.")
public ApiResponse<CommonDTO.IsSuccessDTO> deleteComment(
@Parameter(description = "댓글 ID") @PathVariable Long commentId,
HttpServletRequest request) {
return ApiResponse.onSuccess(commentService.deleteComment(commentId, request));
}
}