Skip to content

Commit

Permalink
Merge pull request #98 from Team-B1ND/FIX/#97
Browse files Browse the repository at this point in the history
  • Loading branch information
suw0n authored Aug 20, 2024
2 parents 56af1b8 + 88b1877 commit 5bcd246
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private void savePoints(List<Student> students, PointReason reason, LocalDate is
}

private void saveScores(List<Student> students, PointReason reason) {
pointService.getScoresBy(students).forEach(
pointService.getScoresByStudentsForUpdate(students).forEach(
s -> s.issue(reason)
);
}
Expand All @@ -77,7 +77,7 @@ public Response cancel(Integer id) {
Point point = pointService.getPointBy(id);
PointReason reason = point.getReason();
Student student = point.getStudent();
PointScore score = pointService.getScoreBy(student);
PointScore score = pointService.getScoreByStudentForUpdate(student);
score.cancel(reason);

pointService.delete(point);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,31 @@
import b1nd.dodam.domain.rds.member.entity.Student;
import b1nd.dodam.domain.rds.point.entity.PointScore;
import b1nd.dodam.domain.rds.point.exception.PointScoreNotFoundException;
import jakarta.persistence.LockModeType;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Lock;
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.Optional;

public interface PointScoreRepository extends JpaRepository<PointScore, Integer> {

@Lock(LockModeType.PESSIMISTIC_WRITE)
@Query("SELECT ps FROM PointScore ps WHERE ps.student = :student")
Optional<PointScore> findByStudentWithPessimisticLock(Student student);

Optional<PointScore> findByStudent(Student student);

List<PointScore> findByStudentIn(List<Student> students);
@Lock(LockModeType.PESSIMISTIC_WRITE)
@Query("SELECT ps FROM PointScore ps WHERE ps.student IN :students")
List<PointScore> findByStudentInWithPessimisticLock(List<Student> students);

default List<PointScore> getByStudentIn(List<Student> students) {
List<PointScore> scores = findByStudentIn(students);
default List<PointScore> getByStudentInWithPessimisticLock(List<Student> students) {
List<PointScore> scores = findByStudentInWithPessimisticLock(students);

if(students.size() == scores.size()) {
if (students.size() == scores.size()) {
return scores;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,13 @@ public PointScore getScoreBy(Student student) {
.orElseThrow(PointScoreNotFoundException::new);
}

public List<PointScore> getScoresBy(List<Student> students) {
return scoreRepository.getByStudentIn(students);
public PointScore getScoreByStudentForUpdate(Student student) {
return scoreRepository.findByStudentWithPessimisticLock(student)
.orElseThrow(PointScoreNotFoundException::new);
}

public List<PointScore> getScoresByStudentsForUpdate(List<Student> students) {
return scoreRepository.getByStudentInWithPessimisticLock(students);
}

public List<PointScore> getAllScores() {
Expand Down

0 comments on commit 5bcd246

Please sign in to comment.