From c357d4a9ff1cf0b8297628ac7c2954039f8a8683 Mon Sep 17 00:00:00 2001 From: eekrwl Date: Thu, 3 Oct 2024 11:37:58 +0900 Subject: [PATCH] =?UTF-8?q?KKUMI-118=20[FEATURE]=20#73=20=EC=A2=8B?= =?UTF-8?q?=EC=95=84=EC=9A=94=20=EC=B7=A8=EC=86=8C=20=EA=B8=B0=EB=8A=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mykkumiserver/like/LikesController.java | 11 ++++++++-- .../mykkumiserver/like/LikesService.java | 20 +++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/swmarastro/mykkumiserver/like/LikesController.java b/src/main/java/com/swmarastro/mykkumiserver/like/LikesController.java index 90d386d..b7dd76b 100644 --- a/src/main/java/com/swmarastro/mykkumiserver/like/LikesController.java +++ b/src/main/java/com/swmarastro/mykkumiserver/like/LikesController.java @@ -13,16 +13,23 @@ import org.springframework.web.bind.annotation.RestController; @RestController -@RequestMapping("/api/v1") +@RequestMapping("/api/v1/likes") @RequiredArgsConstructor public class LikesController { private final LikesService likeService; @RequiresLogin - @PostMapping("/likes") + @PostMapping("/like") public ResponseEntity like(@Login User user, @RequestBody LikesRequest request) { LikesResponse response = LikesResponse.of(likeService.like(user, request.getPostId())); return ResponseEntity.ok(response); } + + @RequiresLogin + @PostMapping("/unlike") + public ResponseEntity unlike(@Login User user, @RequestBody LikesRequest request) { + LikesResponse response = LikesResponse.of(likeService.unlike(user, request.getPostId())); + return ResponseEntity.ok(response); + } } diff --git a/src/main/java/com/swmarastro/mykkumiserver/like/LikesService.java b/src/main/java/com/swmarastro/mykkumiserver/like/LikesService.java index 95ed1bc..ebe9df9 100644 --- a/src/main/java/com/swmarastro/mykkumiserver/like/LikesService.java +++ b/src/main/java/com/swmarastro/mykkumiserver/like/LikesService.java @@ -39,4 +39,24 @@ public Boolean like(User user, Long postId) { //있는데 이미 눌린상태 -> 이미 좋아요 누른 포스트입니다. throw new CommonException(ErrorCode.ALREADY_EXISTS, "이미 좋아요를 누른 포스트입니다.", "이미 좋아요를 누른 포스트입니다."); } + + public Boolean unlike(User user, Long postId) { + Post post = postService.findById(postId); + + //like가 테이블에 있는지 확인 + Optional optionalLike = likeRepository.findByPostAndUser(post, user); + + //없음 -> 좋아요를 취소할 수 없습니다. + if(optionalLike.isEmpty()) { + //TODO 이게 not_found가 맞는지 뭘쓸지 모르겠다 + throw new CommonException(ErrorCode.NOT_FOUND, "좋아요를 취소할 수 없습니다.", "좋아요를 취소할 수 없습니다."); + } + + //있음 -> 좋아요 취소 + Likes likes = optionalLike.get(); + return likes.unlike(); + + //TODO 좋아요가 이미 취소된 상태 -> 그냥 success 보내고 가만히 있으면 되지않나 + } + }