From 08ae59859d3c22bc1c3626f6938264acba255b26 Mon Sep 17 00:00:00 2001 From: Yeseo Date: Fri, 22 Mar 2024 14:02:58 +0900 Subject: [PATCH 1/5] refactor: Modify CommentController (#31) --- .../beotkkot/qtudy/controller/comment/CommentController.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) 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; } } From 1d34aff1478c188748a1723b8011c2070726a7a6 Mon Sep 17 00:00:00 2001 From: Yeseo Date: Fri, 22 Mar 2024 14:03:27 +0900 Subject: [PATCH 2/5] refactor: Modify CommentService (#31) --- .../service/comments/CommentService.java | 30 +++++++++++-------- 1 file changed, 18 insertions(+), 12 deletions(-) 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(); } } From be65c411909303325a95dffdf82c346b12ba1923 Mon Sep 17 00:00:00 2001 From: Yeseo Date: Fri, 22 Mar 2024 14:05:15 +0900 Subject: [PATCH 3/5] refactor: Modify profileImageUrl (#31) --- .../qtudy/dto/response/mypage/GetMyPageInfoResponseDto.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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) { From 30b7df82346150a95ad6d2b881ef68a885bf4111 Mon Sep 17 00:00:00 2001 From: Yeseo Date: Fri, 22 Mar 2024 14:05:47 +0900 Subject: [PATCH 4/5] feat: Add DeleteCommentsResponseDto (#31) --- .../comments/DeleteCommentsResponseDto.java | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/main/java/com/beotkkot/qtudy/dto/response/comments/DeleteCommentsResponseDto.java 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); + } +} From 2091bacaf5b4e7b1f3b093811924372eeba36576 Mon Sep 17 00:00:00 2001 From: Yeseo Date: Fri, 22 Mar 2024 14:06:40 +0900 Subject: [PATCH 5/5] feat: Modify CommentsResponseDto (#31) --- .../response/comments/CommentsResponseDto.java | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) 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); + } }