Skip to content

Commit

Permalink
Merge pull request #141 from softeerbootcamp4th/fix/#139-search-fix
Browse files Browse the repository at this point in the history
fix/#139-search-fix
  • Loading branch information
wjddn2165 authored Aug 15, 2024
2 parents 693e0ef + d73da6a commit e592316
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import JGS.CasperEvent.domain.event.dto.ResponseDto.rushEventResponseDto.AdminRushEventResponseDto;
import JGS.CasperEvent.domain.event.dto.ResponseDto.rushEventResponseDto.LotteryEventWinnerListResponseDto;
import JGS.CasperEvent.domain.event.dto.ResponseDto.rushEventResponseDto.RushEventParticipantsListResponseDto;
import JGS.CasperEvent.domain.event.repository.eventRepository.LotteryEventRepository;
import JGS.CasperEvent.domain.event.service.adminService.AdminService;
import JGS.CasperEvent.global.response.ResponseDto;
import jakarta.validation.Valid;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import JGS.CasperEvent.domain.event.entity.participants.LotteryParticipants;
import JGS.CasperEvent.global.entity.BaseUser;
import io.lettuce.core.dynamic.annotation.Param;
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.Query;
import org.springframework.stereotype.Repository;

import java.util.Optional;
Expand All @@ -13,6 +15,10 @@
public interface LotteryParticipantsRepository extends JpaRepository<LotteryParticipants, Long> {
Optional<LotteryParticipants> findByBaseUser(BaseUser baseUser);

Page<LotteryParticipants> findByBaseUser_Id(String id, Pageable pageable);
@Query("SELECT p FROM LotteryParticipants p WHERE p.baseUser.id LIKE :id%")
Page<LotteryParticipants> findByBaseUser_Id(@Param("id") String id, Pageable pageable);

@Query("SELECT COUNT(p) FROM LotteryParticipants p WHERE p.baseUser.id LIKE :id%")
long countByBaseUser_Id(@Param("id") String id);
}

Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package JGS.CasperEvent.domain.event.repository.participantsRepository;

import JGS.CasperEvent.domain.event.entity.participants.LotteryWinners;
import io.lettuce.core.dynamic.annotation.Param;
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.Query;
import org.springframework.stereotype.Repository;

@Repository
public interface LotteryWinnerRepository extends JpaRepository<LotteryWinners, Long> {
Page<LotteryWinners> findByPhoneNumber(String phoneNumber, Pageable pageable);
}
@Query("SELECT w FROM LotteryWinners w WHERE w.phoneNumber LIKE :phoneNumber%")
Page<LotteryWinners> findByPhoneNumber(@Param("phoneNumber") String phoneNumber, Pageable pageable);

