Skip to content

Commit

Permalink
[BBB-147] Refactor : 인덱스를 타도록 쿼리 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
msjang4 committed Oct 10, 2024
1 parent d226e53 commit ab5bff6
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ public void feedback(Long userId,

}

if (!userStudyRepository.existsByUserIdAndStudyId(userId,
algorithmStudy.getId())) {
if (!userStudyRepository.existsByStudyIdAndUserId(algorithmStudy.getId(),
userId)) {
throw new ForbiddenException(ErrorCode.ONLY_MEMBER_ALLOWED);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ public AssignmentVoteResult voteAssignment(Long userId, Long studyId,
final User user = userRepository.findById(userId)
.orElseThrow(() -> new NotFoundException(ErrorCode.USER_NOT_FOUND));

if (!userStudyRepository.existsByUserIdAndStudyId(userId, studyId)) {
if (!userStudyRepository.existsByStudyIdAndUserId(studyId, userId)) {
throw new ForbiddenException(ErrorCode.ONLY_MEMBER_ALLOWED);
}

Expand Down Expand Up @@ -482,8 +482,8 @@ public AssignmentVoteResult voteAssignment(Long userId, Long studyId,
second = null;
}

AssignmentVote vote = assignmentVoteRepository.findByUserIdAndRound(userId,
nextRound)
AssignmentVote vote = assignmentVoteRepository.findByRoundAndUserId(nextRound,
userId)
.orElseGet(
() -> AssignmentVote.builder()
.user(user)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public Page<StudyResult> readStudy(Pageable pageable) {

@Transactional
public void joinStudy(Long userId, JoinStudyCommand joinStudyCommand) {
if (userStudyRepository.existsByUserIdAndStudyId(userId, joinStudyCommand.studyId())) {
if (userStudyRepository.existsByStudyIdAndUserId(joinStudyCommand.studyId(), userId)) {
throw new BusinessRuleException(ErrorCode.ALREADY_JOINED);
}
User user = userRepository.findById(userId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ void user_cannot_join_study_twice() {
.build();
JoinStudyCommand joinStudyCommand = JoinStudyCommand.builder().studyId(study.getId())
.build();
when(userStudyRepository.existsByUserIdAndStudyId(testuser.getId(), study.getId()))
when(userStudyRepository.existsByStudyIdAndUserId(study.getId(), testuser.getId()))
.thenReturn(true);

/*
Expand Down Expand Up @@ -692,8 +692,8 @@ void apply_feedback_fail_if_user_is_not_a_member() {
testuser.getId(), algorithmProblem.getId()
)).thenReturn(Optional.of(history));

when(userStudyRepository.existsByUserIdAndStudyId(
testuser.getId(), study.getId()
when(userStudyRepository.existsByStudyIdAndUserId(
study.getId(), testuser.getId()
)).thenReturn(false);
/*
* When & Then
Expand Down Expand Up @@ -755,8 +755,8 @@ void apply_feedback_fail_if_user_not_found() {
round.getId(),
algorithmProblem.getId())).thenReturn(true);

when(userStudyRepository.existsByUserIdAndStudyId(
testuser.getId(), study.getId()
when(userStudyRepository.existsByStudyIdAndUserId(
study.getId(), testuser.getId()
)).thenReturn(true);

when(userRepository.findById(
Expand Down Expand Up @@ -2321,7 +2321,7 @@ void vote_assignment_fail_if_not_member() {
.thenReturn(Optional.of(mock(User.class)));
when(studyRepository.findById(studyId))
.thenReturn(Optional.of(mock(Study.class)));
when(userStudyRepository.existsByUserIdAndStudyId(userId, studyId))
when(userStudyRepository.existsByStudyIdAndUserId(studyId, userId))
.thenReturn(false);


Expand Down Expand Up @@ -2350,7 +2350,7 @@ void vote_assignment_fail_if_no_more_round() {
.thenReturn(Optional.of(mock(User.class)));
when(studyRepository.findById(studyId))
.thenReturn(Optional.of(mock(Study.class)));
when(userStudyRepository.existsByUserIdAndStudyId(userId, studyId))
when(userStudyRepository.existsByStudyIdAndUserId(studyId, userId))
.thenReturn(true);

when(roundRepository.findTop1RoundByStudyIdAndStartDateAfterOrderByIdx(eq(studyId), any(
Expand Down Expand Up @@ -2384,7 +2384,7 @@ void vote_assignment_fail_if_first_assignment_not_found() {
.thenReturn(Optional.of(mock(User.class)));
when(studyRepository.findById(studyId))
.thenReturn(Optional.of(mock(Study.class)));
when(userStudyRepository.existsByUserIdAndStudyId(userId, studyId))
when(userStudyRepository.existsByStudyIdAndUserId(studyId, userId))
.thenReturn(true);

when(roundRepository.findTop1RoundByStudyIdAndStartDateAfterOrderByIdx(eq(studyId), any(
Expand Down Expand Up @@ -2419,7 +2419,7 @@ void vote_assignment_fail_if_first_assignment_not_next_round() {
.thenReturn(Optional.of(mock(User.class)));
when(studyRepository.findById(studyId))
.thenReturn(Optional.of(mock(Study.class)));
when(userStudyRepository.existsByUserIdAndStudyId(userId, studyId))
when(userStudyRepository.existsByStudyIdAndUserId(studyId, userId))
.thenReturn(true);

when(roundRepository.findTop1RoundByStudyIdAndStartDateAfterOrderByIdx(eq(studyId), any(
Expand Down Expand Up @@ -2461,7 +2461,7 @@ void vote_assignment_fail_if_second_assignment_not_found() {
.thenReturn(Optional.of(mock(User.class)));
when(studyRepository.findById(studyId))
.thenReturn(Optional.of(mock(Study.class)));
when(userStudyRepository.existsByUserIdAndStudyId(userId, studyId))
when(userStudyRepository.existsByStudyIdAndUserId(studyId, userId))
.thenReturn(true);

when(roundRepository.findTop1RoundByStudyIdAndStartDateAfterOrderByIdx(eq(studyId), any(
Expand Down Expand Up @@ -2506,7 +2506,7 @@ void vote_assignment_fail_if_second_assignment_not_next_round() {
.thenReturn(Optional.of(mock(User.class)));
when(studyRepository.findById(studyId))
.thenReturn(Optional.of(mock(Study.class)));
when(userStudyRepository.existsByUserIdAndStudyId(userId, studyId))
when(userStudyRepository.existsByStudyIdAndUserId(studyId, userId))
.thenReturn(true);

when(roundRepository.findTop1RoundByStudyIdAndStartDateAfterOrderByIdx(eq(studyId), any(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
public interface AssignmentVoteRepository extends JpaRepository<AssignmentVote, Long> {


@Query("SELECT av FROM AssignmentVote av WHERE av.user.id = :userId AND round = :round")
Optional<AssignmentVote> findByUserIdAndRound(Long userId, Round round);
@Query("SELECT av FROM AssignmentVote av WHERE round = :round AND av.user.id = :userId ")
Optional<AssignmentVote> findByRoundAndUserId(Round round, Long userId);

@Query("SELECT av FROM AssignmentVote av WHERE round = :round")
List<AssignmentVote> findAllByRound(Round round);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

public interface UserStudyRepository extends JpaRepository<UserStudy, Long> {

boolean existsByUserIdAndStudyId(Long userId, Long studyId);
boolean existsByStudyIdAndUserId(Long studyId, Long userId);

@Query("SELECT us FROM UserStudy us JOIN FETCH us.user "
+ "WHERE us.study.id = :studyId")
Expand Down

0 comments on commit ab5bff6

Please sign in to comment.