Skip to content

Commit

Permalink
Merge pull request #95 from softeerbootcamp4th/fix/#91-joo-feedback
Browse files Browse the repository at this point in the history
Fix/#91 joo feedback
  • Loading branch information
wjddn2165 authored Aug 12, 2024
2 parents 2dfbd4a + 2dd3a51 commit c3c2603
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@
import JGS.CasperEvent.global.entity.BaseUser;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import org.apache.coyote.BadRequestException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.nio.file.attribute.UserPrincipalNotFoundException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

@RestController
Expand All @@ -41,7 +45,7 @@ public ResponseEntity<LotteryEventResponseDto> getLotteryEvent(){
@PostMapping("/casperBot")
public ResponseEntity<CasperBotResponseDto> postCasperBot(
HttpServletRequest request,
@RequestBody @Valid CasperBotRequestDto postCasperBot) throws BadRequestException {
@RequestBody @Valid CasperBotRequestDto postCasperBot) throws NoSuchPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException {
BaseUser user = (BaseUser) request.getAttribute("user");
return ResponseEntity
.status(HttpStatus.CREATED)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import lombok.Getter;

@Getter
public class CasperBotRequestDto {

@NotNull(message = "눈 모양 값은 필수 필드입니다.")
Expand Down Expand Up @@ -35,37 +37,5 @@ public class CasperBotRequestDto {
private String name;

private String expectation;


public Integer getEyeShape() {
return eyeShape;
}

public Integer getEyePosition() {
return eyePosition;
}

public Integer getMouthShape() {
return mouthShape;
}

public Integer getColor() {
return color;
}

public Integer getSticker() {
return sticker;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getExpectation() {
return expectation;
}
private String referralId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,27 @@

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

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

public record LotteryEventDetailResponseDto(LocalDateTime startDateTime, LocalDateTime endDateTime,
AtomicInteger appliedCount, int winnerCount,
LocalDateTime createdAt, LocalDateTime updatedAt) {
public record LotteryEventDetailResponseDto(
LocalDate startDate, LocalTime startTime,
LocalDate endDate, LocalTime endTime,
AtomicInteger appliedCount, int winnerCount,
LocalDateTime createdAt, LocalDateTime updatedAt) {

public static ArrayList<LotteryEventDetailResponseDto> of(List<LotteryEvent> lotteryEvent) {
ArrayList<LotteryEventDetailResponseDto> lotteryEventDtoList = new ArrayList<>();
for (LotteryEvent event : lotteryEvent) {
lotteryEventDtoList.add(new LotteryEventDetailResponseDto(
event.getStartDateTime(),
event.getEndDateTime(),
event.getStartDateTime().toLocalDate(),
event.getStartDateTime().toLocalTime(),
event.getEndDateTime().toLocalDate(),
event.getEndDateTime().toLocalTime(),
event.getAppliedCount(),
event.getWinnerCount(),
event.getCreatedAt(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,35 @@
import JGS.CasperEvent.global.error.exception.CustomException;
import JGS.CasperEvent.global.error.exception.LotteryEventNotExists;
import JGS.CasperEvent.global.jwt.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import JGS.CasperEvent.global.util.AESUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import java.nio.file.attribute.UserPrincipalNotFoundException;
import java.time.LocalDate;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Optional;

@Service
@Transactional
@RequiredArgsConstructor
public class LotteryEventService {

private final UserRepository userRepository;
private final LotteryEventRepository lotteryEventRepository;
private final LotteryParticipantsRepository lotteryParticipantsRepository;
private final CasperBotRepository casperBotRepository;
private final RedisService redisService;
private final SecretKey secretKey;

@Autowired
public LotteryEventService(LotteryEventRepository lotteryEventRepository,
LotteryParticipantsRepository lotteryParticipantsRepository,
CasperBotRepository casperBotRepository,
RedisService redisService, UserRepository userRepository) {
this.lotteryEventRepository = lotteryEventRepository;
this.lotteryParticipantsRepository = lotteryParticipantsRepository;
this.casperBotRepository = casperBotRepository;
this.redisService = redisService;
this.userRepository = userRepository;
}

public CasperBotResponseDto postCasperBot(BaseUser user, CasperBotRequestDto casperBotRequestDto) throws CustomException {
LotteryParticipants participants = registerUserIfNeed(user);
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);

CasperBot casperBot = casperBotRepository.save(new CasperBot(casperBotRequestDto, user.getId()));
Expand Down Expand Up @@ -80,26 +76,32 @@ public CasperBotResponseDto getCasperBot(Long casperId) {
}


public LotteryParticipants registerUserIfNeed(BaseUser user) {
public LotteryParticipants registerUserIfNeed(BaseUser user, CasperBotRequestDto casperBotRequestDto) throws NoSuchPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException {
LotteryParticipants participant = lotteryParticipantsRepository.findByBaseUser(user).orElse(null);

if (participant == null) {
participant = new LotteryParticipants(user);
lotteryParticipantsRepository.save(participant);
}

if (!casperBotRequestDto.getReferralId().isEmpty()) {
String referralId = AESUtils.decrypt(casperBotRequestDto.getReferralId(), secretKey);
Optional<LotteryParticipants> referralParticipant =
lotteryParticipantsRepository.findByBaseUser(
userRepository.findById(referralId).orElse(null)
);
referralParticipant.ifPresent(LotteryParticipants::linkClickedCountAdded);
}

user.updateLotteryParticipants(participant);
userRepository.save(user);

return participant;
}

// TODO: 가짜 API, DB 접속되도록 수정
public LotteryEventResponseDto getLotteryEvent() {
return new LotteryEventResponseDto(LocalDateTime.now(),
LocalDate.of(2000, 9, 27).atStartOfDay(),
LocalDate.of(2100, 9, 27).atStartOfDay(),
ChronoUnit.DAYS.between(LocalDate.of(2000, 9, 27), LocalDate.of(2100, 9, 27)));
LotteryEvent lotteryEvent = lotteryEventRepository.findById(1L).orElseThrow(LotteryEventNotExists::new);
return LotteryEventResponseDto.of(lotteryEvent, LocalDateTime.now());
}

}

0 comments on commit c3c2603

Please sign in to comment.