diff --git a/src/main/java/newCar/event_page/entity/event/quiz/Quiz.java b/src/main/java/newCar/event_page/entity/event/quiz/Quiz.java index e0bda88..3cece8e 100644 --- a/src/main/java/newCar/event_page/entity/event/quiz/Quiz.java +++ b/src/main/java/newCar/event_page/entity/event/quiz/Quiz.java @@ -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; @@ -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("질문을 입력해주세요"); diff --git a/src/main/java/newCar/event_page/service/EventService.java b/src/main/java/newCar/event_page/service/EventService.java index 2b09c00..a2a4163 100644 --- a/src/main/java/newCar/event_page/service/EventService.java +++ b/src/main/java/newCar/event_page/service/EventService.java @@ -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 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 quizList = quizRepository.findAllByOrderByPostDateAsc(); for (Quiz quiz : quizList) { quiz.setPostDate(startDate); startDate = startDate.plusDays(1L); } - quizList.forEach(quizRepository::save); - //quizRepository.saveAll(quizList); + quizRepository.saveAll(quizList); } }