diff --git a/src/main/java/com/v1/matripserver/comment/controller/CommentController.java b/src/main/java/com/v1/matripserver/comment/controller/CommentController.java index 517502a..8c22c2d 100644 --- a/src/main/java/com/v1/matripserver/comment/controller/CommentController.java +++ b/src/main/java/com/v1/matripserver/comment/controller/CommentController.java @@ -10,6 +10,7 @@ import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestController; @@ -39,18 +40,18 @@ public void createComment(@RequestBody CommentRequestDto commentRequestDto){ // 댓글 조회 @GetMapping("") - public ResponseEntity> readComment(CommentRequestDto commentRequestDto) { + public ResponseEntity> readComment(CommentRequestDto commentRequestDto, @RequestHeader(value = "Authorization") String accessToken) { - List commentResponseDtoList = commentService.readComment(commentRequestDto); + List commentResponseDtoList = commentService.readComment(commentRequestDto, accessToken); return ResponseEntity.ok(commentResponseDtoList); } // 댓글 삭제 @DeleteMapping("/{id}") @ResponseStatus(HttpStatus.NO_CONTENT) - public void deleteComment(@PathVariable Long id){ + public void deleteComment(@PathVariable Long id, @RequestHeader(value = "Authorization") String accessToken){ - commentService.deleteComment(id); + commentService.deleteComment(id, accessToken); } // 댓글 수정 diff --git a/src/main/java/com/v1/matripserver/comment/service/CommentService.java b/src/main/java/com/v1/matripserver/comment/service/CommentService.java index 75e43b8..af988d7 100644 --- a/src/main/java/com/v1/matripserver/comment/service/CommentService.java +++ b/src/main/java/com/v1/matripserver/comment/service/CommentService.java @@ -8,6 +8,7 @@ import org.springframework.http.HttpStatus; import org.springframework.stereotype.Service; +import com.v1.matripserver.auth.JwtTokenUtil; import com.v1.matripserver.comment.dto.CommentRequestDto; import com.v1.matripserver.comment.dto.CommentResponseDto; import com.v1.matripserver.comment.entity.Comment; @@ -34,6 +35,8 @@ public class CommentService { private final MemberService memberService; + private static String secretKey = "test001"; + // 댓글 작성 public void createComment(CommentRequestDto commentRequestDto){ @@ -80,11 +83,13 @@ public void createComment(CommentRequestDto commentRequestDto){ } // 댓글 조회 - public List readComment(CommentRequestDto commentRequestDto){ + public List readComment(CommentRequestDto commentRequestDto, String token){ try { Long journeyId = commentRequestDto.getJourneyId(); List commentList = commentRepository.readComment(journeyId); + String memberEmail = JwtTokenUtil.getLoginId(token, secretKey); + Member member = memberService.getMemberByEmail(memberEmail); // 변수 선언 Long commentWriterId; @@ -112,9 +117,9 @@ public List readComment(CommentRequestDto commentRequestDto) // 비밀 댓글일 때 if (comment.isSecret()) { // 댓글 작성자 혹은 게시글 작성자일 때 - log.info(comment.getId() + " " + commentWriterId); - if (commentWriterId.equals(commentRequestDto.getMemberId()) || journeyWriterId.equals( - commentRequestDto.getMemberId())) { + log.info(comment.getMemberId() + " " + commentWriterId); + if (commentWriterId.equals(member.getId()) || journeyWriterId.equals( + member.getId())) { commentResponseDto = entityToDto(comment.getId(), comment.getContent(), comment.isSecret(), comment.getCreated(), parentId, comment.getMemberId()); // 제 3자일 때 @@ -156,10 +161,11 @@ private CommentResponseDto entityToDto(Long id, String content, boolean secret, } // 댓글 삭제 - public void deleteComment(Long id){ + public void deleteComment(Long id, String token){ try { Comment comment = commentRepository.findById(id).orElseThrow(() -> new CustomException(BaseResponseStatus.COMMON_NOT_FOUND, HttpStatus.NOT_FOUND)); + String memberEmail = JwtTokenUtil.getLoginId(token, secretKey); comment.setStatus(Status.DELETED); commentRepository.save(comment);