From f5aced9e72699daaa9d09c6f88ba4ba8920a06af Mon Sep 17 00:00:00 2001 From: FaberJoo Date: Thu, 16 Nov 2023 20:49:03 +0900 Subject: [PATCH 1/3] =?UTF-8?q?refactor:=20=EC=95=A0=EB=8B=88=EB=A9=94=20?= =?UTF-8?q?=EC=A1=B0=ED=9A=8C=20db=20lock=20=EC=BF=BC=EB=A6=AC=20=EB=A9=94?= =?UTF-8?q?=EC=86=8C=EB=93=9C=20=EC=82=AD=EC=A0=9C=20=EC=95=A0=EB=8B=88=20?= =?UTF-8?q?=EC=A1=B0=EA=B1=B4=20=EC=B6=94=EA=B0=80=20#111?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/oduck/api/domain/anime/repository/AnimeRepository.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/oduck/api/domain/anime/repository/AnimeRepository.java b/src/main/java/io/oduck/api/domain/anime/repository/AnimeRepository.java index bcc7ae75..e7bba382 100644 --- a/src/main/java/io/oduck/api/domain/anime/repository/AnimeRepository.java +++ b/src/main/java/io/oduck/api/domain/anime/repository/AnimeRepository.java @@ -14,11 +14,11 @@ @Repository public interface AnimeRepository extends JpaRepository, 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 findByIdForUpdate(@Param("id")Long id); - + @Query("select a from Anime a where a.id = :id and a.deletedAt = null and a.isReleased = :isReleased") Optional findAnimeByConditions(@Param("id") Long id, @Param("isReleased") boolean isReleased); From a835f3bdcedbce817db5bb676c5dd33fe0f35f75 Mon Sep 17 00:00:00 2001 From: FaberJoo Date: Thu, 16 Nov 2023 20:53:23 +0900 Subject: [PATCH 2/3] =?UTF-8?q?refactor:=20=EB=B3=84=EC=A0=90=20=EC=88=98?= =?UTF-8?q?=EC=A0=95=EC=8B=9C=20=EC=95=A0=EB=8B=88=EB=A9=94=20=ED=86=A0?= =?UTF-8?q?=ED=83=88=20=ED=8F=89=EC=A0=90=20=EC=88=98=EC=A0=95=20=EB=A1=9C?= =?UTF-8?q?=EC=A7=81=20=EC=B6=94=EA=B0=80=20#111?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../starRating/service/StarRatingServiceImpl.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/oduck/api/domain/starRating/service/StarRatingServiceImpl.java b/src/main/java/io/oduck/api/domain/starRating/service/StarRatingServiceImpl.java index 71314ec9..7823f04f 100644 --- a/src/main/java/io/oduck/api/domain/starRating/service/StarRatingServiceImpl.java +++ b/src/main/java/io/oduck/api/domain/starRating/service/StarRatingServiceImpl.java @@ -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); @@ -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; From 47197fcf3668b4a154c231939f9535c85429a8c0 Mon Sep 17 00:00:00 2001 From: FaberJoo Date: Thu, 16 Nov 2023 21:01:10 +0900 Subject: [PATCH 3/3] =?UTF-8?q?test:=20=EB=B3=84=EC=A0=90=20=EC=84=9C?= =?UTF-8?q?=EB=B9=84=EC=8A=A4=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20db=20lock=20?= =?UTF-8?q?=EC=BF=BC=EB=A6=AC=20=EB=A9=94=EC=86=8C=EB=93=9C=20=EB=B0=98?= =?UTF-8?q?=EC=98=81=20#111?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/unit/starRating/service/StarRatingServiceTest.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/test/java/io/oduck/api/unit/starRating/service/StarRatingServiceTest.java b/src/test/java/io/oduck/api/unit/starRating/service/StarRatingServiceTest.java index 2d3e40d7..ac0d412e 100644 --- a/src/test/java/io/oduck/api/unit/starRating/service/StarRatingServiceTest.java +++ b/src/test/java/io/oduck/api/unit/starRating/service/StarRatingServiceTest.java @@ -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 @@ -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); @@ -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);