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.ExceptionType; import nju.seec.helper.util.enums.QuizState; import nju.seec.helper.util.enums.SlideState; import nju.seec.helper.util.enums.UserType; import nju.seec.helper.util.exception.HelperException; import nju.seec.helper.vo.CourseVO; import nju.seec.helper.vo.SlideVO; import nju.seec.helper.vo.quiz.ChoiceQuestionVO; 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; import org.springframework.web.bind.annotation.*; import java.util.*; import static nju.seec.helper.controller.QuizController.objectMapper; /** * 题库 * * @doc http://47.100.18.120:3000/SEEC-BOK/API-DOC/src/master/helper/api/Quiz-%e6%b5%8b%e8%af%95.md */ @RestController @RequestMapping("/api/question") public class QuestionController { @Autowired private CourseDAO courseDAO; @Autowired private SlideDAO slideDAO; @Autowired private QuestionService questionService; /** * 获得题目列表 */ @Auth(roles = {UserType.TEACHER}, message = "获得题目列表") @GetMapping("") public Map getQuestionList(LoginUser user, String stem,@PageableDefault(Integer.MAX_VALUE) Pageable pageable) throws JsonProcessingException { final Page page = questionService.getQuestionList(user, stem, pageable); Map ret=new LinkedHashMap(2); ret.put("questions",page.getContent()); ret.put("page",PageResponse.PageInfo.of(page.getNumber()+1, page.getSize(), page.getTotalPages(), Long.valueOf(page.getTotalElements()).intValue())); return ret; } /** * 获得题目列表 */ @Auth(roles = {UserType.TEACHER}, message = "获得题目详情") @GetMapping("/{questionId}") public QuestionVO getQuestion(LoginUser user,@PathVariable("questionId") String questionId) throws JsonProcessingException { return questionService.getQuestion(questionId); } /** * 创建题目 */ @Auth(roles = {UserType.TEACHER}, message = "创建题目") @PostMapping("") public QuestionVO PostQuestion(LoginUser user, @RequestBody Map question) { if(question.get("kind")==null){throw HelperException.of(ExceptionType.PARAM_ERROR,"no kind");} String kind = (String)question.get("kind"); QuestionVO newQuestion=null; switch (kind){ case "CHOICE": newQuestion = (objectMapper.convertValue(question, ChoiceQuestionVO.class)); return questionService.newQuestion(newQuestion); case "TRUE_FALSE": newQuestion = (objectMapper.convertValue(question, TrueOrFalseQuestionVO.class)); return questionService.newQuestion(newQuestion); } throw new ParseException('1',"parse failed:"+question.toString()); } /** * 更新题目 */ @Auth(roles = {UserType.TEACHER}, message = "更新题目") @PutMapping("/{questionId}") public QuestionVO updateQuestion(LoginUser user, @RequestBody Map question, @PathVariable("questionId") String questionId) { if(question.get("kind")==null){throw new ParseException('1',"no kind");} String kind = (String)question.get("kind"); QuestionVO newQuestion=null; switch (kind){ case "CHOICE": newQuestion= (objectMapper.convertValue(question, ChoiceQuestionVO.class)); return questionService.updateQuestion(newQuestion); case "TRUE_FALSE": newQuestion = (objectMapper.convertValue(question, TrueOrFalseQuestionVO.class)); return questionService.updateQuestion(newQuestion); } throw new ParseException('1',"parse failed:"+question.toString()); } /** * 删除题目 */ @Auth(roles = {UserType.TEACHER}, message = "删除题目") @DeleteMapping("/{questionId}") public Map deleteQuestion(LoginUser user, @PathVariable("questionId") String questionId) { questionService.deleteQuestion(questionId); Map map=new HashMap(); map.put("message","success"); return map; } }