|
|
@@ -1,117 +0,0 @@
|
|
|
-package nju.seec.helper.api.bok;
|
|
|
-
|
|
|
-import com.google.common.collect.ImmutableMap;
|
|
|
-import lombok.Data;
|
|
|
-import nju.seec.helper.api.bok.constant.QuestionType;
|
|
|
-import nju.seec.helper.api.bok.vo.PageableQuestionListVO;
|
|
|
-import nju.seec.helper.api.bok.vo.QuestionVO;
|
|
|
-import nju.seec.helper.dto.question.BaseQuestionDTO;
|
|
|
-import nju.seec.helper.dto.question.ChoiceQuestionDTO;
|
|
|
-import nju.seec.helper.dto.question.TrueOrFalseQuestionDTO;
|
|
|
-import nju.seec.helper.enums.ExceptionType;
|
|
|
-import nju.seec.helper.exception.HelperException;
|
|
|
-import nju.seec.helper.util.RestRequestUtil;
|
|
|
-import nju.seec.helper.util.StringUtils;
|
|
|
-import nju.seec.helper.util.cache.CaffeineCacheUtils;
|
|
|
-import org.springframework.beans.factory.annotation.Value;
|
|
|
-import org.springframework.boot.context.properties.ConfigurationProperties;
|
|
|
-import org.springframework.data.domain.Page;
|
|
|
-import org.springframework.data.domain.PageImpl;
|
|
|
-import org.springframework.data.domain.Pageable;
|
|
|
-import org.springframework.stereotype.Component;
|
|
|
-
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.stream.Collectors;
|
|
|
-
|
|
|
-/**
|
|
|
- * @author xst
|
|
|
- * <p>
|
|
|
- * updated by cst
|
|
|
- */
|
|
|
-@Component
|
|
|
-@ConfigurationProperties("bok.question")
|
|
|
-@Data
|
|
|
-public class QuestionApi {
|
|
|
- @Value("${bok.question-cache-name}")
|
|
|
- private String bokQuestionCacheName;
|
|
|
-
|
|
|
- private final RestRequestUtil restRequestUtil;
|
|
|
- private final CaffeineCacheUtils cacheUtils;
|
|
|
- private String url;
|
|
|
- private String searchUrl;
|
|
|
-
|
|
|
- public QuestionApi(RestRequestUtil restRequestUtil, CaffeineCacheUtils cacheUtils) {
|
|
|
- this.restRequestUtil = restRequestUtil;
|
|
|
- this.cacheUtils = cacheUtils;
|
|
|
- }
|
|
|
-
|
|
|
- public Page<QuestionVO> getQuestionList(String stem, String tag, String knowledgeId, Pageable pageable) {
|
|
|
- Map<String, ?> urlParams = ImmutableMap.<String, Object>builder()
|
|
|
- .put("stem", stem)
|
|
|
- .put("tag", tag)
|
|
|
- .put("knowledgeId", knowledgeId)
|
|
|
- .put("page", 1 + pageable.getPageNumber())
|
|
|
- .put("size", pageable.getPageSize())
|
|
|
- .put("sort", StringUtils.getSortString(pageable.getSort()))
|
|
|
- .build();
|
|
|
- PageableQuestionListVO result = restRequestUtil.sendGetRequest(searchUrl, PageableQuestionListVO.class, urlParams);
|
|
|
-
|
|
|
- List<QuestionVO> questionVOList = result.getQuestions();
|
|
|
- cacheUtils.setAll(bokQuestionCacheName, questionVOList.parallelStream().collect(Collectors.toMap(QuestionVO::getId, bokQuestion -> bokQuestion)));
|
|
|
- return new PageImpl<>(questionVOList, pageable, result.getPage().getTotalElements());
|
|
|
- }
|
|
|
-
|
|
|
- public QuestionVO getQuestionsById(String questionId) {
|
|
|
- Object cachedBokQuestion = cacheUtils.get(bokQuestionCacheName, questionId);
|
|
|
- if (cachedBokQuestion instanceof QuestionVO) {
|
|
|
- return (QuestionVO) cachedBokQuestion;
|
|
|
- }
|
|
|
- QuestionVO questionVO = restRequestUtil.sendGetRequest(url + "/" + questionId, QuestionVO.class);
|
|
|
- cacheUtils.set(bokQuestionCacheName, questionVO.getId(), questionVO);
|
|
|
- return questionVO;
|
|
|
- }
|
|
|
-
|
|
|
- public QuestionVO createQuestion(String questionId, BaseQuestionDTO baseQuestionDTO) {
|
|
|
- QuestionVO questionVO = createQuestionVO(baseQuestionDTO);
|
|
|
- questionVO.setId(questionId);
|
|
|
- questionVO = restRequestUtil.sendPostRequest(url, questionVO, QuestionVO.class);
|
|
|
- return questionVO;
|
|
|
- }
|
|
|
-
|
|
|
- public QuestionVO updateQuestion(String questionId, BaseQuestionDTO baseQuestionDTO) {
|
|
|
- QuestionVO questionVO = createQuestionVO(baseQuestionDTO);
|
|
|
- questionVO.setId(questionId);
|
|
|
- restRequestUtil.sendPutRequest(url + "/" + questionId, questionVO);
|
|
|
- return getQuestionsById(questionId);
|
|
|
- }
|
|
|
-
|
|
|
- public void deleteQuestion(String questionId) {
|
|
|
- restRequestUtil.sendDeleteRequest(url + "/" + questionId);
|
|
|
- }
|
|
|
-
|
|
|
- private QuestionVO createQuestionVO(BaseQuestionDTO baseQuestionDTO) {
|
|
|
- QuestionVO questionVO = new QuestionVO();
|
|
|
- questionVO.setType(QuestionType.valueOf(baseQuestionDTO.getType()));
|
|
|
- questionVO.setStem(baseQuestionDTO.getStem());
|
|
|
- questionVO.setKeyPoints(baseQuestionDTO.getKeyPoints());
|
|
|
- questionVO.setTags(baseQuestionDTO.getTags());
|
|
|
- questionVO.setKnowledgeId(baseQuestionDTO.getKnowledgeId());
|
|
|
- questionVO.setAnalysis(baseQuestionDTO.getAnalysis());
|
|
|
-
|
|
|
- switch (questionVO.getType()) {
|
|
|
- case choice:
|
|
|
- ChoiceQuestionDTO choiceQuestionDTO = (ChoiceQuestionDTO) baseQuestionDTO;
|
|
|
- questionVO.setOptions(choiceQuestionDTO.getOptions());
|
|
|
- questionVO.setAnswer(choiceQuestionDTO.getAnswer());
|
|
|
- break;
|
|
|
- case true_false:
|
|
|
- TrueOrFalseQuestionDTO trueOrFalseQuestionDTO = (TrueOrFalseQuestionDTO) baseQuestionDTO;
|
|
|
- questionVO.setAnswer(String.valueOf(trueOrFalseQuestionDTO.getAnswer()));
|
|
|
- break;
|
|
|
- default:
|
|
|
- throw HelperException.of(ExceptionType.ERROR, "不支持的题目类型");
|
|
|
- }
|
|
|
- return questionVO;
|
|
|
- }
|
|
|
-}
|