Skip to content

Commit

Permalink
feat : 좋아요 누를시 좋아요 수 동시성 문제 해결
Browse files Browse the repository at this point in the history
  • Loading branch information
MyunghyunNero committed Nov 26, 2023
1 parent 2899f0b commit 4bd64dc
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -12,13 +16,9 @@ public interface ProgramRespository extends JpaRepository<Program, Long> {
@Override
Optional<Program> findById(Long programId);

Page<Program> findAllByProgramTypeOrderByCreatedAtDesc(String programType , Pageable pageable);

Page<Program> findProgramByProgramNameContaining(String programName, Pageable pageable);

List<Program> findTop4ByOrderByCreatedAtDesc();

List<Program> findTop4ByOrderByProgramLikeDesc();
@Lock(LockModeType.PESSIMISTIC_WRITE)
@Query("select p from Program p where p.id = :programId")
Optional<Program> findWithIdForUpdate(@Param("programId") Long programId);

Program findByUserIdAndStatus(Long id, Program.ProgramStatus programStatus);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 4bd64dc

Please sign in to comment.