-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
03a651f
commit a1c433d
Showing
9 changed files
with
263 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/main/java/org/dongguk/onroad/roadmap/application/dto/request/CreateQuizRequestDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package org.dongguk.onroad.roadmap.application.dto.request; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import jakarta.validation.constraints.NotNull; | ||
import jakarta.validation.constraints.Size; | ||
|
||
import java.util.List; | ||
|
||
public record CreateQuizRequestDto( | ||
|
||
@NotNull(message = "퀴즈 목록은 필수 입력 값입니다.") | ||
@JsonProperty("quiz_list") | ||
List<Quiz> quizList | ||
|
||
) { | ||
public record Quiz( | ||
|
||
@NotNull(message = "check Point id는 필수 입력 값입니다.") | ||
@JsonProperty("id") | ||
Long id, | ||
|
||
@NotNull(message = "퀴즈 내용은 필수 입력 값입니다.") | ||
@JsonProperty("content") | ||
String content, | ||
|
||
@NotNull(message = "선택지 목록은 필수 입력 값입니다.") | ||
@Size(min = 3, max = 3, message = "선택지는 반드시 3개여야 합니다.") | ||
@JsonProperty("choice_list") | ||
List<Choice> choiceList | ||
|
||
) {} | ||
|
||
public record Choice( | ||
|
||
@NotNull(message = "선택지 내용은 필수 입력 값입니다.") | ||
@JsonProperty("content") | ||
String content, | ||
|
||
@NotNull(message = "정답 여부는 필수 입력 값입니다.") | ||
@JsonProperty("is_answer") | ||
Boolean isAnswer | ||
|
||
) {} | ||
} |
60 changes: 60 additions & 0 deletions
60
src/main/java/org/dongguk/onroad/roadmap/application/service/CreateQuizService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package org.dongguk.onroad.roadmap.application.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.dongguk.onroad.core.exception.error.ErrorCode; | ||
import org.dongguk.onroad.core.exception.type.CommonException; | ||
import org.dongguk.onroad.roadmap.application.dto.request.CreateQuizRequestDto; | ||
import org.dongguk.onroad.roadmap.application.usecase.CreateQuizUseCase; | ||
import org.dongguk.onroad.roadmap.domain.CheckPoint; | ||
import org.dongguk.onroad.roadmap.domain.Choice; | ||
import org.dongguk.onroad.roadmap.domain.Quiz; | ||
import org.dongguk.onroad.roadmap.domain.service.ChoiceService; | ||
import org.dongguk.onroad.roadmap.domain.service.QuizService; | ||
import org.dongguk.onroad.roadmap.domain.type.EQuizType; | ||
import org.dongguk.onroad.roadmap.repository.CheckPointRepository; | ||
import org.dongguk.onroad.roadmap.repository.QuizRepository; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.List; | ||
import java.util.UUID; | ||
import java.util.stream.Stream; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class CreateQuizService implements CreateQuizUseCase { | ||
|
||
private final CheckPointRepository checkPointRepository; | ||
|
||
private final QuizService quizService; | ||
private final ChoiceService choiceService; | ||
private final QuizRepository quizRepository; | ||
|
||
@Override | ||
@Transactional | ||
public void execute(UUID userId, CreateQuizRequestDto requestDto) { | ||
|
||
List<Quiz> quizList = requestDto.quizList().stream() | ||
.map(quizRequestDto -> { | ||
CheckPoint checkPoint = checkPointRepository.findById(quizRequestDto.id()) | ||
.orElseThrow(() -> new CommonException(ErrorCode.NOT_FOUND_RESOURCE)); | ||
|
||
Quiz quiz = quizService.createQuiz( | ||
EQuizType.MULTIPLE_CHOICE, | ||
quizRequestDto.content(), | ||
checkPoint | ||
); | ||
|
||
quizRequestDto.choiceList().forEach( | ||
choiceRequestDto -> choiceService.createChoice( | ||
choiceRequestDto.content(), | ||
choiceRequestDto.isAnswer(), | ||
quiz | ||
)); | ||
|
||
return quiz; | ||
}).toList(); | ||
|
||
quizRepository.saveAll(quizList); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/org/dongguk/onroad/roadmap/application/usecase/CreateQuizUseCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package org.dongguk.onroad.roadmap.application.usecase; | ||
|
||
import org.dongguk.onroad.core.annotation.bean.UseCase; | ||
import org.dongguk.onroad.roadmap.application.dto.request.CreateQuizRequestDto; | ||
|
||
import java.util.UUID; | ||
|
||
@UseCase | ||
public interface CreateQuizUseCase { | ||
|
||
void execute( | ||
UUID userId, | ||
CreateQuizRequestDto requestDto | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/main/java/org/dongguk/onroad/roadmap/domain/service/ChoiceService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package org.dongguk.onroad.roadmap.domain.service; | ||
|
||
import org.dongguk.onroad.roadmap.domain.Choice; | ||
import org.dongguk.onroad.roadmap.domain.Quiz; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class ChoiceService { | ||
|
||
public Choice createChoice(String content, boolean isAnswer, Quiz quiz) { | ||
|
||
Choice choice = Choice.builder() | ||
.content(content) | ||
.isAnswer(isAnswer) | ||
.quiz(quiz) | ||
.build(); | ||
|
||
quiz.getChoices().add(choice); | ||
|
||
return choice; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/org/dongguk/onroad/roadmap/domain/service/QuizService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package org.dongguk.onroad.roadmap.domain.service; | ||
|
||
import org.dongguk.onroad.roadmap.domain.CheckPoint; | ||
import org.dongguk.onroad.roadmap.domain.Quiz; | ||
import org.dongguk.onroad.roadmap.domain.type.EQuizType; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class QuizService { | ||
|
||
public Quiz createQuiz(EQuizType quizType, String content, CheckPoint checkPoint) { | ||
Quiz quiz = Quiz.builder() | ||
.type(quizType) | ||
.content(content) | ||
.checkPoint(checkPoint) | ||
.build(); | ||
|
||
checkPoint.updateQuiz(quiz); | ||
|
||
return quiz; | ||
} | ||
} |