Browse Source

修改判题接口,实现做题记录保存

UsagiHao 5 years ago
parent
commit
bbd2a112bf

+ 2 - 1
judgement-client/src/main/java/cn/seecoder/judgement/JudgementApi.java

@@ -1,9 +1,10 @@
 package cn.seecoder.judgement;
 
+import cn.seecoder.judgement.dto.StudentAnswerDTO;
 import cn.seecoder.judgement.vo.JudgedAnswersVO;
 
 import java.util.List;
 
 public interface JudgementApi {
-    JudgedAnswersVO submitQuestionStudentAnswer(List<String> questionsIds, List<Object> answers) throws JudgementException;
+    JudgedAnswersVO submitQuestionStudentAnswer(StudentAnswerDTO studentAnswerDTO) throws JudgementException;
 }

+ 2 - 10
judgement-client/src/main/java/cn/seecoder/judgement/JudgementClient.java

@@ -65,20 +65,12 @@ public class JudgementClient implements JudgementApi {
 
 
     @Override
-    public JudgedAnswersVO submitQuestionStudentAnswer(List<String> questionsIds, List<Object> answers) throws JudgementException{
+    public JudgedAnswersVO submitQuestionStudentAnswer(StudentAnswerDTO studentAnswerDTO) throws JudgementException{
         try {
             ObjectMapper objectMapper = new ObjectMapper();
             Request.Builder builder = new Request.Builder()
                     .addHeader("Authorization", token);
-            Map<String, StudentAnswerDTO.AnswerRecordDTO> records = new HashMap<>();
-            for (int i = 0; i < questionsIds.size(); i++){
-                StudentAnswerDTO.AnswerRecordDTO answerRecordDTO = new StudentAnswerDTO.AnswerRecordDTO();
-                answerRecordDTO.setAnswer(answers.get(i));
-                records.put(questionsIds.get(i), answerRecordDTO);
-            }
-            StudentAnswerDTO studentAnswerDTO=StudentAnswerDTO.builder()
-                    .records(records)
-                    .build();
+
             builder = builder
                     .url(host + "/api/judgement/submit")
                     .post(RequestBody.create(MediaType.parse("application/json; charset=utf-8"), objectMapper.writeValueAsString(studentAnswerDTO)));

+ 10 - 0
judgement-common/src/main/java/cn/seecoder/judgement/dto/StudentAnswerDTO.java

@@ -23,6 +23,8 @@ public class StudentAnswerDTO implements Serializable {
     private static final long serialVersionUID = -5846899734313166305L;
 //    @NotEmpty(message = "回答不能为空")
 //    private Map<String, Object> answers;
+    @NotNull(message = "用户id不能为空")
+    String userId;
 
     @NotNull(message = "缺少做题记录")
     @Valid
@@ -32,5 +34,13 @@ public class StudentAnswerDTO implements Serializable {
     public static class AnswerRecordDTO {
         @NotNull(message = "缺少答案")
         private Object answer;
+
+        @NotNull(message = "缺少做题开始时间")
+        @Past(message = "开始时间错误")
+        private LocalDateTime startAt;
+
+        @NotNull(message = "缺少做题结束时间")
+        @Past(message = "结束时间错误")
+        private LocalDateTime endAt;
     }
 }

+ 2 - 0
judgement-common/src/main/java/cn/seecoder/judgement/vo/question/BaseQuestionVO.java

@@ -32,6 +32,8 @@ public abstract class BaseQuestionVO {
                 return new ChoiceQuestionVO(questionVO, withAnswer);
             case true_false:
                 return new TrueOrFalseQuestionVO(questionVO, withAnswer);
+            case blank:
+                return new BlankQuestionVO(questionVO, withAnswer);
 //            case multiple_choice:
 //                return new MultipleChoiceQuestionVO(questionVO, withAnswer);
             default:

+ 31 - 0
judgement-common/src/main/java/cn/seecoder/judgement/vo/question/BlankQuestionVO.java

@@ -0,0 +1,31 @@
+package cn.seecoder.judgement.vo.question;
+
+import cn.seecoder.bok.common.vo.QuestionVO;
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+import lombok.NonNull;
+
+/**
+ * @author xst
+ * <p>
+ * updated by cst
+ */
+@EqualsAndHashCode(callSuper = true)
+@Data
+public class BlankQuestionVO extends BaseQuestionVO {
+    private String answer;
+
+    public BlankQuestionVO(@NonNull QuestionVO questionVO, boolean withAnswer) {
+        super(questionVO);
+
+        if (withAnswer) {
+            this.answer = questionVO.getAnswer();
+            this.analysis = questionVO.getAnalysis();
+        }
+    }
+
+    @Override
+    public boolean pass(Object answer) {
+        return this.answer.equals(String.valueOf(answer));
+    }
+}

+ 22 - 0
judgement-server/src/main/java/cn/seecoder/judgement/service/impl/JudgementServiceImpl.java

@@ -1,5 +1,7 @@
 package cn.seecoder.judgement.service.impl;
 
+import cn.seecoder.bok.client.SeecoderBokClient;
+import cn.seecoder.bok.common.vo.RecordVO;
 import cn.seecoder.judgement.dto.StudentAnswerDTO;
 import cn.seecoder.judgement.service.JudgementService;
 import cn.seecoder.judgement.service.QuestionService;
@@ -26,6 +28,8 @@ public class JudgementServiceImpl implements JudgementService {
         System.out.println(new Date());
         System.out.println("start judging, students'answers are:");
         System.out.println(studentAnswerDTO);
+
+        String userId = studentAnswerDTO.getUserId();
         Map<String, StudentAnswerDTO.AnswerRecordDTO> records = studentAnswerDTO.getRecords();
         List<String> questionIds = new ArrayList<>(records.keySet());
         List<BaseQuestionVO> questionVOList = questionService.getQuestionsByIds(questionIds);
@@ -34,6 +38,8 @@ public class JudgementServiceImpl implements JudgementService {
 
         AtomicInteger correct = new AtomicInteger();
 
+        List<RecordVO> judgedAnswerRecords = new ArrayList<>();
+
         Map<String, JudgedAnswersVO.QuestionStudentAnswerInfo> judgedAnswers = new HashMap<>();
         questionVOList.forEach(baseQuestionVO -> {
             String questionId = baseQuestionVO.getId();
@@ -43,8 +49,24 @@ public class JudgementServiceImpl implements JudgementService {
                 correct.getAndIncrement();
             }
             judgedAnswers.put(questionId, JudgedAnswersVO.QuestionStudentAnswerInfo.of(questionId, answer, isTrue));
+
+            //保存做题数据
+            StudentAnswerDTO.AnswerRecordDTO answerRecordDTO = records.get(questionId);
+            RecordVO record = new RecordVO();
+            record.setUserId(userId);
+            record.setQuestionId(questionId);
+            record.setPass(isTrue);
+            record.setStudentAnswer(String.valueOf(answer));
+            record.setStartAt(answerRecordDTO.getStartAt());
+            record.setFinishAt(answerRecordDTO.getEndAt());
+
+            judgedAnswerRecords.add(record);
         });
         JudgedAnswersVO res = new JudgedAnswersVO(judgedAnswers);
+
+        SeecoderBokClient seecoderBokClient = new SeecoderBokClient("http://bok.seecoder.cn", 0);
+        List<RecordVO> recordVOS = seecoderBokClient.createUserRecord(judgedAnswerRecords);
+
         System.out.println("results are as follows:");
         System.out.println(res);
         return res;

+ 1 - 0
judgement-server/src/main/java/cn/seecoder/judgement/service/impl/QuestionServiceImpl.java

@@ -1,6 +1,7 @@
 package cn.seecoder.judgement.service.impl;
 
 import cn.seecoder.bok.client.SeecoderBokClient;
+import cn.seecoder.bok.common.vo.RecordVO;
 import cn.seecoder.judgement.service.QuestionService;
 import cn.seecoder.judgement.vo.question.BaseQuestionVO;
 import org.springframework.beans.factory.annotation.Autowired;

+ 1 - 0
pom.xml

@@ -57,6 +57,7 @@
         <dependency>
             <groupId>com.squareup.okhttp3</groupId>
             <artifactId>okhttp</artifactId>
+            <version>4.8.0</version>
         </dependency>
 
         <dependency>