Skip to content

Commit

Permalink
Merge pull request #123 from softeerbootcamp4th/feature/#115-admin-lo…
Browse files Browse the repository at this point in the history
…ttery-event-get-expectation

Feature/#115 admin lottery event get expectation
  • Loading branch information
k000927 authored Aug 13, 2024
2 parents 1cd4e9c + bbee6d4 commit bcea85f
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
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.LotteryEventDetailResponseDto;
import JGS.CasperEvent.domain.event.dto.ResponseDto.lotteryEventResponseDto.LotteryEventExpectationResponseDto;
import JGS.CasperEvent.domain.event.dto.ResponseDto.lotteryEventResponseDto.LotteryEventResponseDto;
import JGS.CasperEvent.domain.event.dto.ResponseDto.lotteryEventResponseDto.LotteryEventParticipantsListResponseDto;
import JGS.CasperEvent.domain.event.dto.ResponseDto.rushEventResponseDto.AdminRushEventResponseDto;
import JGS.CasperEvent.domain.event.dto.ResponseDto.rushEventResponseDto.RushEventParticipantsListResponseDto;
import JGS.CasperEvent.domain.event.dto.ResponseDto.rushEventResponseDto.RushEventResponseDto;
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 All @@ -27,7 +26,6 @@
@RequiredArgsConstructor
public class AdminController {
private final AdminService adminService;
private final LotteryEventRepository lotteryEventRepository;

// 어드민 생성
@PostMapping("/join")
Expand Down Expand Up @@ -132,4 +130,12 @@ public ResponseEntity<LotteryEventDetailResponseDto> updateLotteryEvent(@Request

return ResponseEntity.ok(updatedLotteryEventDetailResponseDto);
}

// 추첨 이벤트 특정 사용자의 기대평 조회
@GetMapping("/event/lottery/participants/{participantId}/expectations")
public ResponseEntity<List<LotteryEventExpectationResponseDto>> getLotteryEventExpectations(@PathVariable("participantId") Long participantId) {
List<LotteryEventExpectationResponseDto> lotteryEventExpectationResponseDtoList = adminService.getLotteryEventExpectations(participantId);

return ResponseEntity.ok(lotteryEventExpectationResponseDtoList);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package JGS.CasperEvent.domain.event.dto.ResponseDto.lotteryEventResponseDto;

import JGS.CasperEvent.domain.event.entity.casperBot.CasperBot;

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

public record LotteryEventExpectationResponseDto(String expectation, LocalDate createdDate,
LocalTime createdTime) {

public static LotteryEventExpectationResponseDto of(CasperBot casperBot) {

LocalDateTime createdAt = casperBot.getCreatedAt();
LocalDate createdDate = createdAt.toLocalDate();
LocalTime createdTime = createdAt.toLocalTime();

return new LotteryEventExpectationResponseDto(
casperBot.getExpectation(),
createdDate,
createdTime
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,11 @@ public LotteryEvent(LocalDateTime startDateTime, LocalDateTime endDateTime, int

public LotteryEvent() {
}

public LotteryEvent updateLotteryEvent(LocalDateTime startDateTime, LocalDateTime endDateTime, int winnerCount) {
this.startDateTime = startDateTime;
this.endDateTime = endDateTime;
this.winnerCount = winnerCount;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface CasperBotRepository extends JpaRepository<CasperBot, Long> {
List<CasperBot> findByPhoneNumber(String phoneNumber);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import java.util.Optional;

@Repository
public interface LotteryParticipantsRepository extends JpaRepository<LotteryParticipants, String> {
public interface LotteryParticipantsRepository extends JpaRepository<LotteryParticipants, Long> {
Optional<LotteryParticipants> findByBaseUser(BaseUser baseUser);

Page<LotteryParticipants> findByBaseUser_Id(String id, Pageable pageable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@
import JGS.CasperEvent.domain.event.dto.ResponseDto.lotteryEventResponseDto.LotteryEventParticipantsListResponseDto;
import JGS.CasperEvent.domain.event.dto.ResponseDto.lotteryEventResponseDto.LotteryEventParticipantsResponseDto;
import JGS.CasperEvent.domain.event.dto.ResponseDto.lotteryEventResponseDto.LotteryEventResponseDto;
import JGS.CasperEvent.domain.event.dto.ResponseDto.lotteryEventResponseDto.*;
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.entity.admin.Admin;
import JGS.CasperEvent.domain.event.entity.casperBot.CasperBot;
import JGS.CasperEvent.domain.event.entity.event.LotteryEvent;
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.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;
Expand Down Expand Up @@ -57,6 +60,7 @@ public class AdminService {
private final RushParticipantsRepository rushParticipantsRepository;
private final RushOptionRepository rushOptionRepository;
private final S3Service s3Service;
private final CasperBotRepository casperBotRepository;

public Admin verifyAdmin(AdminRequestDto adminRequestDto) {
return adminRepository.findByIdAndPassword(adminRequestDto.getAdminId(), adminRequestDto.getPassword()).orElseThrow(NoSuchElementException::new);
Expand Down Expand Up @@ -240,12 +244,7 @@ else if (newStartDateTime.isBefore(now)) {
}

// 필드 업데이트
currentLotteryEvent.setStartDateTime(newStartDateTime);
currentLotteryEvent.setEndDateTime(newEndDateTime);
currentLotteryEvent.setWinnerCount(lotteryEventRequestDto.getWinnerCount());

// 저장
lotteryEventRepository.save(currentLotteryEvent);
currentLotteryEvent.updateLotteryEvent(newStartDateTime, newEndDateTime, lotteryEventRequestDto.getWinnerCount());

return LotteryEventDetailResponseDto.of(currentLotteryEvent);
}
Expand Down Expand Up @@ -311,4 +310,16 @@ else if (startDateTime.isBefore(now)) {
}
return rushEventResponseDtoList;
}


public List<LotteryEventExpectationResponseDto> getLotteryEventExpectations(Long participantId) {
LotteryParticipants lotteryParticipant = lotteryParticipantsRepository.findById(participantId).orElseThrow(
() -> new CustomException(CustomErrorCode.USER_NOT_FOUND)
);

List<CasperBot> casperBotList = casperBotRepository.findByPhoneNumber(lotteryParticipant.getBaseUser().getId());

return casperBotList.stream().map(LotteryEventExpectationResponseDto::of).toList();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,7 @@ public class LotteryEventService {
public CasperBotResponseDto postCasperBot(BaseUser user, CasperBotRequestDto casperBotRequestDto) throws CustomException, NoSuchPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException {
LotteryParticipants participants = registerUserIfNeed(user, casperBotRequestDto);

List<LotteryEvent> lotteryEventList = lotteryEventRepository.findAll();

if (lotteryEventList.isEmpty()) {
throw new CustomException("현재 진행중인 lotteryEvent가 존재하지 않습니다.", CustomErrorCode.NO_LOTTERY_EVENT);
}

if (lotteryEventList.size() > 1) {
throw new CustomException("현재 진행중인 lotteryEvent가 2개 이상입니다.", CustomErrorCode.TOO_MANY_LOTTERY_EVENT);
}

LotteryEvent lotteryEvent = lotteryEventList.get(0);
LotteryEvent lotteryEvent = getEvent();

CasperBot casperBot = casperBotRepository.save(new CasperBot(casperBotRequestDto, user.getId()));
lotteryEvent.addAppliedCount();
Expand Down Expand Up @@ -112,6 +102,11 @@ public LotteryParticipants registerUserIfNeed(BaseUser user, CasperBotRequestDto
}

public LotteryEventResponseDto getLotteryEvent() {
LotteryEvent lotteryEvent = getEvent();
return LotteryEventResponseDto.of(lotteryEvent, LocalDateTime.now());
}

private LotteryEvent getEvent() {
List<LotteryEvent> lotteryEventList = lotteryEventRepository.findAll();

if (lotteryEventList.isEmpty()) {
Expand All @@ -122,7 +117,6 @@ public LotteryEventResponseDto getLotteryEvent() {
throw new CustomException("현재 진행중인 lotteryEvent가 2개 이상입니다.", CustomErrorCode.TOO_MANY_LOTTERY_EVENT);
}

LotteryEvent lotteryEvent = lotteryEventList.get(0);
return LotteryEventResponseDto.of(lotteryEvent, LocalDateTime.now());
return lotteryEventList.get(0);
}
}

0 comments on commit bcea85f

Please sign in to comment.