Skip to content

Commit

Permalink
KKUMI-118 [FEATURE] #73 좋아요 취소 기능
Browse files Browse the repository at this point in the history
  • Loading branch information
eekrwl committed Oct 3, 2024
1 parent 401ffe9 commit c357d4a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<LikesResponse> 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<LikesResponse> unlike(@Login User user, @RequestBody LikesRequest request) {
LikesResponse response = LikesResponse.of(likeService.unlike(user, request.getPostId()));
return ResponseEntity.ok(response);
}
}
20 changes: 20 additions & 0 deletions src/main/java/com/swmarastro/mykkumiserver/like/LikesService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Likes> 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 보내고 가만히 있으면 되지않나
}

}

0 comments on commit c357d4a

Please sign in to comment.