Skip to content

Commit

Permalink
refactor: 별점 수정시 애니메 토탈 평점 수정 로직 추가 #111
Browse files Browse the repository at this point in the history
  • Loading branch information
FaberJoo committed Nov 16, 2023
1 parent f5aced9 commit a835f3b
Showing 1 changed file with 11 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public boolean createScore(Long memberId, Long animeId, int score) {
Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new NotFoundException("Member"));

Anime anime = animeRepository.findById(animeId)
Anime anime = animeRepository.findByIdForUpdate(animeId)
.orElseThrow(() -> new NotFoundException("Anime"));

anime.increaseStarRatingScore(score);
Expand All @@ -60,15 +60,24 @@ public RatedRes checkRated(Long memberId, Long animeId) {
}

@Override
@Transactional
public boolean updateScore(Long memberId, Long animeId, int score) {
StarRating foundStarRating = findByMemberIdAndAnimeId(memberId, animeId)
.orElseThrow(() -> new NotFoundException("StarRating"));

if (foundStarRating.getScore() == score) {
int prevScore = foundStarRating.getScore();

Anime anime = animeRepository.findByIdForUpdate(animeId)
.orElseThrow(() -> new NotFoundException("Anime"));

if (prevScore == score) {
return false;
}

anime.decreaseStarRatingScore(prevScore);

foundStarRating.updateScore(score);
anime.increaseStarRatingScore(score);

starRatingRepository.save(foundStarRating);
return true;
Expand Down

0 comments on commit a835f3b

Please sign in to comment.