@Query("SELECT COUNT(w) FROM LotteryWinners w WHERE w.phoneNumber LIKE :phoneNumber%")
long countByPhoneNumber(@Param("phoneNumber") String phoneNumber);}
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ long findUserRankByEventIdAndUserIdAndOptionId(@Param("eventId") Long eventId,

Page<RushParticipants> findByRushEvent_RushEventId(Long rushEventId, Pageable pageable);

Page<RushParticipants> findByRushEvent_RushEventIdAndBaseUser_Id(Long rushEventId, String baseUser_id, Pageable pageable);
@Query("SELECT p FROM RushParticipants p WHERE p.rushEvent.rushEventId = :rushEventId AND p.baseUser.id LIKE :baseUserId%")
Page<RushParticipants> findByRushEvent_RushEventIdAndBaseUser_Id(@Param("rushEventId") Long rushEventId, @Param("baseUserId") String baseUserId, Pageable pageable);

Page<RushParticipants> findByRushEvent_RushEventIdAndOptionId(Long rushEventId, int optionId, Pageable pageable);

Page<RushParticipants> findByRushEvent_RushEventIdAndOptionIdAndBaseUser_Id(Long rushEventId, int optionId, String baseUser_id, Pageable pageable);
@Query("SELECT p FROM RushParticipants p WHERE p.rushEvent.rushEventId = :rushEventId AND p.optionId = :optionId AND p.baseUser.id LIKE :baseUserId%")
Page<RushParticipants> findByRushEvent_RushEventIdAndOptionIdAndBaseUser_Id(@Param("rushEventId") Long rushEventId, @Param("optionId") int optionId, @Param("baseUserId") String baseUserId, Pageable pageable);


@Query("SELECT rp FROM RushParticipants rp " +
Expand All @@ -50,7 +52,7 @@ Page<RushParticipants> findWinnerByEventIdAndOptionId(
@Query("SELECT rp FROM RushParticipants rp " +
"WHERE rp.rushEvent.rushEventId = :eventId " +
"AND rp.optionId = :optionId " +
"AND rp.baseUser.id = :phoneNumber " +
"AND rp.baseUser.id LIKE :phoneNumber% " +
"ORDER BY rp.id ASC ")
Page<RushParticipants> findWinnerByEventIdAndOptionIdAndPhoneNumber(
@Param("eventId") Long eventId, @Param("optionId") int optionId, @Param("phoneNumber") String phoneNumber, Pageable pageable
Expand All @@ -64,14 +66,15 @@ Page<RushParticipants> findWinnerByEventIdAndOptionIdAndPhoneNumber(

@Query("SELECT rp FROM RushParticipants rp " +
"WHERE rp.rushEvent.rushEventId = :eventId " +
"AND rp.baseUser.id = :phoneNumber " +
"AND rp.baseUser.id LIKE :phoneNumber% " +
"ORDER BY rp.id ASC ")
Page<RushParticipants> findByWinnerByEventIdAndPhoneNumber(@Param("eventId") Long eventId, @Param("phoneNumber") String phoneNumber, Pageable pageable);

long countByRushEvent_RushEventIdAndOptionIdAndBaseUser_Id(long rushEventId, int optionId, String id);
@Query("SELECT COUNT(p) FROM RushParticipants p WHERE p.rushEvent.rushEventId = :rushEventId AND p.optionId = :optionId AND p.baseUser.id LIKE :baseUserId%")
long countByRushEvent_RushEventIdAndOptionIdAndBaseUser_Id(@Param("rushEventId") Long rushEventId, @Param("optionId") int optionId, @Param("baseUserId") String baseUserId);

long countByRushEvent_RushEventId(long rushEventId);

long countByRushEvent_RushEventIdAndBaseUser_Id(long rushEventId, String phoneNumber);

}
@Query("SELECT COUNT(p) FROM RushParticipants p WHERE p.rushEvent.rushEventId = :rushEventId AND p.baseUser.id LIKE :baseUserId%")
long countByRushEvent_RushEventIdAndBaseUser_Id(@Param("rushEventId") Long rushEventId, @Param("baseUserId") String baseUserId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,14 @@ public LotteryEventParticipantsListResponseDto getLotteryEventParticipants(int s
Pageable pageable = PageRequest.of(page, size);

Page<LotteryParticipants> lotteryParticipantsPage = null;
if (phoneNumber.isEmpty()) lotteryParticipantsPage = lotteryParticipantsRepository.findAll(pageable);
else lotteryParticipantsPage = lotteryParticipantsRepository.findByBaseUser_Id(phoneNumber, pageable);
long count;
if (phoneNumber.isEmpty()) {
lotteryParticipantsPage = lotteryParticipantsRepository.findAll(pageable);
count = lotteryParticipantsRepository.count();
} else {
lotteryParticipantsPage = lotteryParticipantsRepository.findByBaseUser_Id(phoneNumber, pageable);
count = lotteryParticipantsRepository.countByBaseUser_Id(phoneNumber);
}

List<LotteryEventParticipantsResponseDto> lotteryEventParticipantsResponseDtoList = new ArrayList<>();

Expand All @@ -113,7 +119,7 @@ public LotteryEventParticipantsListResponseDto getLotteryEventParticipants(int s
);
}
Boolean isLastPage = !lotteryParticipantsPage.hasNext();
return new LotteryEventParticipantsListResponseDto(lotteryEventParticipantsResponseDtoList, isLastPage, lotteryParticipantsRepository.count());
return new LotteryEventParticipantsListResponseDto(lotteryEventParticipantsResponseDtoList, isLastPage, count);
}

public AdminRushEventResponseDto createRushEvent(RushEventRequestDto rushEventRequestDto, MultipartFile prizeImg, MultipartFile leftOptionImg, MultipartFile rightOptionImg) {
Expand Down Expand Up @@ -209,7 +215,6 @@ public RushEventParticipantsListResponseDto getRushEventParticipants(long rushEv
}

Boolean isLastPage = !rushParticipantsPage.hasNext();
// todo 전체 참여자 아닌 옵션별 참여자로 수정하기
return new RushEventParticipantsListResponseDto(rushEventParticipantResponseDtoList, isLastPage, count);
}

Expand Down Expand Up @@ -318,7 +323,7 @@ private LotteryEvent getCurrentLotteryEvent() {

@Transactional
public ResponseDto pickLotteryEventWinners() {
if(lotteryWinnerRepository.count() > 1) throw new CustomException(CustomErrorCode.LOTTERY_EVENT_ALREADY_DRAWN);
if (lotteryWinnerRepository.count() > 1) throw new CustomException(CustomErrorCode.LOTTERY_EVENT_ALREADY_DRAWN);
LotteryEvent lotteryEvent = getCurrentLotteryEvent();

int winnerCount = lotteryEvent.getWinnerCount();
Expand All @@ -334,13 +339,19 @@ public ResponseDto pickLotteryEventWinners() {
return new ResponseDto("추첨이 완료되었습니다.");
}

public LotteryEventWinnerListResponseDto getLotteryEventWinners(int size, int page, String phoneNumber){
public LotteryEventWinnerListResponseDto getLotteryEventWinners(int size, int page, String phoneNumber) {
Pageable pageable = PageRequest.of(page, size);
if(lotteryWinnerRepository.count() == 0) throw new CustomException(CustomErrorCode.LOTTERY_EVENT_NOT_DRAWN);
if (lotteryWinnerRepository.count() == 0) throw new CustomException(CustomErrorCode.LOTTERY_EVENT_NOT_DRAWN);

Page<LotteryWinners> lotteryWinnersPage = null;
if (phoneNumber.isEmpty()) lotteryWinnersPage = lotteryWinnerRepository.findAll(pageable);
else lotteryWinnersPage = lotteryWinnerRepository.findByPhoneNumber(phoneNumber, pageable);
long count;
if (phoneNumber.isEmpty()) {
lotteryWinnersPage = lotteryWinnerRepository.findAll(pageable);
count = lotteryWinnerRepository.count();
} else {
lotteryWinnersPage = lotteryWinnerRepository.findByPhoneNumber(phoneNumber, pageable);
count = lotteryWinnerRepository.countByPhoneNumber(phoneNumber);
}

List<LotteryEventWinnerResponseDto> lotteryEventWinnerResponseDto = new ArrayList<>();

Expand All @@ -350,7 +361,7 @@ public LotteryEventWinnerListResponseDto getLotteryEventWinners(int size, int pa
);
}
Boolean isLastPage = !lotteryWinnersPage.hasNext();
return new LotteryEventWinnerListResponseDto(lotteryEventWinnerResponseDto, isLastPage, lotteryWinnerRepository.count());
return new LotteryEventWinnerListResponseDto(lotteryEventWinnerResponseDto, isLastPage, count);
}

@Transactional
Expand Down

0 comments on commit e592316

Please sign in to comment.