-
Notifications
You must be signed in to change notification settings - Fork 0
[#31] 회차별 좋아요 등록/해제 #34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: feature/30
Are you sure you want to change the base?
Conversation
|
f-lab-saponin
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
회차별 좋아요 확인했습니다. 고생하셨습니다! 리뷰한번 확인부탁드립니다!!
| import lombok.Getter; | ||
|
|
||
| @Getter | ||
| public class EpisodeException extends RuntimeException { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
EpisodeException 이게 사용중인 곳이 없어서 사용하는지 한번 확인부탁드려요!
| Integer newCount = episodeRepository.findById(episodeId) | ||
| .map(ep -> ep.getLikeCount() == null ? 0 : ep.getLikeCount()) | ||
| .orElse(0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
여기도 굳이 다시 쿼리하지 않아도 업뎅트 성공했다면 likeCount + delta 로 반환해도될것 같아요.
| @PostMapping("/create") | ||
| public ResponseEntity<EpisodeLikeResponse> like( | ||
| @AuthenticationPrincipal CustomUserPrincipal principal, | ||
| @Valid @RequestBody EpisodeLikeRequest req | ||
| ) { | ||
| EpisodeLikeResponse episodeLikeResponse | ||
| = episodeLikeService.like(principal.id(), req.episodeId()); | ||
|
|
||
| return ResponseEntity.ok(episodeLikeResponse); | ||
| } | ||
|
|
||
| @DeleteMapping("/delete/{episodeId}") | ||
| public ResponseEntity<EpisodeLikeResponse> unlike( | ||
| @AuthenticationPrincipal CustomUserPrincipal principal, | ||
| @PathVariable Long episodeId |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| throw new AlreadyEpisodeLikeException(); | ||
| } | ||
|
|
||
| episodeLikeRepository.save(EpisodeLike.like(member, episode)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
member_id, episode_id 유니크 조건이 있으면 중복 인서트를 방지할 수 있을 것 같아요. 혹은 다른 좋은 방법이 있으면 적용해보면 좋아요.
| @Modifying(clearAutomatically = true, flushAutomatically = true) | ||
| @Query(""" | ||
| UPDATE Episode e | ||
| SET e.likeCount = | ||
| CASE | ||
| WHEN :delta < 0 AND COALESCE(e.likeCount, 0) <= 0 | ||
| THEN 0 | ||
| ELSE COALESCE(e.likeCount, 0) + :delta | ||
| END | ||
| WHERE e.id = :episodeId | ||
| """) | ||
| void updateLikeCountSafely(@Param("episodeId") Long episodeId, | ||
| @Param("delta") int delta); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
deleted_at IS NULL
조건은 필요없을지 고민이 필요해보여요!



작업 내용
테스트 내용