Skip to content

Commit

Permalink
Merge pull request #114 from oduck-team/feature/111
Browse files Browse the repository at this point in the history
별점 수정시 애니 토탈 별점 반영 #111
  • Loading branch information
FaberJoo authored Nov 17, 2023
2 parents 6196fe6 + 884dcf4 commit 11f8e1c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
@Repository
public interface AnimeRepository extends JpaRepository<Anime,Long>, AnimeRepositoryCustom {

@Query("select a from Anime a where a.id = :id")
@Query("select a from Anime a where a.id = :id and a.deletedAt is null")
@Lock(LockModeType.PESSIMISTIC_WRITE)
@QueryHints({@QueryHint(name = "javax.persistence.lock.timeout", value ="3000")})
Optional<Anime> findByIdForUpdate(@Param("id")Long id);

@Query("select a from Anime a where a.id = :id and a.deletedAt = null and a.isReleased = :isReleased")
Optional<Anime> findAnimeByConditions(@Param("id") Long id, @Param("isReleased") boolean isReleased);

Expand Down
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
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void createScore() {
.willReturn(Optional.empty());
given(memberRepository.findById(memberId))
.willReturn(Optional.ofNullable(member));
given(animeRepository.findById(animeId))
given(animeRepository.findByIdForUpdate(animeId))
.willReturn(Optional.ofNullable(anime));

// when
Expand Down Expand Up @@ -173,6 +173,8 @@ void updateScore() {

given(starRatingRepository.findByMemberIdAndAnimeId(memberId, animeId))
.willReturn(Optional.ofNullable(starRating));
given(animeRepository.findByIdForUpdate(animeId))
.willReturn(Optional.ofNullable(anime));

// when
boolean result = starRatingService.updateScore(memberId, animeId, 5);
Expand All @@ -194,6 +196,8 @@ void updateScoreIfNotExist() {

given(starRatingRepository.findByMemberIdAndAnimeId(memberId, animeId))
.willReturn(Optional.ofNullable(starRating));
given(animeRepository.findByIdForUpdate(animeId))
.willReturn(Optional.ofNullable(anime));

// when
boolean result = starRatingService.updateScore(memberId, animeId, score);
Expand Down

0 comments on commit 11f8e1c

Please sign in to comment.