diff --git a/src/main/java/team5/doghae/domain/question/Tag.java b/src/main/java/team5/doghae/domain/question/Tag.java index ac83467..1383b7d 100644 --- a/src/main/java/team5/doghae/domain/question/Tag.java +++ b/src/main/java/team5/doghae/domain/question/Tag.java @@ -1,5 +1,5 @@ package team5.doghae.domain.question; public enum Tag { - GRAMMAR, EAZY, NORMAL, HARD + GRAMMAR, EASY, NORMAL, HARD } diff --git a/src/main/java/team5/doghae/domain/question/dto/QuestionRequest.java b/src/main/java/team5/doghae/domain/question/dto/QuestionRequest.java new file mode 100644 index 0000000..06bc733 --- /dev/null +++ b/src/main/java/team5/doghae/domain/question/dto/QuestionRequest.java @@ -0,0 +1,16 @@ +package team5.doghae.domain.question.dto; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +public class QuestionRequest { + + @Getter + @NoArgsConstructor + @AllArgsConstructor + public static class Evaluate { + private Long questionId; + private String answer; + } +} diff --git a/src/main/java/team5/doghae/domain/question/dto/QuestionResponse.java b/src/main/java/team5/doghae/domain/question/dto/QuestionResponse.java index 54787bd..6c991d7 100644 --- a/src/main/java/team5/doghae/domain/question/dto/QuestionResponse.java +++ b/src/main/java/team5/doghae/domain/question/dto/QuestionResponse.java @@ -38,4 +38,28 @@ public static QuestionResponse.Create of(Question question) { } } + + @Getter + @Builder + @AllArgsConstructor + @NoArgsConstructor + public static class Evaluate { + private Long questionId; + private String keyword; + private String problem; + private List choices; + private Tag tag; + private boolean isAnswer; + + public static QuestionResponse.Evaluate of(Question question, boolean isAnswer) { + return Evaluate.builder() + .questionId(question.getId()) + .keyword(question.getKeyword()) + .problem(question.getProblem()) + .choices(question.getChoices()) + .tag(question.getTag()) + .isAnswer(isAnswer) + .build(); + } + } } diff --git a/src/main/java/team5/doghae/domain/review/repository/ReviewRepository.java b/src/main/java/team5/doghae/domain/review/repository/ReviewRepository.java new file mode 100644 index 0000000..678c348 --- /dev/null +++ b/src/main/java/team5/doghae/domain/review/repository/ReviewRepository.java @@ -0,0 +1,7 @@ +package team5.doghae.domain.review.repository; + +import org.springframework.data.jpa.repository.JpaRepository; +import team5.doghae.domain.review.domain.Review; + +public interface ReviewRepository extends JpaRepository { +} diff --git a/src/main/java/team5/doghae/domain/review/service/ReviewService.java b/src/main/java/team5/doghae/domain/review/service/ReviewService.java new file mode 100644 index 0000000..5c55a0a --- /dev/null +++ b/src/main/java/team5/doghae/domain/review/service/ReviewService.java @@ -0,0 +1,14 @@ +package team5.doghae.domain.review.service; + +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import team5.doghae.domain.review.repository.ReviewRepository; + +@Service +@RequiredArgsConstructor +public class ReviewService { + + private final ReviewRepository reviewRepository; + + +} diff --git a/src/main/java/team5/doghae/domain/stage/controller/StageController.java b/src/main/java/team5/doghae/domain/stage/controller/StageController.java index 558d992..472bb65 100644 --- a/src/main/java/team5/doghae/domain/stage/controller/StageController.java +++ b/src/main/java/team5/doghae/domain/stage/controller/StageController.java @@ -3,14 +3,14 @@ import lombok.RequiredArgsConstructor; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import team5.doghae.common.resolver.AuthUser; import team5.doghae.common.response.SuccessResponse; import team5.doghae.common.security.jwt.JwtTokenInfo; +import team5.doghae.domain.stage.dto.StageRequest; import team5.doghae.domain.stage.service.StageService; + @RestController @RequiredArgsConstructor public class StageController { @@ -27,4 +27,15 @@ public ResponseEntity getStage( ).setStatus(HttpStatus.OK); } + @PostMapping("/stage/{stageId}") + public ResponseEntity evaluateStage( + @AuthUser JwtTokenInfo jwtTokenInfo, + @PathVariable("stageId") Long stageId, + @RequestBody StageRequest.Evaluate evaluateRequest + ) { + return SuccessResponse.of( + stageService.evaluateStage(jwtTokenInfo.getUserId(), stageId, evaluateRequest) + ).setStatus(HttpStatus.OK); + } + } diff --git a/src/main/java/team5/doghae/domain/stage/domain/StageResultInfo.java b/src/main/java/team5/doghae/domain/stage/domain/StageResultInfo.java index dedb187..37c5181 100644 --- a/src/main/java/team5/doghae/domain/stage/domain/StageResultInfo.java +++ b/src/main/java/team5/doghae/domain/stage/domain/StageResultInfo.java @@ -4,6 +4,9 @@ import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; +import team5.doghae.domain.question.Tag; + +import java.util.Map; @Embeddable @Getter @@ -12,8 +15,26 @@ public class StageResultInfo { private int grammarCount; - private int eazyCount; + private int easyCount; private int normalCount; private int hardCount; + public static StageResultInfo of(Map map) { + int grammarCount = map.getOrDefault(Tag.GRAMMAR, 0); + int easyCount = map.getOrDefault(Tag.EASY, 0); + int normalCount = map.getOrDefault(Tag.NORMAL, 0); + int hardCount = map.getOrDefault(Tag.HARD, 0); + + return new StageResultInfo(grammarCount, easyCount, normalCount, hardCount); + } + + @Override + public String toString() { + return "StageResultInfo{" + + "grammarCount=" + grammarCount + + ", easyCount=" + easyCount + + ", normalCount=" + normalCount + + ", hardCount=" + hardCount + + '}'; + } } diff --git a/src/main/java/team5/doghae/domain/stage/dto/StageRequest.java b/src/main/java/team5/doghae/domain/stage/dto/StageRequest.java new file mode 100644 index 0000000..4d2b197 --- /dev/null +++ b/src/main/java/team5/doghae/domain/stage/dto/StageRequest.java @@ -0,0 +1,20 @@ +package team5.doghae.domain.stage.dto; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import team5.doghae.domain.question.dto.QuestionRequest; + +import java.util.List; + +public class StageRequest { + + @Getter + @NoArgsConstructor + @AllArgsConstructor + public static class Evaluate { + + private List answers; + + } +} diff --git a/src/main/java/team5/doghae/domain/stage/dto/StageResponse.java b/src/main/java/team5/doghae/domain/stage/dto/StageResponse.java index d2a52b1..81cb51c 100644 --- a/src/main/java/team5/doghae/domain/stage/dto/StageResponse.java +++ b/src/main/java/team5/doghae/domain/stage/dto/StageResponse.java @@ -6,6 +6,7 @@ import lombok.NoArgsConstructor; import team5.doghae.domain.stage.domain.Stage; + public class StageResponse { @Getter @@ -27,4 +28,12 @@ public static Create of(Stage stage) { } } + +// @Getter +// @NoArgsConstructor +// @AllArgsConstructor +// public static class Evaluate { +// +// +// } } diff --git a/src/main/java/team5/doghae/domain/stage/repository/StageRepository.java b/src/main/java/team5/doghae/domain/stage/repository/StageRepository.java index 7c56798..047ab8e 100644 --- a/src/main/java/team5/doghae/domain/stage/repository/StageRepository.java +++ b/src/main/java/team5/doghae/domain/stage/repository/StageRepository.java @@ -3,5 +3,6 @@ import org.springframework.data.jpa.repository.JpaRepository; import team5.doghae.domain.stage.domain.Stage; + public interface StageRepository extends JpaRepository { } diff --git a/src/main/java/team5/doghae/domain/stage/service/StageService.java b/src/main/java/team5/doghae/domain/stage/service/StageService.java index d85369c..ab4efa9 100644 --- a/src/main/java/team5/doghae/domain/stage/service/StageService.java +++ b/src/main/java/team5/doghae/domain/stage/service/StageService.java @@ -2,15 +2,20 @@ import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; +import team5.doghae.domain.question.Tag; +import team5.doghae.domain.question.domain.Question; +import team5.doghae.domain.question.dto.QuestionRequest; import team5.doghae.domain.question.dto.QuestionResponse; import team5.doghae.domain.stage.domain.Stage; +import team5.doghae.domain.stage.domain.StageResultInfo; +import team5.doghae.domain.stage.dto.StageRequest; import team5.doghae.domain.stage.repository.StageRepository; import team5.doghae.domain.user.domain.User; import team5.doghae.domain.user.repository.UserRepository; +import team5.doghae.domain.user_stage_map.domain.UserStage; import team5.doghae.domain.user_stage_map.repository.UserStageRepository; -import java.util.List; -import java.util.NoSuchElementException; +import java.util.*; @Service @RequiredArgsConstructor @@ -20,36 +25,69 @@ public class StageService { private final StageRepository stageRepository; private final UserStageRepository userStageRepository; - public List getStage(Long userId, Long stageId) { User user = userRepository.findById(userId) - .orElseThrow( - () -> new NoSuchElementException("존재하지 않는 회원입니다.") - ); + .orElseThrow(() -> new NoSuchElementException("존재하지 않는 회원입니다.")); Stage stage = stageRepository.findById(stageId) - .orElseThrow( - () -> new NoSuchElementException("존재하지 않는 스테이지입니다.") - ); + .orElseThrow(() -> new NoSuchElementException("존재하지 않는 스테이지입니다.")); - List questions = stage.getQuestions() + return stage.getQuestions() .stream() .map(QuestionResponse.Create::of) .toList(); + } - for (QuestionResponse.Create question : questions) { - System.out.println(question.getQuestionId() + ": " + question.getProblem() + " " + question.getTag()); - for (String choice : question.getChoices()) { - System.out.println(choice); - } + public List evaluateStage(Long userId, Long stageId, StageRequest.Evaluate evaluateRequest) { + + User user = userRepository.findById(userId) + .orElseThrow(() -> new NoSuchElementException("존재하지 않는 회원입니다.")); + + Stage stage = stageRepository.findById(stageId) + .orElseThrow(() -> new NoSuchElementException("존재하지 않는 스테이지입니다.")); + + List questions = stage.getQuestions() + .stream() + .sorted(Comparator.comparing(Question::getId)) + .toList(); + + List answers = evaluateRequest.getAnswers() + .stream() + .sorted(Comparator.comparing(QuestionRequest.Evaluate::getQuestionId)) + .toList(); + + if (questions.size() != answers.size()) { + throw new IllegalArgumentException("요청한 문제 정보가 올바르지 않습니다."); } + Map map = new HashMap<>(); - return questions; - } + List result = new ArrayList<>(); + + for (int i = 0; i < questions.size(); i++) { + + Question question = questions.get(i); + QuestionRequest.Evaluate answer = answers.get(i); + + if (!question.getId().equals(answer.getQuestionId())) { + throw new IllegalArgumentException("요청한 문제 정보가 올바르지 않습니다."); + } + + result.add( + QuestionResponse.Evaluate.of(question, question.getAnswer().equals(answer.getAnswer())) + ); + + Tag tag = question.getTag(); + map.put(tag, map.getOrDefault(tag, 0) + 1); + } + + StageResultInfo resultInfo = StageResultInfo.of(map); + System.out.println(resultInfo); - public void evaluateStage() { + UserStage userStage = UserStage.of(user, stage); + userStageRepository.save(userStage); + return result; } } diff --git a/src/main/java/team5/doghae/domain/user_stage_map/domain/UserStage.java b/src/main/java/team5/doghae/domain/user_stage_map/domain/UserStage.java index 454d9c7..be76b2f 100644 --- a/src/main/java/team5/doghae/domain/user_stage_map/domain/UserStage.java +++ b/src/main/java/team5/doghae/domain/user_stage_map/domain/UserStage.java @@ -1,17 +1,19 @@ package team5.doghae.domain.user_stage_map.domain; import jakarta.persistence.*; -import lombok.Getter; -import lombok.Setter; +import lombok.*; import team5.doghae.domain.stage.domain.Stage; import team5.doghae.domain.stage.domain.StageResultInfo; import team5.doghae.domain.user.domain.User; import java.time.LocalDate; +import java.time.ZoneId; @Entity @Getter -@Setter +@Builder +@NoArgsConstructor +@AllArgsConstructor public class UserStage { @Id @@ -33,4 +35,13 @@ public class UserStage { private boolean isSolved; + + public static UserStage of(User user, Stage stage) { + return UserStage.builder() + .user(user) + .stage(stage) + .solvedDate(LocalDate.now(ZoneId.of("Asia/Seoul"))) + .isSolved(true) + .build(); + } }