From 4bd64dc657a0eb9663063fc0bb98d42fb0043c51 Mon Sep 17 00:00:00 2001 From: MyunghyunNero Date: Sun, 26 Nov 2023 14:53:04 +0900 Subject: [PATCH] =?UTF-8?q?feat=20:=20=EC=A2=8B=EC=95=84=EC=9A=94=20?= =?UTF-8?q?=EB=88=84=EB=A5=BC=EC=8B=9C=20=EC=A2=8B=EC=95=84=EC=9A=94=20?= =?UTF-8?q?=EC=88=98=20=EB=8F=99=EC=8B=9C=EC=84=B1=20=EB=AC=B8=EC=A0=9C=20?= =?UTF-8?q?=ED=95=B4=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../repository/program/ProgramRespository.java | 14 +++++++------- .../service/favorite/FavoriteServiceImpl.java | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main/java/kusitms/gallae/repository/program/ProgramRespository.java b/src/main/java/kusitms/gallae/repository/program/ProgramRespository.java index f586c88..e9c8a8f 100644 --- a/src/main/java/kusitms/gallae/repository/program/ProgramRespository.java +++ b/src/main/java/kusitms/gallae/repository/program/ProgramRespository.java @@ -1,9 +1,13 @@ package kusitms.gallae.repository.program; +import jakarta.persistence.LockModeType; import kusitms.gallae.domain.Program; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Lock; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; import java.util.List; import java.util.Optional; @@ -12,13 +16,9 @@ public interface ProgramRespository extends JpaRepository { @Override Optional findById(Long programId); - Page findAllByProgramTypeOrderByCreatedAtDesc(String programType , Pageable pageable); - - Page findProgramByProgramNameContaining(String programName, Pageable pageable); - - List findTop4ByOrderByCreatedAtDesc(); - - List findTop4ByOrderByProgramLikeDesc(); + @Lock(LockModeType.PESSIMISTIC_WRITE) + @Query("select p from Program p where p.id = :programId") + Optional findWithIdForUpdate(@Param("programId") Long programId); Program findByUserIdAndStatus(Long id, Program.ProgramStatus programStatus); diff --git a/src/main/java/kusitms/gallae/service/favorite/FavoriteServiceImpl.java b/src/main/java/kusitms/gallae/service/favorite/FavoriteServiceImpl.java index 8a1efd0..9cad426 100644 --- a/src/main/java/kusitms/gallae/service/favorite/FavoriteServiceImpl.java +++ b/src/main/java/kusitms/gallae/service/favorite/FavoriteServiceImpl.java @@ -57,7 +57,7 @@ public class FavoriteServiceImpl implements FavoriteService { @Override public void postFavorite(String username, Long programId) { User user = userRepository.findById(Long.valueOf(username)).get(); - Program program = programRespository.findById(programId).get(); + Program program = programRespository.findWithIdForUpdate(programId).get(); Favorite favorite = favoriteRepository.findByUserAndProgram(user,program).orElse(null); if(favorite == null ) { program.setProgramLike(program.getProgramLike() + 1);