Skip to content

Commit

Permalink
Merge pull request #37 from doghae/Feat/issue#14
Browse files Browse the repository at this point in the history
Feat: 채점 로직 1차
  • Loading branch information
LSBsity authored Aug 5, 2024
2 parents 30e15b3 + 759df68 commit 4f30365
Show file tree
Hide file tree
Showing 12 changed files with 198 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/main/java/team5/doghae/domain/question/Tag.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package team5.doghae.domain.question;

public enum Tag {
GRAMMAR, EAZY, NORMAL, HARD
GRAMMAR, EASY, NORMAL, HARD
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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();
}
}
}
Original file line number Diff line number Diff line change
@@ -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<Review, Long> {
}
Original file line number Diff line number Diff line change
@@ -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;


}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<Tag, Integer> 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 +
'}';
}
}
20 changes: 20 additions & 0 deletions src/main/java/team5/doghae/domain/stage/dto/StageRequest.java
Original file line number Diff line number Diff line change
@@ -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<QuestionRequest.Evaluate> answers;

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import lombok.NoArgsConstructor;
import team5.doghae.domain.stage.domain.Stage;


public class StageResponse {

@Getter
Expand All @@ -27,4 +28,12 @@ public static Create of(Stage stage) {
}

}

// @Getter
// @NoArgsConstructor
// @AllArgsConstructor
// public static class Evaluate {
//
//
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
import org.springframework.data.jpa.repository.JpaRepository;
import team5.doghae.domain.stage.domain.Stage;


public interface StageRepository extends JpaRepository<Stage, Long> {
}
74 changes: 56 additions & 18 deletions src/main/java/team5/doghae/domain/stage/service/StageService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,36 +25,69 @@ public class StageService {
private final StageRepository stageRepository;
private final UserStageRepository userStageRepository;


public List<QuestionResponse.Create> 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<QuestionResponse.Create> 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<QuestionResponse.Evaluate> 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<Question> questions = stage.getQuestions()
.stream()
.sorted(Comparator.comparing(Question::getId))
.toList();

List<QuestionRequest.Evaluate> answers = evaluateRequest.getAnswers()
.stream()
.sorted(Comparator.comparing(QuestionRequest.Evaluate::getQuestionId))
.toList();

if (questions.size() != answers.size()) {
throw new IllegalArgumentException("요청한 문제 정보가 올바르지 않습니다.");
}

Map<Tag, Integer> map = new HashMap<>();

return questions;
}
List<QuestionResponse.Evaluate> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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();
}
}

0 comments on commit 4f30365

Please sign in to comment.