Skip to content

Commit

Permalink
Merge pull request #137 from softeerbootcamp4th/feat/#136-fake-lottery
Browse files Browse the repository at this point in the history
feat/#136-fake-lottery
  • Loading branch information
wjddn2165 authored Aug 15, 2024
2 parents db03679 + b1fe8b6 commit bb2faea
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public ResponseEntity<AdminRushEventOptionResponseDto> getRushEventOptions(@Path
.status(HttpStatus.OK)
.body(adminService.getRushEventOptions(rushEventId));
}

// 추첨 이벤트 삭제
@DeleteMapping("/event/lottery")
public ResponseEntity<Void> deleteLotteryEvent() {
Expand Down Expand Up @@ -176,4 +176,12 @@ public ResponseEntity<Void> deleteLotteryEventExpectation(@PathVariable("casperI

return ResponseEntity.noContent().build();
}

// 추첨 이벤트 당첨자 추첨
@PostMapping("/event/lottery/winner")
public ResponseEntity<ResponseDto> pickWinners(){
return ResponseEntity
.status(HttpStatus.CREATED)
.body(adminService.pickWinners());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import JGS.CasperEvent.domain.event.entity.participants.LotteryParticipants;

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

public record LotteryEventParticipantsResponseDto(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import JGS.CasperEvent.domain.event.entity.participants.RushParticipants;

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

public record RushEventParticipantResponseDto(Long id, String phoneNumber,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package JGS.CasperEvent.domain.event.entity.participants;

import JGS.CasperEvent.global.entity.BaseEntity;
import JGS.CasperEvent.global.entity.BaseUser;
import com.fasterxml.jackson.annotation.JsonBackReference;
import jakarta.persistence.*;
import lombok.Getter;

@Getter
@Entity
public class LotteryParticipants {
public class LotteryParticipants extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
Expand All @@ -33,7 +34,7 @@ public LotteryParticipants() {
}

public void expectationAdded() {
if(expectations == 0) expectations++;
if (expectations == 0) expectations++;
appliedCount = Math.min(10, 1 + expectations + linkClickedCount);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package JGS.CasperEvent.domain.event.entity.participants;

import jakarta.persistence.Entity;
import jakarta.persistence.Id;

import java.time.LocalDateTime;

@Entity
public class LotteryWinners {
@Id
private long id;
private String phoneNumber;
private int linkClickedCount;
private int expectation;
private int appliedCount;
private LocalDateTime createdAt;
private LocalDateTime updatedAt;

public LotteryWinners(LotteryParticipants lotteryParticipants) {
this.id = lotteryParticipants.getId();
this.phoneNumber = lotteryParticipants.getBaseUser().getId();
this.linkClickedCount = lotteryParticipants.getLinkClickedCount();
this.expectation = lotteryParticipants.getExpectations();
this.appliedCount = lotteryParticipants.getAppliedCount();
this.createdAt = lotteryParticipants.getCreatedAt();
this.updatedAt = lotteryParticipants.getUpdatedAt();
}

public LotteryWinners() {

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

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

@Repository
public interface LotteryWinnerRepository extends JpaRepository<LotteryWinners, Long> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
import JGS.CasperEvent.domain.event.entity.event.RushEvent;
import JGS.CasperEvent.domain.event.entity.event.RushOption;
import JGS.CasperEvent.domain.event.entity.participants.LotteryParticipants;
import JGS.CasperEvent.domain.event.entity.participants.LotteryWinners;
import JGS.CasperEvent.domain.event.entity.participants.RushParticipants;
import JGS.CasperEvent.domain.event.repository.AdminRepository;
import JGS.CasperEvent.domain.event.repository.CasperBotRepository;
import JGS.CasperEvent.domain.event.repository.eventRepository.LotteryEventRepository;
import JGS.CasperEvent.domain.event.repository.eventRepository.RushEventRepository;
import JGS.CasperEvent.domain.event.repository.eventRepository.RushOptionRepository;
import JGS.CasperEvent.domain.event.repository.participantsRepository.LotteryParticipantsRepository;
import JGS.CasperEvent.domain.event.repository.participantsRepository.LotteryWinnerRepository;
import JGS.CasperEvent.domain.event.repository.participantsRepository.RushParticipantsRepository;
import JGS.CasperEvent.global.enums.CustomErrorCode;
import JGS.CasperEvent.global.enums.Position;
Expand Down Expand Up @@ -58,6 +60,7 @@ public class AdminService {
private final RushOptionRepository rushOptionRepository;
private final S3Service s3Service;
private final CasperBotRepository casperBotRepository;
private final LotteryWinnerRepository lotteryWinnerRepository;

public Admin verifyAdmin(AdminRequestDto adminRequestDto) {
return adminRepository.findByIdAndPassword(adminRequestDto.getAdminId(), adminRequestDto.getPassword()).orElseThrow(NoSuchElementException::new);
Expand Down Expand Up @@ -316,6 +319,24 @@ private LotteryEvent getCurrentLotteryEvent() {
return lotteryEventList.get(0);
}

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

int winnerCount = lotteryEvent.getWinnerCount();

Pageable pageable = PageRequest.of(0, winnerCount);

//todo 당첨자 추첨 알고리즘 변경해야함
Page<LotteryParticipants> lotteryWinners = lotteryParticipantsRepository.findAll(pageable);
for (LotteryParticipants lotteryWinner : lotteryWinners) {
lotteryWinnerRepository.save(new LotteryWinners(lotteryWinner));
}

return new ResponseDto("추첨이 완료되었습니다.");
}

@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 @@ -29,7 +29,8 @@ public enum CustomErrorCode {
EVENT_BEFORE_START_TIME("이벤트 시작 시간은 현재 시간 이후로 설정해야 합니다.", 400),
EVENT_END_TIME_BEFORE_START_TIME("종료 시간은 시작 시간 이후로 설정해야 합니다.", 400),
INVALID_RUSH_EVENT_OPTION("밸런스 게임 선택지가 유효하지 않습니다."),
EVENT_IN_PROGRESS_CANNOT_DELETE("진행중인 이벤트를 삭제할 수 없습니다.", 409);
EVENT_IN_PROGRESS_CANNOT_DELETE("진행중인 이벤트를 삭제할 수 없습니다.", 409),
LOTTERY_EVENT_ALREADY_DRAWN("추첨 이벤트의 당첨자가 이미 추첨되었습니다.", 409),;



Expand Down

0 comments on commit bb2faea

Please sign in to comment.