diff --git a/server/src/main/java/com/yogit/server/board/controller/CommenrController.java b/server/src/main/java/com/yogit/server/board/controller/CommenrController.java index 09fba83..456db49 100644 --- a/server/src/main/java/com/yogit/server/board/controller/CommenrController.java +++ b/server/src/main/java/com/yogit/server/board/controller/CommenrController.java @@ -1,7 +1,10 @@ package com.yogit.server.board.controller; import com.yogit.server.board.dto.request.comment.CreateCommentReq; +import com.yogit.server.board.dto.request.comment.DeleteCommentReq; +import com.yogit.server.board.dto.request.comment.PatchCommentReq; import com.yogit.server.board.dto.response.comment.CommentRes; +import com.yogit.server.board.dto.response.comment.DeleteCommentRes; import com.yogit.server.board.service.comment.CommentService; import com.yogit.server.global.dto.ApplicationResponse; import com.yogit.server.user.entity.Gender; @@ -57,4 +60,48 @@ public ApplicationResponse createComment(@RequestBody @Validated Cre public ApplicationResponse> findAllComments(@PathVariable("clipBoardId") Long clipBoardId, @PathVariable("userId") Long userId){ return commentService.findAllComments(clipBoardId, userId); } + + + /** + * 코멘트 삭제 + * @author 토마스 + */ + @ApiOperation(value = "코멘트 삭제", notes = "코멘트 아이디를 입력해 코멘트 삭제 요청.") + @ApiResponses({ + @ApiResponse(code= 201, message = "요청에 성공하였습니다."), + @ApiResponse(code= 404, message = "존재하지 않는 유저입니다."), + @ApiResponse(code= 404, message = "존재하지 않는 클립보드입니다."), + @ApiResponse(code= 404, message = "존재하지 않는 코멘트입니다."), + @ApiResponse(code = 4000 , message = "서버 오류입니다.") + }) + @ApiImplicitParams({ + @ApiImplicitParam(name = "userId", required = true, dataTypeClass = Long.class, example = "1"), + @ApiImplicitParam(name = "commentId", required = true, dataTypeClass = Long.class, example = "1") + }) + @PatchMapping("/{commentId}") + public ApplicationResponse deleteComment(@PathVariable("commentId") Long commentId, @RequestBody @Validated DeleteCommentReq deleteCommentReq){ + return commentService.deleteComment(deleteCommentReq, commentId); + } + + + /** + * 코멘트 수정 + * @author 토마스 + */ + @ApiOperation(value = "코멘트 수정", notes = "코멘트 아이디, 수정할 내용을 입력해 코멘트 수정 요청.") + @ApiResponses({ + @ApiResponse(code= 201, message = "요청에 성공하였습니다."), + @ApiResponse(code= 404, message = "존재하지 않는 유저입니다."), + @ApiResponse(code= 404, message = "존재하지 않는 클립보드입니다."), + @ApiResponse(code= 404, message = "존재하지 않는 코멘트입니다."), + @ApiResponse(code = 4000 , message = "서버 오류입니다.") + }) + @ApiImplicitParams({ + @ApiImplicitParam(name = "userId", required = true, dataTypeClass = Long.class, example = "1"), + @ApiImplicitParam(name = "commentId", required = true, dataTypeClass = Long.class, example = "1") + }) + @PatchMapping("/{commentId}/content") + public ApplicationResponse updateComment(@PathVariable("commentId") Long commentId, @RequestBody @Validated PatchCommentReq patchCommentReq){ + return commentService.updateComment(patchCommentReq, commentId); + } } diff --git a/server/src/main/java/com/yogit/server/board/dto/request/comment/DeleteCommentReq.java b/server/src/main/java/com/yogit/server/board/dto/request/comment/DeleteCommentReq.java new file mode 100644 index 0000000..3a5cb5d --- /dev/null +++ b/server/src/main/java/com/yogit/server/board/dto/request/comment/DeleteCommentReq.java @@ -0,0 +1,15 @@ +package com.yogit.server.board.dto.request.comment; + +import io.swagger.annotations.ApiModelProperty; +import io.swagger.annotations.ApiParam; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +public class DeleteCommentReq { + + @ApiModelProperty(example = "1") + @ApiParam(value = "유저 ID", required = true) + private Long userId; +} diff --git a/server/src/main/java/com/yogit/server/board/dto/request/comment/PatchCommentReq.java b/server/src/main/java/com/yogit/server/board/dto/request/comment/PatchCommentReq.java new file mode 100644 index 0000000..b6ad979 --- /dev/null +++ b/server/src/main/java/com/yogit/server/board/dto/request/comment/PatchCommentReq.java @@ -0,0 +1,26 @@ +package com.yogit.server.board.dto.request.comment; + +import io.swagger.annotations.ApiModelProperty; +import io.swagger.annotations.ApiParam; +import lombok.Data; +import lombok.NoArgsConstructor; + +import javax.validation.constraints.NotBlank; + +@Data +@NoArgsConstructor +public class PatchCommentReq { + + @ApiModelProperty(example = "1") + @ApiParam(value = "유저 ID", required = true) + private Long userId; + + @ApiModelProperty(example = "1") + @ApiParam(value = "코멘트 ID", required = true) + private Long commentId; + + @ApiModelProperty(example = "경복궁역 몇 번 출구인가요?") + @ApiParam(value = "클립보드 상세 내용", required = true) + @NotBlank + private String content; +} diff --git a/server/src/main/java/com/yogit/server/board/dto/response/comment/CommentRes.java b/server/src/main/java/com/yogit/server/board/dto/response/comment/CommentRes.java index cb75e1e..5b6ff9c 100644 --- a/server/src/main/java/com/yogit/server/board/dto/response/comment/CommentRes.java +++ b/server/src/main/java/com/yogit/server/board/dto/response/comment/CommentRes.java @@ -2,6 +2,7 @@ import com.yogit.server.board.entity.ClipBoard; import com.yogit.server.board.entity.Comment; +import com.yogit.server.config.domain.BaseStatus; import com.yogit.server.user.entity.User; import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiParam; @@ -37,6 +38,18 @@ public class CommentRes { @ApiParam(value = "ClipBoard ID") private Long clipBoardId; + @ApiModelProperty(example = "ACTIVE") + @ApiParam(value = "객체 상태") + private BaseStatus status; + + @ApiModelProperty(example = "2022-07-13 16:29:30") + @ApiParam(value = "생성 시각") + private String createdAt; + + @ApiModelProperty(example = "2022-07-13 16:29:30") + @ApiParam(value = "마지막 업데이트 시각") + private String updatedAt; + public static CommentRes toDto(Comment comment){ return CommentRes.builder() .commentId(comment.getId()) @@ -45,16 +58,22 @@ public static CommentRes toDto(Comment comment){ .userName(comment.getUser().getName()) .profileImg(comment.getUser().getProfileImg()) .clipBoardId(comment.getClipBoard().getId()) + .status(comment.getStatus()) + .createdAt(comment.getCreatedAt()) + .updatedAt(comment.getUpdatedAt()) .build(); } @Builder - public CommentRes(Long commentId, String content, Long userId, String userName, String profileImg, Long clipBoardId) { + public CommentRes(Long commentId, String content, Long userId, String userName, String profileImg, Long clipBoardId, BaseStatus status, String createdAt, String updatedAt) { this.commentId = commentId; this.content = content; this.userId = userId; this.userName = userName; this.profileImg = profileImg; this.clipBoardId = clipBoardId; + this.status = status; + this.createdAt = createdAt; + this.updatedAt = updatedAt; } } diff --git a/server/src/main/java/com/yogit/server/board/dto/response/comment/DeleteCommentRes.java b/server/src/main/java/com/yogit/server/board/dto/response/comment/DeleteCommentRes.java new file mode 100644 index 0000000..2a49e46 --- /dev/null +++ b/server/src/main/java/com/yogit/server/board/dto/response/comment/DeleteCommentRes.java @@ -0,0 +1,50 @@ +package com.yogit.server.board.dto.response.comment; + +import com.yogit.server.board.entity.Comment; +import com.yogit.server.config.domain.BaseStatus; +import io.swagger.annotations.ApiModelProperty; +import io.swagger.annotations.ApiParam; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +public class DeleteCommentRes { + + @ApiModelProperty(example = "1") + @ApiParam(value = "코멘트 ID") + private Long commentId; + + @ApiModelProperty(example = "ACTIVE") + @ApiParam(value = "객체 상태") + private BaseStatus status; + + @ApiModelProperty(example = "2022-07-13 16:29:30") + @ApiParam(value = "생성 시각") + private String createdAt; + + @ApiModelProperty(example = "2022-07-13 16:29:30") + @ApiParam(value = "마지막 업데이트 시각") + private String updatedAt; + + /* + 연관관계 편의 메서드 + */ + public static DeleteCommentRes toDto(Comment comment){ + return DeleteCommentRes.builder() + .commentId(comment.getId()) + .status(comment.getStatus()) + .createdAt(comment.getCreatedAt()) + .updatedAt(comment.getUpdatedAt()) + .build(); + } + + @Builder + public DeleteCommentRes(Long commentId, BaseStatus status, String createdAt, String updatedAt) { + this.commentId = commentId; + this.status = status; + this.createdAt = createdAt; + this.updatedAt = updatedAt; + } +} diff --git a/server/src/main/java/com/yogit/server/board/entity/Comment.java b/server/src/main/java/com/yogit/server/board/entity/Comment.java index 5930884..1a59e51 100644 --- a/server/src/main/java/com/yogit/server/board/entity/Comment.java +++ b/server/src/main/java/com/yogit/server/board/entity/Comment.java @@ -1,7 +1,9 @@ package com.yogit.server.board.entity; import com.yogit.server.board.dto.request.comment.CreateCommentReq; +import com.yogit.server.board.dto.request.comment.PatchCommentReq; import com.yogit.server.config.domain.BaseEntity; +import com.yogit.server.config.domain.BaseStatus; import com.yogit.server.user.entity.User; import lombok.AccessLevel; import lombok.Getter; @@ -37,4 +39,12 @@ public Comment(CreateCommentReq dto, User user, ClipBoard clipBoard) { this.user = user; this.clipBoard = clipBoard; } + + public void deleteComment(){ + this.setStatus(BaseStatus.INACTIVE); + } + + public void updateComment(PatchCommentReq dto){ + this.content = dto.getContent(); + } } diff --git a/server/src/main/java/com/yogit/server/board/exception/comment/CommentExceptionList.java b/server/src/main/java/com/yogit/server/board/exception/comment/CommentExceptionList.java index a1dc65a..3a96ef0 100644 --- a/server/src/main/java/com/yogit/server/board/exception/comment/CommentExceptionList.java +++ b/server/src/main/java/com/yogit/server/board/exception/comment/CommentExceptionList.java @@ -10,7 +10,8 @@ @RequiredArgsConstructor public enum CommentExceptionList { - NOT_FOUND_COMMENT("CM0001", NOT_FOUND, "존재하지 않는 Comment입니다."); + NOT_FOUND_COMMENT("CM0001", NOT_FOUND, "존재하지 않는 Comment입니다."), + NOT_HOST_OF_COMMENT("CM0002", HttpStatus.BAD_REQUEST, "요청한 유저가 코멘트의 호스트가 아닙니다."); private final String CODE; private final HttpStatus HTTPSTATUS; diff --git a/server/src/main/java/com/yogit/server/board/exception/comment/NotHostOfCommentException.java b/server/src/main/java/com/yogit/server/board/exception/comment/NotHostOfCommentException.java new file mode 100644 index 0000000..3a70299 --- /dev/null +++ b/server/src/main/java/com/yogit/server/board/exception/comment/NotHostOfCommentException.java @@ -0,0 +1,9 @@ +package com.yogit.server.board.exception.comment; + + +public class NotHostOfCommentException extends CommentException{ + public NotHostOfCommentException(){ + super(CommentExceptionList.NOT_FOUND_COMMENT.getCODE(), CommentExceptionList.NOT_FOUND_COMMENT.getHTTPSTATUS(), CommentExceptionList.NOT_FOUND_COMMENT.getMESSAGE()); + } + +} diff --git a/server/src/main/java/com/yogit/server/board/repository/CommentRepository.java b/server/src/main/java/com/yogit/server/board/repository/CommentRepository.java index 9ced032..06da19b 100644 --- a/server/src/main/java/com/yogit/server/board/repository/CommentRepository.java +++ b/server/src/main/java/com/yogit/server/board/repository/CommentRepository.java @@ -6,9 +6,13 @@ import org.springframework.data.repository.query.Param; import java.util.List; +import java.util.Optional; public interface CommentRepository extends JpaRepository { @Query("select cm from Comment cm where cm.clipBoard.id = :clipBoardId and cm.status = 'ACTIVE'") List findAllCommentsByClipBoardId(@Param("clipBoardId") Long clipBoardId); + + @Query("select cm from Comment cm where cm.id = :commentId and cm.status='ACTIVE'") + Optional findCommentById(@Param("commentId") Long commentId); } diff --git a/server/src/main/java/com/yogit/server/board/service/comment/CommentService.java b/server/src/main/java/com/yogit/server/board/service/comment/CommentService.java index e4a3cc2..68e3e2f 100644 --- a/server/src/main/java/com/yogit/server/board/service/comment/CommentService.java +++ b/server/src/main/java/com/yogit/server/board/service/comment/CommentService.java @@ -1,7 +1,10 @@ package com.yogit.server.board.service.comment; import com.yogit.server.board.dto.request.comment.CreateCommentReq; +import com.yogit.server.board.dto.request.comment.DeleteCommentReq; +import com.yogit.server.board.dto.request.comment.PatchCommentReq; import com.yogit.server.board.dto.response.comment.CommentRes; +import com.yogit.server.board.dto.response.comment.DeleteCommentRes; import com.yogit.server.global.dto.ApplicationResponse; import java.util.List; @@ -11,4 +14,8 @@ public interface CommentService { ApplicationResponse createComment(CreateCommentReq createCommentReq); ApplicationResponse> findAllComments(Long clipBoardId, Long userId); + + ApplicationResponse deleteComment(DeleteCommentReq deleteCommentReq, Long commentId); + + ApplicationResponse updateComment(PatchCommentReq patchCommentReq, Long commentId); } diff --git a/server/src/main/java/com/yogit/server/board/service/comment/CommentServiceImpl.java b/server/src/main/java/com/yogit/server/board/service/comment/CommentServiceImpl.java index f0feafa..e3932db 100644 --- a/server/src/main/java/com/yogit/server/board/service/comment/CommentServiceImpl.java +++ b/server/src/main/java/com/yogit/server/board/service/comment/CommentServiceImpl.java @@ -1,10 +1,15 @@ package com.yogit.server.board.service.comment; import com.yogit.server.board.dto.request.comment.CreateCommentReq; +import com.yogit.server.board.dto.request.comment.DeleteCommentReq; +import com.yogit.server.board.dto.request.comment.PatchCommentReq; +import com.yogit.server.board.dto.response.comment.DeleteCommentRes; import com.yogit.server.board.dto.response.comment.CommentRes; import com.yogit.server.board.entity.ClipBoard; import com.yogit.server.board.entity.Comment; import com.yogit.server.board.exception.clipboard.NotFoundClipBoardException; +import com.yogit.server.board.exception.comment.NotFoundCommentException; +import com.yogit.server.board.exception.comment.NotHostOfCommentException; import com.yogit.server.board.repository.ClipBoardRepository; import com.yogit.server.board.repository.CommentRepository; import com.yogit.server.global.dto.ApplicationResponse; @@ -61,4 +66,46 @@ public ApplicationResponse> findAllComments(Long clipBoardId, L return ApplicationResponse.ok(commentResList); } + + + @Transactional(readOnly = false) + @Override + public ApplicationResponse deleteComment(DeleteCommentReq dto, Long commentId){ + + User user = userRepository.findById(dto.getUserId()) + .orElseThrow(() -> new NotFoundUserException()); + + Comment comment = commentRepository.findCommentById(commentId) + .orElseThrow(() -> new NotFoundCommentException()); + + //검증: 요청 유저가 코멘트를 생성한 사람인지 + if(!user.getId().equals(comment.getUser().getId())){ + throw new NotHostOfCommentException(); + } + + comment.deleteComment(); + DeleteCommentRes deleteCommentRes = DeleteCommentRes.toDto(comment); + return ApplicationResponse.ok(deleteCommentRes); + } + + + @Transactional(readOnly = false) + @Override + public ApplicationResponse updateComment(PatchCommentReq dto, Long commentId){ + + User user = userRepository.findById(dto.getUserId()) + .orElseThrow(() -> new NotFoundUserException()); + + Comment comment = commentRepository.findCommentById(dto.getCommentId()) + .orElseThrow(() -> new NotFoundCommentException()); + + //검증: 요청 유저가 코멘트를 생성한 사람인지 + if(!user.getId().equals(comment.getUser().getId())){ + throw new NotHostOfCommentException(); + } + + comment.updateComment(dto); + CommentRes commentRes = CommentRes.toDto(comment); + return ApplicationResponse.ok(commentRes); + } }