Skip to content

Commit

Permalink
Merge pull request #35 from goormthon-Univ/feature/31
Browse files Browse the repository at this point in the history
[Refactor] ๋Œ“๊ธ€, ๋งˆ์ดํŽ˜์ด์ง€ ๋ฆฌํŒฉํ† ๋ง
  • Loading branch information
Ahyun0326 committed Mar 22, 2024
2 parents 696703c + 2091bac commit b296f6e
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -66,7 +67,7 @@ public ResponseEntity<? super CommentsResponseDto> patchComment(@RequestParam("p

// ๋Œ“๊ธ€ ์‚ญ์ œ
@DeleteMapping("/posts/comments")
public ResponseEntity<? super CommentsResponseDto> deleteComment(@RequestParam("postId") Long postId, @RequestParam("commentId") Long commentId, @RequestHeader("Authorization") String token) {
public ResponseEntity<? super DeleteCommentsResponseDto> deleteComment(@RequestParam("postId") Long postId, @RequestParam("commentId") Long commentId, @RequestHeader("Authorization") String token) {
Long kakao_uid;
try {
kakao_uid = authService.getKakaoUserInfo(token).getId();
Expand All @@ -78,7 +79,7 @@ public ResponseEntity<? super CommentsResponseDto> deleteComment(@RequestParam("
return PostsResponseDto.databaseError();
}

ResponseEntity<? super CommentsResponseDto> response = commentService.deleteComment(postId, commentId, kakao_uid);
ResponseEntity<? super DeleteCommentsResponseDto> response = commentService.deleteComment(postId, commentId, kakao_uid);
return response;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<CommentsResponseDto> success() {
CommentsResponseDto result = new CommentsResponseDto();
public static ResponseEntity<CommentsResponseDto> success(String name, String profileImageUrl) {
CommentsResponseDto result = new CommentsResponseDto(name, profileImageUrl);
return ResponseEntity.status(HttpStatus.OK).body(result);
}

Expand All @@ -32,4 +38,9 @@ public static ResponseEntity<? super CommentsResponseDto> notExistedComment() {
ResponseDto result = new ResponseDto(ResponseCode.NOT_EXISTED_COMMENT, ResponseMessage.NOT_EXISTED_COMMENT);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(result);
}

public static ResponseEntity<ResponseDto> noPermission() {
ResponseDto result = new ResponseDto(ResponseCode.NO_PERMISSION, ResponseMessage.NO_PERMISSION);
return ResponseEntity.status(HttpStatus.FORBIDDEN).body(result);
}
}
Original file line number Diff line number Diff line change
@@ -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<DeleteCommentsResponseDto> success() {
DeleteCommentsResponseDto result = new DeleteCommentsResponseDto();
return ResponseEntity.status(HttpStatus.OK).body(result);
}

public static ResponseEntity<ResponseDto> notExistedPost(){
ResponseDto result = new ResponseDto(ResponseCode.NOT_EXISTED_POST, ResponseMessage.NOT_EXISTED_POST);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(result);
}

public static ResponseEntity<ResponseDto> notExistedUser() {
ResponseDto result = new ResponseDto(ResponseCode.NOT_EXISTED_USER, ResponseMessage.NOT_EXISTED_USER);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(result);
}

public static ResponseEntity<? super DeleteCommentsResponseDto> notExistedComment() {
ResponseDto result = new ResponseDto(ResponseCode.NOT_EXISTED_COMMENT, ResponseMessage.NOT_EXISTED_COMMENT);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(result);
}

public static ResponseEntity<ResponseDto> noPermission() {
ResponseDto result = new ResponseDto(ResponseCode.NO_PERMISSION, ResponseMessage.NO_PERMISSION);
return ResponseEntity.status(HttpStatus.FORBIDDEN).body(result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<GetMyPageInfoResponseDto> success(Users user, String email) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -53,16 +54,14 @@ public ResponseEntity<? super CommentsResponseDto> saveComment(Long postId, Long

// postRespo์˜ commentCount ์—…๋ฐ์ดํŠธ
int commentCount = commentRepo.countByPostId(postId);
List<Posts> 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<? super GetCommentsAllResponseDto> getAllComment(Long postId, int page) {
Expand Down Expand Up @@ -96,28 +95,35 @@ public ResponseEntity<? super CommentsResponseDto> 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<? super CommentsResponseDto> deleteComment(Long postId, Long commentId, Long userUid) {
public ResponseEntity<? super DeleteCommentsResponseDto> 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 ์—…๋ฐ์ดํŠธ
Expand All @@ -129,6 +135,6 @@ public ResponseEntity<? super CommentsResponseDto> deleteComment(Long postId, Lo
log.info("error " + exception.getMessage());
return ResponseDto.databaseError();
}
return CommentsResponseDto.success();
return DeleteCommentsResponseDto.success();
}
}

0 comments on commit b296f6e

Please sign in to comment.