Skip to content

Commit

Permalink
Merge pull request #140 from softeerbootcamp4th/feat/#138-get-winners
Browse files Browse the repository at this point in the history
feat/#138-get-winners
  • Loading branch information
wjddn2165 authored Aug 15, 2024
2 parents bb2faea + 5e522a8 commit 693e0ef
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import JGS.CasperEvent.domain.event.dto.ResponseDto.lotteryEventResponseDto.LotteryEventParticipantsListResponseDto;
import JGS.CasperEvent.domain.event.dto.ResponseDto.rushEventResponseDto.AdminRushEventOptionResponseDto;
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;
Expand All @@ -28,7 +29,6 @@
@RequiredArgsConstructor
public class AdminController {
private final AdminService adminService;
private final LotteryEventRepository lotteryEventRepository;

// 어드민 생성
@PostMapping("/join")
Expand Down Expand Up @@ -179,9 +179,20 @@ public ResponseEntity<Void> deleteLotteryEventExpectation(@PathVariable("casperI

// 추첨 이벤트 당첨자 추첨
@PostMapping("/event/lottery/winner")
public ResponseEntity<ResponseDto> pickWinners(){
public ResponseEntity<ResponseDto> pickLotteryEventWinners() {
return ResponseEntity
.status(HttpStatus.CREATED)
.body(adminService.pickWinners());
.body(adminService.pickLotteryEventWinners());
}

// 추첨 이벤트 당첨자 조회
@GetMapping("/event/lottery/winner")
public ResponseEntity<LotteryEventWinnerListResponseDto> getWinners(
@RequestParam(name = "size", required = false, defaultValue = "10") int size,
@RequestParam(name = "page", required = false, defaultValue = "0") int page,
@RequestParam(name = "number", required = false, defaultValue = "") String phoneNumber) {
return ResponseEntity
.status(HttpStatus.OK)
.body(adminService.getLotteryEventWinners(size, page, phoneNumber));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package JGS.CasperEvent.domain.event.dto.ResponseDto.rushEventResponseDto;

import java.util.List;

public record LotteryEventWinnerListResponseDto(List<LotteryEventWinnerResponseDto> participantsList,
Boolean isLastPage, long totalParticipants) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package JGS.CasperEvent.domain.event.dto.ResponseDto.rushEventResponseDto;

import JGS.CasperEvent.domain.event.entity.participants.LotteryWinners;

import java.time.LocalDate;
import java.time.LocalTime;

public record LotteryEventWinnerResponseDto(
Long id, String phoneNumber, int linkClickedCounts,
int expectation, int appliedCount, long ranking,
LocalDate createdDate, LocalTime createdTime) {

public static LotteryEventWinnerResponseDto of(LotteryWinners lotteryWinner) {
return new LotteryEventWinnerResponseDto(
lotteryWinner.getId(),
lotteryWinner.getPhoneNumber(),
lotteryWinner.getLinkClickedCount(),
lotteryWinner.getExpectation(),
lotteryWinner.getAppliedCount(),
lotteryWinner.getRanking(),
lotteryWinner.getCreatedAt().toLocalDate(),
lotteryWinner.getCreatedAt().toLocalTime()
);
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
package JGS.CasperEvent.domain.event.entity.participants;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.Getter;

import java.time.LocalDateTime;

@Entity
@Getter
public class LotteryWinners {
@Id
private long id;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long ranking;

private String phoneNumber;
private int linkClickedCount;
private int expectation;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package JGS.CasperEvent.domain.event.repository.participantsRepository;

import JGS.CasperEvent.domain.event.entity.participants.LotteryWinners;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface LotteryWinnerRepository extends JpaRepository<LotteryWinners, Long> {
Page<LotteryWinners> findByPhoneNumber(String phoneNumber, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
import JGS.CasperEvent.domain.event.dto.RequestDto.rushEventDto.RushEventRequestDto;
import JGS.CasperEvent.domain.event.dto.ResponseDto.ImageUrlResponseDto;
import JGS.CasperEvent.domain.event.dto.ResponseDto.lotteryEventResponseDto.*;
import JGS.CasperEvent.domain.event.dto.ResponseDto.rushEventResponseDto.AdminRushEventOptionResponseDto;
import JGS.CasperEvent.domain.event.dto.ResponseDto.rushEventResponseDto.AdminRushEventResponseDto;
import JGS.CasperEvent.domain.event.dto.ResponseDto.rushEventResponseDto.RushEventParticipantResponseDto;
import JGS.CasperEvent.domain.event.dto.ResponseDto.rushEventResponseDto.RushEventParticipantsListResponseDto;
import JGS.CasperEvent.domain.event.dto.ResponseDto.rushEventResponseDto.*;
import JGS.CasperEvent.domain.event.entity.admin.Admin;
import JGS.CasperEvent.domain.event.entity.casperBot.CasperBot;
import JGS.CasperEvent.domain.event.entity.event.LotteryEvent;
Expand Down Expand Up @@ -320,7 +317,7 @@ private LotteryEvent getCurrentLotteryEvent() {
}

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

Expand All @@ -337,6 +334,25 @@ public ResponseDto pickWinners() {
return new ResponseDto("추첨이 완료되었습니다.");
}

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);

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

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

for (LotteryWinners lotteryWinners : lotteryWinnersPage) {
lotteryEventWinnerResponseDto.add(
LotteryEventWinnerResponseDto.of(lotteryWinners)
);
}
Boolean isLastPage = !lotteryWinnersPage.hasNext();
return new LotteryEventWinnerListResponseDto(lotteryEventWinnerResponseDto, isLastPage, lotteryWinnerRepository.count());
}

@Transactional
public List<AdminRushEventResponseDto> updateRushEvents(List<RushEventRequestDto> rushEventRequestDtoList) {
LocalDateTime now = LocalDateTime.now();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ public enum CustomErrorCode {
EVENT_END_TIME_BEFORE_START_TIME("종료 시간은 시작 시간 이후로 설정해야 합니다.", 400),
INVALID_RUSH_EVENT_OPTION("밸런스 게임 선택지가 유효하지 않습니다."),
EVENT_IN_PROGRESS_CANNOT_DELETE("진행중인 이벤트를 삭제할 수 없습니다.", 409),
LOTTERY_EVENT_ALREADY_DRAWN("추첨 이벤트의 당첨자가 이미 추첨되었습니다.", 409),;
LOTTERY_EVENT_ALREADY_DRAWN("추첨 이벤트의 당첨자가 이미 추첨되었습니다.", 409),
LOTTERY_EVENT_NOT_DRAWN("추첨 이벤트가 아직 추첨되지 않았습니다.", 404);




Expand Down

0 comments on commit 693e0ef

Please sign in to comment.