Просмотр исходного кода

feat: http://47.100.18.120:3000/SEEC-BOK/API-DOC/src/master/helper/api/Question-%e9%97%ae%e9%a2%98.md#%E8%8E%B7%E5%BE%97%E9%A2%98%E7%9B%AE%E5%88%97%E8%A1%A8

XuShengTao 6 лет назад
Родитель
Сommit
c5d1a3ea5c

+ 22 - 0
src/main/java/nju/seec/helper/config/PageableConfig.java

@@ -0,0 +1,22 @@
+package nju.seec.helper.config;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.data.web.config.PageableHandlerMethodArgumentResolverCustomizer;
+
+/**
+ * @ClassName PageableConfig
+ * @PackageName nju.seec.helper.config
+ * @Author SheentXu
+ * @Date 2019/12/24
+ * @Version 1.0
+ * @Description //TODO
+ **/
+@Configuration
+public class PageableConfig {
+
+    @Bean
+    PageableHandlerMethodArgumentResolverCustomizer pageableResolverCustomizer() {
+        return pageableResolver -> pageableResolver.setOneIndexedParameters(true);
+    }
+}

+ 11 - 7
src/main/java/nju/seec/helper/controller/QuestionController.java

@@ -1,10 +1,12 @@
 package nju.seec.helper.controller;
 
+import com.fasterxml.jackson.core.JsonProcessingException;
 import nju.seec.helper.aspect.auth.Auth;
 import nju.seec.helper.controller.response.PageResponse;
 import nju.seec.helper.dao.CourseDAO;
 import nju.seec.helper.dao.SlideDAO;
 import nju.seec.helper.dto.LoginUser;
+import nju.seec.helper.service.QuestionService;
 import nju.seec.helper.util.enums.QuizState;
 import nju.seec.helper.util.enums.SlideState;
 import nju.seec.helper.util.enums.UserType;
@@ -15,6 +17,7 @@ import nju.seec.helper.vo.quiz.QuestionVO;
 import nju.seec.helper.vo.quiz.QuizVO;
 import nju.seec.helper.vo.quiz.TrueOrFalseQuestionVO;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.domain.Page;
 import org.springframework.data.domain.Pageable;
 import org.springframework.data.web.PageableDefault;
 import org.springframework.expression.ParseException;
