Skip to content

Commit

Permalink
Merge pull request #111 from softeerbootcamp4th/feature/#109-admin-lo…
Browse files Browse the repository at this point in the history
…ttery-event-delete

Feature/#109 admin lottery event delete
  • Loading branch information
k000927 authored Aug 13, 2024
2 parents 1e87352 + a740121 commit fd3273c
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,26 @@ public ResponseEntity<LotteryEventParticipantsListResponseDto> getLotteryEventPa
@PostMapping("/event/rush")
public ResponseEntity<RushEventResponseDto> createRushEvent(
@RequestPart(value = "dto") RushEventRequestDto rushEventRequestDto,
@RequestPart(value = "prizeImg")MultipartFile prizeImg,
@RequestPart(value = "leftOptionImg")MultipartFile leftOptionImg,
@RequestPart(value = "rightOptionImg")MultipartFile rightOptionImg) {
@RequestPart(value = "prizeImg") MultipartFile prizeImg,
@RequestPart(value = "leftOptionImg") MultipartFile leftOptionImg,
@RequestPart(value = "rightOptionImg") MultipartFile rightOptionImg) {
return ResponseEntity
.status(HttpStatus.CREATED)
.body(adminService.createRushEvent(rushEventRequestDto, prizeImg, leftOptionImg, rightOptionImg));
}

// 선착순 이벤트 전체 조회
@GetMapping("/event/rush")
public ResponseEntity<List<AdminRushEventResponseDto>> getRushEvents(){
public ResponseEntity<List<AdminRushEventResponseDto>> getRushEvents() {
return ResponseEntity
.status(HttpStatus.OK)
.body(adminService.getRushEvents());
}

// 추첨 이벤트 삭제
@DeleteMapping("/event/lottery")
public ResponseEntity<Void> deleteLotteryEvent() {
adminService.deleteLotteryEvent();
return ResponseEntity.noContent().build(); // 204 No Content
}
}
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.Optional;

@Repository
public interface AdminRepository extends JpaRepository<Admin, String> {
Optional<Admin> findByIdAndPassword(String id, String password);
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class AdminService {
private final S3Service s3Service;

public Admin verifyAdmin(AdminRequestDto adminRequestDto) {
return adminRepository.findById(adminRequestDto.getAdminId()).orElseThrow(NoSuchElementException::new);
return adminRepository.findByIdAndPassword(adminRequestDto.getAdminId(), adminRequestDto.getPassword()).orElseThrow(NoSuchElementException::new);
}

public ResponseDto postAdmin(AdminRequestDto adminRequestDto) {
Expand Down Expand Up @@ -153,4 +153,18 @@ public List<AdminRushEventResponseDto> getRushEvents(){
List<RushEvent> rushEvents = rushEventRepository.findAll();
return AdminRushEventResponseDto.of(rushEvents);
}

public void deleteLotteryEvent() {
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);
}

lotteryEventRepository.deleteAll();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@Service
Expand All @@ -45,7 +46,18 @@ public class LotteryEventService {

public CasperBotResponseDto postCasperBot(BaseUser user, CasperBotRequestDto casperBotRequestDto) throws CustomException, NoSuchPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException {
LotteryParticipants participants = registerUserIfNeed(user, casperBotRequestDto);
LotteryEvent lotteryEvent = lotteryEventRepository.findById(1L).orElseThrow(LotteryEventNotExists::new);

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

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

public LotteryEventResponseDto getLotteryEvent() {
LotteryEvent lotteryEvent = lotteryEventRepository.findById(1L).orElseThrow(LotteryEventNotExists::new);
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);
return LotteryEventResponseDto.of(lotteryEvent, LocalDateTime.now());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public enum CustomErrorCode {
LOTTERY_EVENT_ALREADY_EXISTS("추첨 이벤트가 이미 존재합니다.", 409),
INVALID_RUSH_EVENT_OPTIONS_COUNT("이벤트의 옵션 수가 올바르지 않습니다.", 500),
INVALID_RUSH_EVENT_OPTION_ID("옵션 ID는 1 또는 2여야 합니다.", 400),
EMPTY_FILE("유효하지 않은 파일입니다.", 422);
EMPTY_FILE("유효하지 않은 파일입니다.", 422),
TOO_MANY_LOTTERY_EVENT("현재 진행중인 추첨 이벤트가 2개 이상입니다.", 409);

private final String message;
private int status;
Expand Down

0 comments on commit fd3273c

Please sign in to comment.