Skip to content

Commit

Permalink
Merge pull request #164 from softeerbootcamp4th/test/#163-admin-servi…
Browse files Browse the repository at this point in the history
…ce-test

test/#163-admin-service-test
  • Loading branch information
wjddn2165 authored Aug 19, 2024
2 parents acc7bd5 + 901620c commit 2aad049
Show file tree
Hide file tree
Showing 11 changed files with 1,699 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import java.time.LocalDateTime;

public record RushEventOptionResponseDto(long optionId, String mainText,
public record RushEventOptionResponseDto(Long optionId, String mainText,
String subText, String resultMainText,
String resultSubText, String imageUrl,
Position position,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import JGS.CasperEvent.global.entity.BaseUser;
import JGS.CasperEvent.global.enums.Role;
import jakarta.persistence.Entity;
import lombok.Getter;

@Entity
@Getter
public class Admin extends BaseUser {
private String password;

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

import jakarta.persistence.*;
import lombok.EqualsAndHashCode;
import lombok.Getter;

import java.time.LocalDateTime;

@Entity
@Getter
@EqualsAndHashCode
public class LotteryEvent extends BaseEvent {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import JGS.CasperEvent.global.error.exception.CustomException;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import jakarta.persistence.*;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;

Expand All @@ -17,6 +18,7 @@
@Entity
@Getter
@ToString
@EqualsAndHashCode
public class RushEvent extends BaseEvent {
private String prizeImageUrl;
private String prizeDescription;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@
import JGS.CasperEvent.global.enums.Position;
import com.fasterxml.jackson.annotation.JsonBackReference;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.*;

@Entity
@Getter
@NoArgsConstructor
@AllArgsConstructor
@Builder
@EqualsAndHashCode(exclude = {"rushEvent"})
@ToString(exclude = {"rushEvent"})
public class RushOption extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@Entity
@Getter
public class LotteryWinners {
private long id;
private Long id;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
import JGS.CasperEvent.global.enums.Position;
import JGS.CasperEvent.global.enums.Role;
import JGS.CasperEvent.global.error.exception.CustomException;
import JGS.CasperEvent.global.error.exception.TooManyLotteryEventException;
import JGS.CasperEvent.global.error.exception.TooManyRushEventException;
import JGS.CasperEvent.global.response.ResponseDto;
import JGS.CasperEvent.global.service.S3Service;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -62,10 +60,12 @@ public class AdminService {
private final LotteryWinnerRepository lotteryWinnerRepository;
private final RedisTemplate<String, CasperBotResponseDto> casperBotRedisTemplate;

// 어드민 인증
public Admin verifyAdmin(AdminRequestDto adminRequestDto) {
return adminRepository.findByIdAndPassword(adminRequestDto.getAdminId(), adminRequestDto.getPassword()).orElseThrow(NoSuchElementException::new);
}

// 어드민 생성
public ResponseDto postAdmin(AdminRequestDto adminRequestDto) {
String adminId = adminRequestDto.getAdminId();
//Todo: 비밀번호 암호화 필요
Expand All @@ -79,12 +79,14 @@ public ResponseDto postAdmin(AdminRequestDto adminRequestDto) {
return ResponseDto.of("관리자 생성 성공");
}

// 이미지 업로드
public ImageUrlResponseDto postImage(MultipartFile image) {
return new ImageUrlResponseDto(s3Service.upload(image));
}

// 추첨 이벤트 생성
public LotteryEventResponseDto createLotteryEvent(LotteryEventRequestDto lotteryEventRequestDto) {
if (lotteryEventRepository.count() >= 1) throw new TooManyLotteryEventException();
if (lotteryEventRepository.count() >= 1) throw new CustomException(CustomErrorCode.TOO_MANY_LOTTERY_EVENT);

LotteryEvent lotteryEvent = lotteryEventRepository.save(new LotteryEvent(
LocalDateTime.of(lotteryEventRequestDto.getStartDate(), lotteryEventRequestDto.getStartTime()),
Expand All @@ -95,12 +97,14 @@ public LotteryEventResponseDto createLotteryEvent(LotteryEventRequestDto lottery
return LotteryEventResponseDto.of(lotteryEvent, LocalDateTime.now());
}

// 추첨 이벤트 조회
public LotteryEventDetailResponseDto getLotteryEvent() {
return LotteryEventDetailResponseDto.of(
getCurrentLotteryEvent()
);
}

// 추첨 이벤트 참여자 조회
public LotteryEventParticipantsListResponseDto getLotteryEventParticipants(int size, int page, String phoneNumber) {
Pageable pageable = PageRequest.of(page, size);

Expand All @@ -125,14 +129,13 @@ public LotteryEventParticipantsListResponseDto getLotteryEventParticipants(int s
return new LotteryEventParticipantsListResponseDto(lotteryEventParticipantsResponseDtoList, isLastPage, count);
}

// 선착순 이벤트 생성
public AdminRushEventResponseDto createRushEvent(RushEventRequestDto rushEventRequestDto, MultipartFile prizeImg, MultipartFile leftOptionImg, MultipartFile rightOptionImg) {
if (rushEventRepository.count() >= 6) throw new TooManyRushEventException();

if (rushEventRepository.count() >= 6) throw new CustomException(CustomErrorCode.TOO_MANY_RUSH_EVENT);
String prizeImgSrc = s3Service.upload(prizeImg);
String leftOptionImgSrc = s3Service.upload(leftOptionImg);
String rightOptionImgSrc = s3Service.upload(rightOptionImg);

// Img s3 저장
RushEvent rushEvent = rushEventRepository.save(
new RushEvent(
LocalDateTime.of(rushEventRequestDto.getEventDate(), rushEventRequestDto.getStartTime()),
Expand Down Expand Up @@ -166,10 +169,10 @@ public AdminRushEventResponseDto createRushEvent(RushEventRequestDto rushEventRe
));

rushEvent.addOption(leftRushOption, rightRushOption);

return AdminRushEventResponseDto.of(rushEvent);
}

// 선착순 이벤트 조회
public List<AdminRushEventResponseDto> getRushEvents() {
List<RushEvent> rushEvents = rushEventRepository.findAll();
List<AdminRushEventResponseDto> rushEventResponseDtoList = new ArrayList<>();
Expand All @@ -179,6 +182,7 @@ public List<AdminRushEventResponseDto> getRushEvents() {
return rushEventResponseDtoList;
}

// 선착순 이벤트 참여자 조회
public RushEventParticipantsListResponseDto getRushEventParticipants(long rushEventId, int size, int page, int optionId, String phoneNumber) {
Pageable pageable = PageRequest.of(page, size);

Expand Down Expand Up @@ -221,6 +225,7 @@ public RushEventParticipantsListResponseDto getRushEventParticipants(long rushEv
return new RushEventParticipantsListResponseDto(rushEventParticipantResponseDtoList, isLastPage, count);
}

// 선착순 이벤트 당첨자 조회
public RushEventParticipantsListResponseDto getRushEventWinners(long rushEventId, int size, int page, String phoneNumber) {
Page<RushParticipants> rushParticipantsPage = null;

Expand All @@ -233,10 +238,8 @@ public RushEventParticipantsListResponseDto getRushEventWinners(long rushEventId

boolean isPhoneNumberEmpty = phoneNumber.isEmpty();

int winnerOptionId;
if (leftSelect > rightSelect) winnerOptionId = 1;
else if (leftSelect < rightSelect) winnerOptionId = 2;
else winnerOptionId = 0;
int winnerOptionId = (leftSelect > rightSelect) ? 1 : (leftSelect < rightSelect) ? 2 : 0;


if (!isPhoneNumberEmpty && winnerOptionId != 0) {
// 전화번호와 유효한 옵션 ID가 있는 경우
Expand Down Expand Up @@ -270,12 +273,14 @@ public RushEventParticipantsListResponseDto getRushEventWinners(long rushEventId
return new RushEventParticipantsListResponseDto(rushEventParticipantResponseDtoList, isLastPage, totalParticipants);
}

// 선착순 이벤트 삭제
@Transactional
public void deleteLotteryEvent() {
LotteryEvent currentLotteryEvent = getCurrentLotteryEvent();
lotteryEventRepository.deleteById(currentLotteryEvent.getLotteryEventId());
}

// 선착순 이벤트 업데이트
@Transactional
public LotteryEventDetailResponseDto updateLotteryEvent(LotteryEventRequestDto lotteryEventRequestDto) {
LotteryEvent currentLotteryEvent = getCurrentLotteryEvent();
Expand Down Expand Up @@ -310,6 +315,7 @@ else if (newStartDateTime.isBefore(now)) {
return LotteryEventDetailResponseDto.of(currentLotteryEvent);
}

// 추첨 이벤트 조회
private LotteryEvent getCurrentLotteryEvent() {
List<LotteryEvent> lotteryEventList = lotteryEventRepository.findAll();

Expand All @@ -324,6 +330,7 @@ private LotteryEvent getCurrentLotteryEvent() {
return lotteryEventList.get(0);
}

// 추첨 이벤트 당첨자 추첨
@Transactional
public ResponseDto pickLotteryEventWinners() {
if (lotteryWinnerRepository.count() > 1) throw new CustomException(CustomErrorCode.LOTTERY_EVENT_ALREADY_DRAWN);
Expand All @@ -332,6 +339,13 @@ public ResponseDto pickLotteryEventWinners() {
int winnerCount = lotteryEvent.getWinnerCount();

List<LotteryParticipants> lotteryParticipants = lotteryParticipantsRepository.findAll();

if(winnerCount >= lotteryParticipants.size()){
for (LotteryParticipants lotteryParticipant : lotteryParticipants) {
lotteryWinnerRepository.save(new LotteryWinners(lotteryParticipant));
}
return new ResponseDto("추첨이 완료되었습니다.");
}
Set<LotteryParticipants> lotteryEventWinners = new HashSet<>();

int totalWeight;
Expand All @@ -347,7 +361,7 @@ public ResponseDto pickLotteryEventWinners() {
int cumulativeSum = 0;
for (LotteryParticipants lotteryParticipant : lotteryParticipants) {
cumulativeSum += lotteryParticipant.getAppliedCount();
if(randomValue <= cumulativeSum){
if (randomValue <= cumulativeSum) {
lotteryEventWinners.add(lotteryParticipant);
lotteryParticipants.remove(lotteryParticipant);
break;
Expand All @@ -362,11 +376,13 @@ public ResponseDto pickLotteryEventWinners() {
return new ResponseDto("추첨이 완료되었습니다.");
}

public ResponseDto deleteLotteryEventWinners(){
// 당첨자 명단 삭제
public ResponseDto deleteLotteryEventWinners() {
lotteryWinnerRepository.deleteAll();
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);
Expand All @@ -392,6 +408,7 @@ public LotteryEventWinnerListResponseDto getLotteryEventWinners(int size, int pa
return new LotteryEventWinnerListResponseDto(lotteryEventWinnerResponseDto, isLastPage, count);
}

// 선착순 이벤트 업데이트
@Transactional
public List<AdminRushEventResponseDto> updateRushEvents(List<RushEventRequestDto> rushEventRequestDtoList) {
LocalDateTime now = LocalDateTime.now();
Expand Down Expand Up @@ -441,6 +458,7 @@ else if (startDateTime.isBefore(now)) {
return rushEventResponseDtoList;
}

// 선착순 이벤트 삭제
@Transactional
public ResponseDto deleteRushEvent(Long rushEventId) {
RushEvent rushEvent = rushEventRepository.findById(rushEventId).orElseThrow(() -> new CustomException(CustomErrorCode.NO_RUSH_EVENT));
Expand All @@ -455,6 +473,7 @@ public ResponseDto deleteRushEvent(Long rushEventId) {
return ResponseDto.of("요청에 성공하였습니다.");
}

// 선착순 이벤트 선택지 조회
public AdminRushEventOptionResponseDto getRushEventOptions(Long rushEventId) {
return AdminRushEventOptionResponseDto.of(
rushEventRepository.findById(rushEventId).orElseThrow(
Expand All @@ -463,6 +482,7 @@ public AdminRushEventOptionResponseDto getRushEventOptions(Long rushEventId) {
);
}

// 기대평 조회
public LotteryEventExpectationsResponseDto getLotteryEventExpectations(int page, int size, Long participantId) {
LotteryParticipants lotteryParticipant = lotteryParticipantsRepository.findById(participantId).orElseThrow(
() -> new CustomException(CustomErrorCode.USER_NOT_FOUND)
Expand All @@ -489,6 +509,7 @@ public LotteryEventExpectationsResponseDto getLotteryEventExpectations(int page,
return new LotteryEventExpectationsResponseDto(lotteryEventExpectationResponseDtoList, isLastPage, casperBotPage.getTotalElements());
}

// 부적절한 기대평 삭제
@Transactional
public void deleteLotteryEventExpectation(Long casperId) {
CasperBot casperBot = casperBotRepository.findById(casperId).orElseThrow(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import jakarta.persistence.EntityListeners;
import jakarta.persistence.MappedSuperclass;
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
Expand All @@ -19,7 +18,6 @@
@EntityListeners(AuditingEntityListener.class)
@MappedSuperclass
@Getter
@Setter
public class BaseEntity {
@CreatedDate
@JsonSerialize(using = LocalDateTimeSerializer.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public enum CustomErrorCode {
INVALID_RUSH_EVENT_OPTION_ID("옵션 ID는 1 또는 2여야 합니다.", 400),
EMPTY_FILE("유효하지 않은 파일입니다.", 422),
TOO_MANY_LOTTERY_EVENT("현재 진행중인 추첨 이벤트가 2개 이상입니다.", 409),
TOO_MANY_RUSH_EVENT("현재 진행중인 선착순 이벤트가 6개 이상입니다.", 409),
EVENT_IN_PROGRESS_CANNOT_CHANGE_START_TIME("현재 진행 중인 이벤트의 시작 시간을 변경할 수 없습니다.", 400),
EVENT_IN_PROGRESS_END_TIME_BEFORE_NOW("현재 진행 중인 이벤트의 종료 시간을 현재 시간보다 이전으로 설정할 수 없습니다.", 400),
EVENT_BEFORE_START_TIME("이벤트 시작 시간은 현재 시간 이후로 설정해야 합니다.", 400),
Expand Down
Loading

0 comments on commit 2aad049

Please sign in to comment.