QuestionController.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package nju.seec.helper.controller;
  2. import com.fasterxml.jackson.core.JsonProcessingException;
  3. import nju.seec.helper.aspect.auth.Auth;
  4. import nju.seec.helper.controller.response.PageResponse;
  5. import nju.seec.helper.dao.CourseDAO;
  6. import nju.seec.helper.dao.SlideDAO;
  7. import nju.seec.helper.dto.LoginUser;
  8. import nju.seec.helper.service.QuestionService;
  9. import nju.seec.helper.util.enums.ExceptionType;
  10. import nju.seec.helper.util.enums.QuizState;
  11. import nju.seec.helper.util.enums.SlideState;
  12. import nju.seec.helper.util.enums.UserType;
  13. import nju.seec.helper.util.exception.HelperException;
  14. import nju.seec.helper.vo.CourseVO;
  15. import nju.seec.helper.vo.SlideVO;
  16. import nju.seec.helper.vo.quiz.ChoiceQuestionVO;
  17. import nju.seec.helper.vo.quiz.QuestionVO;
  18. import nju.seec.helper.vo.quiz.QuizVO;
  19. import nju.seec.helper.vo.quiz.TrueOrFalseQuestionVO;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.data.domain.Page;
  22. import org.springframework.data.domain.Pageable;
  23. import org.springframework.data.web.PageableDefault;
  24. import org.springframework.expression.ParseException;
  25. import org.springframework.web.bind.annotation.*;
  26. import java.util.*;
  27. import static nju.seec.helper.controller.QuizController.objectMapper;
  28. /**
  29. * 题库
  30. *
  31. * @doc http://47.100.18.120:3000/SEEC-BOK/API-DOC/src/master/helper/api/Quiz-%e6%b5%8b%e8%af%95.md
  32. */
  33. @RestController
  34. @RequestMapping("/api/question")
  35. public class QuestionController {
  36. @Autowired
  37. private CourseDAO courseDAO;
  38. @Autowired
  39. private SlideDAO slideDAO;
  40. @Autowired
  41. private QuestionService questionService;
  42. /**
  43. * 获得题目列表
  44. */
  45. @Auth(roles = {UserType.TEACHER}, message = "获得题目列表")
  46. @GetMapping("")
  47. public Map getQuestionList(LoginUser user, String stem,@PageableDefault(Integer.MAX_VALUE) Pageable pageable) throws JsonProcessingException {
  48. final Page<QuestionVO> page = questionService.getQuestionList(user, stem, pageable);
  49. Map ret=new LinkedHashMap(2);
  50. ret.put("questions",page.getContent());
  51. ret.put("page",PageResponse.PageInfo.of(page.getNumber()+1, page.getSize(), page.getTotalPages(), Long.valueOf(page.getTotalElements()).intValue()));
  52. return ret;
  53. }
  54. /**
  55. * 获得题目列表
  56. */
  57. @Auth(roles = {UserType.TEACHER}, message = "获得题目详情")
  58. @GetMapping("/{questionId}")
  59. public QuestionVO getQuestion(LoginUser user,@PathVariable("questionId") String questionId) throws JsonProcessingException {
  60. return questionService.getQuestion(questionId);
  61. }
  62. /**
  63. * 创建题目
  64. */
  65. @Auth(roles = {UserType.TEACHER}, message = "创建题目")
  66. @PostMapping("")
  67. public QuestionVO PostQuestion(LoginUser user, @RequestBody Map question) {
  68. if(question.get("kind")==null){throw HelperException.of(ExceptionType.PARAM_ERROR,"no kind");}
  69. String kind = (String)question.get("kind");
  70. QuestionVO newQuestion=null;
  71. switch (kind){
  72. case "CHOICE":
  73. newQuestion = (objectMapper.convertValue(question, ChoiceQuestionVO.class));
  74. return questionService.newQuestion(newQuestion);
  75. case "TRUE_FALSE":
  76. newQuestion = (objectMapper.convertValue(question, TrueOrFalseQuestionVO.class));
  77. return questionService.newQuestion(newQuestion);
  78. }
  79. throw new ParseException('1',"parse failed:"+question.toString());
  80. }
  81. /**
  82. * 更新题目
  83. */
  84. @Auth(roles = {UserType.TEACHER}, message = "更新题目")
  85. @PutMapping("/{questionId}")
  86. public QuestionVO updateQuestion(LoginUser user, @RequestBody Map question, @PathVariable("questionId") String questionId) {
  87. if(question.get("kind")==null){throw new ParseException('1',"no kind");}
  88. String kind = (String)question.get("kind");
  89. QuestionVO newQuestion=null;
  90. switch (kind){
  91. case "CHOICE":
  92. newQuestion= (objectMapper.convertValue(question, ChoiceQuestionVO.class));
  93. return questionService.updateQuestion(newQuestion);
  94. case "TRUE_FALSE":
  95. newQuestion = (objectMapper.convertValue(question, TrueOrFalseQuestionVO.class));
  96. return questionService.updateQuestion(newQuestion);
  97. }
  98. throw new ParseException('1',"parse failed:"+question.toString());
  99. }
  100. /**
  101. * 删除题目
  102. */
  103. @Auth(roles = {UserType.TEACHER}, message = "删除题目")
  104. @DeleteMapping("/{questionId}")
  105. public Map deleteQuestion(LoginUser user, @PathVariable("questionId") String questionId) {
  106. questionService.deleteQuestion(questionId);
  107. Map map=new HashMap();
  108. map.put("message","success");
  109. return map;
  110. }
  111. }