Skip to content

Commit

Permalink
#27 feat: Comment 수정 API구현
Browse files Browse the repository at this point in the history
  • Loading branch information
xhaktmchl committed Nov 3, 2022
1 parent e89e93b commit ebdc9ba
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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;
Expand Down Expand Up @@ -81,4 +82,26 @@ public ApplicationResponse<List<CommentRes>> findAllComments(@PathVariable("clip
public ApplicationResponse<DeleteCommentRes> 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<CommentRes> updateComment(@PathVariable("commentId") Long commentId, @RequestBody @Validated PatchCommentReq patchCommentReq){
return commentService.updateComment(patchCommentReq, commentId);
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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;
Expand Down Expand Up @@ -42,4 +43,8 @@ public Comment(CreateCommentReq dto, User user, ClipBoard clipBoard) {
public void deleteComment(){
this.setStatus(BaseStatus.INACTIVE);
}

public void updateComment(PatchCommentReq dto){
this.content = dto.getContent();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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;
Expand All @@ -15,4 +16,6 @@ public interface CommentService {
ApplicationResponse<List<CommentRes>> findAllComments(Long clipBoardId, Long userId);

ApplicationResponse<DeleteCommentRes> deleteComment(DeleteCommentReq deleteCommentReq, Long commentId);

ApplicationResponse<CommentRes> updateComment(PatchCommentReq patchCommentReq, Long commentId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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;
Expand Down Expand Up @@ -86,4 +87,25 @@ public ApplicationResponse<DeleteCommentRes> deleteComment(DeleteCommentReq dto,
DeleteCommentRes deleteCommentRes = DeleteCommentRes.toDto(comment);
return ApplicationResponse.ok(deleteCommentRes);
}


@Transactional(readOnly = false)
@Override
public ApplicationResponse<CommentRes> 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);
}
}

0 comments on commit ebdc9ba

Please sign in to comment.