Skip to content

Commit

Permalink
Merge pull request #83 from softeerbootcamp4th/feat/#81-lottery-event
Browse files Browse the repository at this point in the history
Feat/#81 lottery event
  • Loading branch information
wjddn2165 authored Aug 11, 2024
2 parents 1cbf11c + 57627e0 commit ddabea4
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package JGS.CasperEvent.domain.event.controller.adminController;

import JGS.CasperEvent.domain.event.dto.RequestDto.AdminRequestDto;
import JGS.CasperEvent.domain.event.dto.RequestDto.LotteryEventRequestDto;
import JGS.CasperEvent.domain.event.dto.ResponseDto.LotteryEventResponseDto;
import JGS.CasperEvent.domain.event.service.adminService.AdminService;
import JGS.CasperEvent.global.response.ResponseDto;
import jakarta.validation.Valid;
Expand All @@ -20,9 +22,17 @@ public AdminController(AdminService adminService) {
}

@PostMapping("/join")
public ResponseEntity<ResponseDto> postAdmin(@RequestBody @Valid AdminRequestDto adminRequestDto){
public ResponseEntity<ResponseDto> postAdmin(@RequestBody @Valid AdminRequestDto adminRequestDto) {
return ResponseEntity
.status(HttpStatus.CREATED)
.body(adminService.postAdmin(adminRequestDto));
}

@PostMapping("/event/lottery")
public ResponseEntity<LotteryEventResponseDto> createLotteryEvent(
@Valid @RequestBody LotteryEventRequestDto lotteryEventRequestDto) {
return ResponseEntity
.status(HttpStatus.CREATED)
.body(adminService.createLotteryEvent(lotteryEventRequestDto));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package JGS.CasperEvent.domain.event.dto.RequestDto;

import jakarta.validation.constraints.NotNull;
import lombok.Getter;

import java.time.LocalDateTime;

@Getter
public class LotteryEventRequestDto {

@NotNull(message = "이벤트 시작 일자를 지정하세요.")
private LocalDateTime eventStartDate;

@NotNull(message = "이벤트 종료 일자를 지정하세요.")
private LocalDateTime eventEndDate;

@NotNull(message = "당첨인원 수를 지정하세요.")
private int winnerCount;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

import JGS.CasperEvent.domain.event.entity.event.LotteryEvent;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

public record LotteryEventResponseDto(LocalDateTime serverDateTime, LocalDateTime eventStartDate, LocalDateTime eventEndDate,
long activePeriod) {
public static LotteryEventResponseDto of(LocalDateTime serverDateTime, LotteryEvent lotteryEvent) {
public static LotteryEventResponseDto of(LotteryEvent lotteryEvent, LocalDateTime serverDateTime) {
return new LotteryEventResponseDto(
serverDateTime,
lotteryEvent.getStartDateTime(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,22 @@
import jakarta.persistence.*;
import lombok.Getter;

import java.time.LocalDateTime;

@Entity
@Getter
public class LotteryEvent extends BaseEvent {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long lotteryEventId;

public LotteryEvent(LocalDateTime eventStartDate, LocalDateTime eventEndDate, int winnerCount){
this.eventStartDate = eventStartDate;
this.eventEndDate = eventEndDate;
this.winnerCount = winnerCount;
}

public LotteryEvent() {

}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
package JGS.CasperEvent.domain.event.service.adminService;

import JGS.CasperEvent.domain.event.dto.RequestDto.AdminRequestDto;
import JGS.CasperEvent.domain.event.dto.RequestDto.LotteryEventRequestDto;
import JGS.CasperEvent.domain.event.dto.ResponseDto.LotteryEventResponseDto;
import JGS.CasperEvent.domain.event.entity.admin.Admin;
import JGS.CasperEvent.domain.event.entity.event.LotteryEvent;
import JGS.CasperEvent.domain.event.repository.AdminRepository;
import JGS.CasperEvent.domain.event.repository.eventRepository.LotteryEventRepository;
import JGS.CasperEvent.domain.event.repository.eventRepository.RushEventRepository;
import JGS.CasperEvent.global.enums.CustomErrorCode;
import JGS.CasperEvent.global.enums.Role;
import JGS.CasperEvent.global.error.exception.CustomException;
import JGS.CasperEvent.global.response.ResponseDto;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.NoSuchElementException;

@RequiredArgsConstructor
@Service
public class AdminService {

private final AdminRepository adminRepository;
private final LotteryEventRepository lotteryEventRepository;
private final RushEventRepository rushEventRepository;

public Admin verifyAdmin(AdminRequestDto adminRequestDto) {
return adminRepository.findById(adminRequestDto.getAdminId()).orElseThrow(NoSuchElementException::new);
Expand All @@ -33,4 +42,14 @@ public ResponseDto postAdmin(AdminRequestDto adminRequestDto) {

return ResponseDto.of("관리자 생성 성공");
}

public LotteryEventResponseDto createLotteryEvent(LotteryEventRequestDto lotteryEventRequestDto) {
LotteryEvent lotteryEvent = lotteryEventRepository.save(new LotteryEvent(
lotteryEventRequestDto.getEventStartDate(),
lotteryEventRequestDto.getEventEndDate(),
lotteryEventRequestDto.getWinnerCount()
));

return LotteryEventResponseDto.of(lotteryEvent, LocalDateTime.now());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
@Getter
public enum CustomErrorCode {
NO_RUSH_EVENT("선착순 이벤트를 찾을 수 없습니다.", 404),
INVALID_CASPERBOT_PARAMETER("잘못된 파라미터 입력입니다.", 400),
INVALID_PARAMETER("잘못된 파라미터 입력입니다.", 400),
CASPERBOT_NOT_FOUND("배지를 찾을 수 없습니다.", 404),
BAD_REQUEST("잘못된 요청입니다.", 400),
UNAUTHORIZED("권한이 없습니다.", 401),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public ResponseEntity<ErrorResponse> methodArgumentNotValidExceptionHandler(Meth

return ResponseEntity
.status(HttpStatus.BAD_REQUEST)
.body(ErrorResponse.of(CustomErrorCode.INVALID_CASPERBOT_PARAMETER, builder.toString()));
.body(ErrorResponse.of(CustomErrorCode.INVALID_PARAMETER, builder.toString()));
}

@ExceptionHandler(RuntimeException.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
@Slf4j
@RequiredArgsConstructor
public class VerifyAdminFilter implements Filter {
public static final String AUTHENTICATE_ADMIN = "authenticateAdmin";
public static final String AUTHENTICATE_ADMIN = "authenticateUser";
private final ObjectMapper objectMapper;

private final AdminService adminService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ void createCasperBotFailureTest_RequiredFieldNotExist() throws Exception {

//then
perform.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.errorCode").value("INVALID_CASPERBOT_PARAMETER"))
.andExpect(jsonPath("$.errorCode").value("INVALID_PARAMETER"))
.andDo(print());

}
Expand Down Expand Up @@ -141,7 +141,7 @@ void createCasperBotSuccessTest_WrongValue() throws Exception {

//then
perform.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.errorCode").value("INVALID_CASPERBOT_PARAMETER"))
.andExpect(jsonPath("$.errorCode").value("INVALID_PARAMETER"))
.andDo(print());
}

Expand Down

0 comments on commit ddabea4

Please sign in to comment.