@@ -37,18 +40,19 @@ public class QuestionController {
     private CourseDAO courseDAO;
     @Autowired
     private SlideDAO slideDAO;
+    @Autowired
+    private QuestionService questionService;
     /**
      * 获得题目列表
      */
-    @Auth(roles = {UserType.TEACHER}, message = "获得题目列表")
+//    @Auth(roles = {UserType.TEACHER}, message = "获得题目列表")
     @GetMapping("")
-    public Map getQuestionList(LoginUser user, String stem,  @PageableDefault(Integer.MAX_VALUE) Pageable pageable) {
-        List<QuestionVO> Questions=new ArrayList<>();
-        Questions.add(ChoiceQ());
-        Questions.add(TFQ());
+    public Map getQuestionList(LoginUser user, String stem,@PageableDefault(Integer.MAX_VALUE) Pageable pageable) throws JsonProcessingException {
+
+        final Page<QuestionVO> questionList = questionService.getQuestionList(user, stem, pageable);
         Map ret=new LinkedHashMap(2);
-        ret.put("questions",Questions);
-        ret.put("page",PageResponse.PageInfo.of(1,5,1,1));
+        ret.put("questions",questionList.getContent());
+        ret.put("page",questionList.getPageable());
         return ret;
     }
 

+ 2 - 5
src/main/java/nju/seec/helper/controller/QuizController.java

@@ -9,8 +9,6 @@ import nju.seec.helper.dto.LoginUser;
 import nju.seec.helper.dto.QuizAnswerDTO;
 import nju.seec.helper.dto.QuizDTO;
 import nju.seec.helper.dto.QuizUpdateDTO;
-import nju.seec.helper.entity.User;
-import nju.seec.helper.util.Consts;
 import nju.seec.helper.util.enums.QuestionKindState;
 import nju.seec.helper.util.enums.QuizState;
 import nju.seec.helper.util.enums.SlideState;
@@ -26,7 +24,6 @@ import org.springframework.data.domain.Pageable;
 import org.springframework.data.web.PageableDefault;
 import org.springframework.web.bind.annotation.*;
 
-import javax.servlet.http.HttpSession;
 import java.util.*;
 
 /**
@@ -162,11 +159,11 @@ public class QuizController {
         map.put("B","This is B");
         map.put("C","This is C");
         map.put("D","This is D");
-        return ChoiceQuestionVO.builder().QuizId("1").studentAnswer("A").stem("这是一道测试选择题").name("t1")
+        return ChoiceQuestionVO.builder().QuestionId("1").studentAnswer("A").stem("这是一道测试选择题")
                 .pass(true).answer("A").analysis("这里是分析").kind(QuestionKindState.CHOICE).options(map).build();
     }
     static TrueOrFalseQuestionVO TFQ(){
-        return TrueOrFalseQuestionVO.builder().QuizId("2").studentAnswer(true).stem("这是一道测试判断题").name("t2")
+        return TrueOrFalseQuestionVO.builder().QuestionId("2").studentAnswer(true).stem("这是一道测试判断题")
                 .pass(true).answer(false).analysis("这里是分析").kind(QuestionKindState.TRUE_FALSE).build();
     }
 }

+ 12 - 0
src/main/java/nju/seec/helper/service/QuestionService.java

@@ -0,0 +1,12 @@
+package nju.seec.helper.service;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import nju.seec.helper.dto.LoginUser;
+import nju.seec.helper.vo.quiz.QuestionVO;
+import org.springframework.data.domain.Page;
+
+import java.awt.print.Pageable;
+
+public interface QuestionService {
+    Page<QuestionVO> getQuestionList(LoginUser user, String stem, org.springframework.data.domain.Pageable pageable) throws JsonProcessingException;
+}

+ 78 - 0
src/main/java/nju/seec/helper/service/impl/QuestionServiceImpl.java

@@ -0,0 +1,78 @@
+package nju.seec.helper.service.impl;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import nju.seec.helper.dto.LoginUser;
+import nju.seec.helper.service.QuestionService;
+import nju.seec.helper.util.JsonUtils;
+import nju.seec.helper.util.RestRequestUtil;
+import nju.seec.helper.util.enums.QuestionKindState;
+import nju.seec.helper.vo.quiz.ChoiceQuestionVO;
+import nju.seec.helper.vo.quiz.QuestionVO;
+import nju.seec.helper.vo.quiz.TrueOrFalseQuestionVO;
+import org.omg.CORBA.portable.UnknownException;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.PageImpl;
+import org.springframework.data.domain.Pageable;
+import org.springframework.stereotype.Service;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+@Service
+public class QuestionServiceImpl implements QuestionService {
+    @Autowired
+    private RestRequestUtil restRequestUtil;
+
+    @Override
+    public Page<QuestionVO> getQuestionList(LoginUser user, String stem, Pageable pageable) throws JsonProcessingException {
+        List<QuestionVO> Questions = new ArrayList<QuestionVO>();
+        Map page = new HashMap();
+        BokFindByStemLike(stem, Questions,page,pageable);
+        Page<QuestionVO> ret =new PageImpl<QuestionVO>(Questions,pageable,Long.valueOf((Integer)page.get("totalElements")));
+        return ret;
+    }
+    // 应该做成bok的库
+    private void BokFindByStemLike(String stem,List<QuestionVO> Questions, Map page,Pageable pageable) {
+
+        Map<String,String> urlParams=new HashMap<>();
+        urlParams.put("content",stem);
+        urlParams.put("sort",pageable.getSort().toString().replaceAll(" ","").replaceAll(":",","));
+        urlParams.put("size",String.valueOf(pageable.getPageSize()));
+        urlParams.put("page",String.valueOf(1+pageable.getPageNumber()));
+        try {
+            Map ret = (Map)restRequestUtil.sendPostGet(
+                    "http://bok.seecoder.cn/api/tq/search/findByStemLike?content={content}&sort={sort}&size={size}&page={page}", urlParams);
+
+            List<Map> tqs= ((List)((Map)ret.get("_embedded")).get("choiceQuestions"));
+
+            final List collect = tqs.stream().map(q -> {
+                switch ((String) q.get("type")) {
+                    case "choice":
+                        return ChoiceQuestionVO.builder().stem((String) q.get("stem")).kind(QuestionKindState.CHOICE)
+                                .options((Map<String, String>) q.get("options")).answer(((String) q.get("answer")).replaceAll("【答案】", ""))
+                                .analysis((String) q.get("analysis")).QuestionId(String.valueOf((Integer) q.get("tq_id"))).build();
+
+                    case "true_false":
+                        return TrueOrFalseQuestionVO.builder().stem((String) q.get("stem")).kind(QuestionKindState.CHOICE)
+                                .answer((Boolean) (q.get("answer")))
+                                .analysis((String) q.get("analysis")).QuestionId(String.valueOf((Integer) q.get("tq_id"))).build();
+
+                }
+                return new Exception("unknown type:" + (String) q.get("type"));
+            }).collect(Collectors.toList());
+            try {
+                page.putAll((Map)ret.get("page"));
+                Questions.addAll(collect);
+            }catch (Exception e){
+                e.printStackTrace();
+            }
+
+        }catch (Exception e){
+            e.printStackTrace();
+        }
+    }
+}

+ 7 - 1
src/main/java/nju/seec/helper/util/JsonUtils.java

@@ -1,7 +1,9 @@
 package nju.seec.helper.util;
 
+import com.fasterxml.jackson.databind.ObjectMapper;
 import com.google.gson.Gson;
 import lombok.experimental.UtilityClass;
+import org.springframework.stereotype.Component;
 
 /**
  * @author cst
@@ -11,8 +13,12 @@ public class JsonUtils {
     public String toJson(Object o) {
         return new Gson().toJson(o);
     }
-
+    private ObjectMapper objectMapper = new ObjectMapper();
     public <T> T fromJson(String json, Class<T> classOfT) {
         return new Gson().fromJson(json, classOfT);
     }
+
+    public ObjectMapper getJsonMapper() {
+        return objectMapper;
+    }
 }

+ 30 - 0
src/main/java/nju/seec/helper/util/RestRequestUtil.java

@@ -0,0 +1,30 @@
+package nju.seec.helper.util;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import org.springframework.http.*;
+import org.springframework.stereotype.Component;
+import org.springframework.web.client.RestTemplate;
+
+import java.util.List;
+import java.util.Map;
+@Component
+public class RestRequestUtil {
+
+    public List sendPostRequest(String url, Map<String, String> params) throws JsonProcessingException {
+        RestTemplate client = new RestTemplate();
+        HttpHeaders headers = new HttpHeaders();
+        HttpMethod method = HttpMethod.POST;
+        headers.setContentType(MediaType.APPLICATION_JSON);
+        HttpEntity<Map> requestEntity = new HttpEntity<>(params, headers);
+        ResponseEntity ret = client.postForEntity(url, requestEntity, List.class);
+        return (List) ret.getBody();
+    }
+    public Object sendPostGet(String url, Map<String, String> urlParams) throws JsonProcessingException {
+        RestTemplate client = new RestTemplate();
+        HttpHeaders headers = new HttpHeaders();
+        HttpMethod method = HttpMethod.GET;
+        headers.setContentType(MediaType.APPLICATION_JSON);
+        ResponseEntity ret = client.getForEntity(url,Object.class,urlParams);
+        return ret.getBody();
+    }
+}

+ 1 - 1
src/main/java/nju/seec/helper/vo/quiz/ChoiceQuestionVO.java

@@ -23,7 +23,7 @@ public class ChoiceQuestionVO extends QuestionVO{
     @JsonProperty(value = "studentAnswer")
     private String studentAnswer;
     @JsonProperty(value = "pass")
-    private boolean pass;
+    private Boolean pass;
 }
 //kind: "CHOICE";
 //  options: {

+ 1 - 3
src/main/java/nju/seec/helper/vo/quiz/QuestionVO.java

@@ -18,9 +18,7 @@ import org.springframework.beans.BeanUtils;
 @SuperBuilder
 public abstract class QuestionVO {
     @JsonProperty(value = "id")
-    private String QuizId;
-    @JsonProperty(value = "name")
-    private String name;
+    private String QuestionId;
     @JsonProperty(value = "kind")
     private QuestionKindState kind;
     @JsonProperty(value = "stem")

+ 3 - 3
src/main/java/nju/seec/helper/vo/quiz/TrueOrFalseQuestionVO.java

@@ -15,11 +15,11 @@ import java.util.Map;
 @NoArgsConstructor
 public class TrueOrFalseQuestionVO extends QuestionVO{
     @JsonProperty(value = "answer")
-    private boolean answer;
+    private Boolean answer;
     @JsonProperty(value = "analysis")
     private String analysis;
     @JsonProperty(value = "studentAnswer")
-    private boolean studentAnswer;
+    private Boolean studentAnswer;
     @JsonProperty(value = "pass")
-    private boolean pass;
+    private Boolean pass;
 }