Skip to content

Commit

Permalink
이벤트 진행 날짜보다 적은 데이터가 존재할 경우 퀴즈 더미데이터를 생성한다
Browse files Browse the repository at this point in the history
  • Loading branch information
장준하 authored and 장준하 committed Aug 5, 2024
1 parent 32bcf85 commit 94fc4bd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
5 changes: 1 addition & 4 deletions src/main/java/newCar/event_page/entity/event/quiz/Quiz.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@

import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import newCar.event_page.dto.QuizDTO;
import newCar.event_page.entity.event.EventId;

import java.time.LocalDate;

Expand Down Expand Up @@ -56,7 +53,7 @@ public void update(QuizDTO quizDTO) {
this.correctAnswer=quizDTO.getCorrectAnswer();
}

public static Quiz getDummy(QuizEvent quizEvent){
public static Quiz createDummy(QuizEvent quizEvent){
Quiz quiz = new Quiz();
quiz.setPostDate(LocalDate.parse("2000-01-01"));
quiz.setQuestion("질문을 입력해주세요");
Expand Down
26 changes: 15 additions & 11 deletions src/main/java/newCar/event_page/service/EventService.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,34 +37,38 @@ public EventCommonDTO getEventInfo() {
public EventCommonDTO updateEventInfo(EventCommonDTO eventCommonDTO){
EventCommon eventCommon = eventCommonRepository.findById(1L)
.orElseThrow(() -> new NoSuchElementException("공통 이벤트 정보가 존재하지 않아 수정이 불가능합니다."));

eventCommon.update(eventCommonDTO);

long duration = eventCommon.getDuration();
updateQuiz(eventCommonDTO.getStartTime().toLocalDate() , duration);

putDummyIfRequired(duration);
updateQuiz(eventCommonDTO.getStartTime().toLocalDate() , duration);

return EventCommonDTO.toDTO(eventCommon);
}

private void updateQuiz(LocalDate startDate, long duration) {
List<Quiz> quizList = quizRepository.findAllByOrderByPostDateAsc();
private void putDummyIfRequired(long duration) {
long quizCount = quizRepository.count();

if(quizCount > duration) return;

QuizEvent quizEvent = quizEventRepository.findById(EventId.Quiz.getValue())
.orElseThrow(() -> new NoSuchElementException("퀴즈 이벤트가 존재하지 않습니다."));

int quizCount = quizList.size();
if (quizCount < duration) {
for (int i = 0; i < duration - quizCount; i++) {
quizList.add(Quiz.getDummy(quizEvent));
}
for(int i = 0; i < duration - quizCount; i++){
quizRepository.save(Quiz.createDummy(quizEvent));
}
}

private void updateQuiz(LocalDate startDate, long duration) {
List<Quiz> quizList = quizRepository.findAllByOrderByPostDateAsc();

for (Quiz quiz : quizList) {
quiz.setPostDate(startDate);
startDate = startDate.plusDays(1L);
}

quizList.forEach(quizRepository::save);
//quizRepository.saveAll(quizList);
quizRepository.saveAll(quizList);
}
}

0 comments on commit 94fc4bd

Please sign in to comment.