diff --git a/src/main/java/com/beotkkot/qtudy/controller/comment/CommentController.java b/src/main/java/com/beotkkot/qtudy/controller/comment/CommentController.java index 60676cf..332cb3f 100644 --- a/src/main/java/com/beotkkot/qtudy/controller/comment/CommentController.java +++ b/src/main/java/com/beotkkot/qtudy/controller/comment/CommentController.java @@ -2,6 +2,7 @@ import com.beotkkot.qtudy.dto.request.comments.CommentsRequestDto; import com.beotkkot.qtudy.dto.response.comments.CommentsResponseDto; +import com.beotkkot.qtudy.dto.response.comments.DeleteCommentsResponseDto; import com.beotkkot.qtudy.dto.response.comments.GetCommentsAllResponseDto; import com.beotkkot.qtudy.dto.response.posts.PostsResponseDto; import com.beotkkot.qtudy.service.auth.AuthService; @@ -66,7 +67,7 @@ public ResponseEntity patchComment(@RequestParam("p // 댓글 삭제 @DeleteMapping("/posts/comments") - public ResponseEntity deleteComment(@RequestParam("postId") Long postId, @RequestParam("commentId") Long commentId, @RequestHeader("Authorization") String token) { + public ResponseEntity deleteComment(@RequestParam("postId") Long postId, @RequestParam("commentId") Long commentId, @RequestHeader("Authorization") String token) { Long kakao_uid; try { kakao_uid = authService.getKakaoUserInfo(token).getId(); @@ -78,7 +79,7 @@ public ResponseEntity deleteComment(@RequestParam(" return PostsResponseDto.databaseError(); } - ResponseEntity response = commentService.deleteComment(postId, commentId, kakao_uid); + ResponseEntity response = commentService.deleteComment(postId, commentId, kakao_uid); return response; } } diff --git a/src/main/java/com/beotkkot/qtudy/dto/response/comments/CommentsResponseDto.java b/src/main/java/com/beotkkot/qtudy/dto/response/comments/CommentsResponseDto.java index 0dce2c4..12dedc0 100644 --- a/src/main/java/com/beotkkot/qtudy/dto/response/comments/CommentsResponseDto.java +++ b/src/main/java/com/beotkkot/qtudy/dto/response/comments/CommentsResponseDto.java @@ -9,12 +9,18 @@ @Getter public class CommentsResponseDto extends ResponseDto{ - public CommentsResponseDto() { + + private String name; + private String profileImageUrl; + + public CommentsResponseDto(String name, String profileImageUrl) { super(ResponseCode.SUCCESS, ResponseMessage.SUCCESS); + this.name = name; + this.profileImageUrl = profileImageUrl; } - public static ResponseEntity success() { - CommentsResponseDto result = new CommentsResponseDto(); + public static ResponseEntity success(String name, String profileImageUrl) { + CommentsResponseDto result = new CommentsResponseDto(name, profileImageUrl); return ResponseEntity.status(HttpStatus.OK).body(result); } @@ -32,4 +38,9 @@ public static ResponseEntity notExistedComment() { ResponseDto result = new ResponseDto(ResponseCode.NOT_EXISTED_COMMENT, ResponseMessage.NOT_EXISTED_COMMENT); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(result); } + + public static ResponseEntity noPermission() { + ResponseDto result = new ResponseDto(ResponseCode.NO_PERMISSION, ResponseMessage.NO_PERMISSION); + return ResponseEntity.status(HttpStatus.FORBIDDEN).body(result); + } } diff --git a/src/main/java/com/beotkkot/qtudy/dto/response/comments/DeleteCommentsResponseDto.java b/src/main/java/com/beotkkot/qtudy/dto/response/comments/DeleteCommentsResponseDto.java new file mode 100644 index 0000000..d623266 --- /dev/null +++ b/src/main/java/com/beotkkot/qtudy/dto/response/comments/DeleteCommentsResponseDto.java @@ -0,0 +1,40 @@ +package com.beotkkot.qtudy.dto.response.comments; + +import com.beotkkot.qtudy.common.ResponseCode; +import com.beotkkot.qtudy.common.ResponseMessage; +import com.beotkkot.qtudy.dto.response.ResponseDto; +import lombok.Getter; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; + +@Getter +public class DeleteCommentsResponseDto extends ResponseDto{ + public DeleteCommentsResponseDto() { + super(ResponseCode.SUCCESS, ResponseMessage.SUCCESS); + } + + public static ResponseEntity success() { + DeleteCommentsResponseDto result = new DeleteCommentsResponseDto(); + return ResponseEntity.status(HttpStatus.OK).body(result); + } + + public static ResponseEntity notExistedPost(){ + ResponseDto result = new ResponseDto(ResponseCode.NOT_EXISTED_POST, ResponseMessage.NOT_EXISTED_POST); + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(result); + } + + public static ResponseEntity notExistedUser() { + ResponseDto result = new ResponseDto(ResponseCode.NOT_EXISTED_USER, ResponseMessage.NOT_EXISTED_USER); + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(result); + } + + public static ResponseEntity notExistedComment() { + ResponseDto result = new ResponseDto(ResponseCode.NOT_EXISTED_COMMENT, ResponseMessage.NOT_EXISTED_COMMENT); + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(result); + } + + public static ResponseEntity noPermission() { + ResponseDto result = new ResponseDto(ResponseCode.NO_PERMISSION, ResponseMessage.NO_PERMISSION); + return ResponseEntity.status(HttpStatus.FORBIDDEN).body(result); + } +} diff --git a/src/main/java/com/beotkkot/qtudy/dto/response/mypage/GetMyPageInfoResponseDto.java b/src/main/java/com/beotkkot/qtudy/dto/response/mypage/GetMyPageInfoResponseDto.java index 802739b..d90c61b 100644 --- a/src/main/java/com/beotkkot/qtudy/dto/response/mypage/GetMyPageInfoResponseDto.java +++ b/src/main/java/com/beotkkot/qtudy/dto/response/mypage/GetMyPageInfoResponseDto.java @@ -21,7 +21,7 @@ private GetMyPageInfoResponseDto(Users user, String email) { super(ResponseCode.SUCCESS, ResponseMessage.SUCCESS); this.name = user.getName(); this.email = email; - this.profileImageUrl = null; + this.profileImageUrl = user.getProfileImageUrl(); } public static ResponseEntity success(Users user, String email) { diff --git a/src/main/java/com/beotkkot/qtudy/service/comments/CommentService.java b/src/main/java/com/beotkkot/qtudy/service/comments/CommentService.java index be0776a..49f1d1b 100644 --- a/src/main/java/com/beotkkot/qtudy/service/comments/CommentService.java +++ b/src/main/java/com/beotkkot/qtudy/service/comments/CommentService.java @@ -6,6 +6,7 @@ import com.beotkkot.qtudy.dto.request.comments.CommentsRequestDto; import com.beotkkot.qtudy.dto.response.ResponseDto; import com.beotkkot.qtudy.dto.response.comments.CommentsResponseDto; +import com.beotkkot.qtudy.dto.response.comments.DeleteCommentsResponseDto; import com.beotkkot.qtudy.dto.response.comments.GetCommentsAllResponseDto; import com.beotkkot.qtudy.repository.comments.CommentsRepository; import com.beotkkot.qtudy.repository.posts.PostsRepository; @@ -53,16 +54,14 @@ public ResponseEntity saveComment(Long postId, Long // postRespo의 commentCount 업데이트 int commentCount = commentRepo.countByPostId(postId); - List posts = postRepo.findAllByPostId(postId); - for (Posts post : posts) { - post.setCommentCount(commentCount); - } + Posts post = postRepo.findByPostId(postId); + post.setCommentCount(commentCount); } } catch (Exception exception) { log.info("error " + exception.getMessage()); return ResponseDto.databaseError(); } - return CommentsResponseDto.success(); + return CommentsResponseDto.success(userRepo.findByKakaoId(userUid).getName(), userRepo.findByKakaoId(userUid).getProfileImageUrl()); } public ResponseEntity getAllComment(Long postId, int page) { @@ -96,28 +95,35 @@ public ResponseEntity patchComment(Long postId, Lon } else { // 댓글 수정 Comments comment = commentRepo.findById(commentId).get(); + System.out.println("comment.getUserUid() = " + comment.getUserUid()); + System.out.println("userUid = " + userUid); + if (!comment.getUserUid().equals(userUid)) { + return CommentsResponseDto.noPermission(); + } comment.setContent(dto.getContent()); - System.out.println("content = " + comment.getContent()); } } catch (Exception exception) { log.info("error " + exception.getMessage()); return ResponseDto.databaseError(); } - return CommentsResponseDto.success(); + return CommentsResponseDto.success(userRepo.findByKakaoId(userUid).getName(), userRepo.findByKakaoId(userUid).getProfileImageUrl()); } @Transactional - public ResponseEntity deleteComment(Long postId, Long commentId, Long userUid) { + public ResponseEntity deleteComment(Long postId, Long commentId, Long userUid) { try { if (userRepo.findByKakaoId(userUid) == null) { - return CommentsResponseDto.notExistedUser(); + return DeleteCommentsResponseDto.notExistedUser(); } else if (!postRepo.existsById(postId)) { - return CommentsResponseDto.notExistedPost(); + return DeleteCommentsResponseDto.notExistedPost(); } else if (!commentRepo.existsById(commentId)) { - return CommentsResponseDto.notExistedComment(); + return DeleteCommentsResponseDto.notExistedComment(); } else { // 댓글 삭제 Comments comment = commentRepo.findById(commentId).get(); + if (!comment.getUserUid().equals(userUid)) { + return DeleteCommentsResponseDto.noPermission(); + } commentRepo.delete(comment); // postRespo의 commentCount 업데이트 @@ -129,6 +135,6 @@ public ResponseEntity deleteComment(Long postId, Lo log.info("error " + exception.getMessage()); return ResponseDto.databaseError(); } - return CommentsResponseDto.success(); + return DeleteCommentsResponseDto.success(); } }