QuestionController.java 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package nju.seec.helper.controller;
  2. import nju.seec.helper.api.bok.dto.CollectDTO;
  3. import nju.seec.helper.api.bok.dto.RateDTO;
  4. import nju.seec.helper.api.bok.vo.InteractiveVO;
  5. import nju.seec.helper.aspect.auth.Auth;
  6. import nju.seec.helper.aspect.auth.LoginUser;
  7. import nju.seec.helper.controller.response.PageResponse;
  8. import nju.seec.helper.dto.groups.Create;
  9. import nju.seec.helper.dto.groups.Modify;
  10. import nju.seec.helper.dto.question.BaseQuestionDTO;
  11. import nju.seec.helper.enums.UserRole;
  12. import nju.seec.helper.service.QuestionService;
  13. import nju.seec.helper.vo.question.BaseQuestionVO;
  14. import org.springframework.data.domain.Pageable;
  15. import org.springframework.data.web.PageableDefault;
  16. import org.springframework.validation.annotation.Validated;
  17. import org.springframework.web.bind.annotation.*;
  18. import java.util.List;
  19. import java.util.Set;
  20. /**
  21. * 题库
  22. *
  23. * @author xst
  24. * <p>
  25. * updated by cst
  26. */
  27. @RestController
  28. @RequestMapping("/api/question")
  29. public class QuestionController {
  30. private final QuestionService questionService;
  31. public QuestionController(QuestionService questionService) {
  32. this.questionService = questionService;
  33. }
  34. /**
  35. * 获得题目列表
  36. */
  37. @Auth(roles = {UserRole.TEACHER}, message = "获得题目列表")
  38. @GetMapping
  39. public PageResponse<BaseQuestionVO> getQuestionList(LoginUser user,
  40. @RequestParam(required = false, defaultValue = "") String stem,
  41. @RequestParam(required = false, defaultValue = "") String tag,
  42. @RequestParam(required = false, defaultValue = "") String knowledgeId,
  43. @PageableDefault(Integer.MAX_VALUE) Pageable pageable) {
  44. return PageResponse.of(questionService.getQuestions(user, stem, tag, knowledgeId, pageable));
  45. }
  46. /**
  47. * 获取某一测试题目
  48. */
  49. @Auth(roles = {UserRole.TEACHER, UserRole.STUDENT}, message = "获得测试题目")
  50. @GetMapping("/quiz/{quizId}")
  51. public List<BaseQuestionVO> getQuestionsByQuiz(LoginUser user, @PathVariable Long quizId) {
  52. return questionService.getQuestionsByQuiz(user, quizId);
  53. }
  54. /**
  55. * 获取某一题目信息
  56. */
  57. @Auth(roles = {UserRole.TEACHER}, message = "获取题目信息")
  58. @GetMapping("/{questionId}")
  59. public BaseQuestionVO getOneQuestion(LoginUser user, @PathVariable String questionId) {
  60. return questionService.getOneQuestion(user, questionId);
  61. }
  62. /**
  63. * 创建题目
  64. */
  65. @Auth(roles = UserRole.TEACHER, message = "创建题目")
  66. @PostMapping
  67. public BaseQuestionVO createQuestion(LoginUser user, @Validated(Create.class) @RequestBody BaseQuestionDTO baseQuestionDTO) {
  68. return questionService.createQuestion(user, baseQuestionDTO);
  69. }
  70. /**
  71. * 修改题目
  72. */
  73. @Auth(roles = UserRole.TEACHER, message = "修改题目")
  74. @PutMapping("/{questionId}")
  75. public BaseQuestionVO modifyQuestion(LoginUser user, @PathVariable String questionId, @Validated(Modify.class) @RequestBody BaseQuestionDTO baseQuestionDTO) {
  76. return questionService.modifyQuestion(user, questionId, baseQuestionDTO);
  77. }
  78. /**
  79. * 删除题目
  80. */
  81. @Auth(roles = UserRole.TEACHER, message = "删除题目")
  82. @DeleteMapping("/{questionId}")
  83. public void deleteQuestion(LoginUser user, @PathVariable String questionId) {
  84. questionService.deleteQuestion(user, questionId);
  85. }
  86. /**
  87. * 获取创建题目
  88. */
  89. @Auth(roles = UserRole.TEACHER, message = "获取创建的题目")
  90. @GetMapping("/created")
  91. public PageResponse<BaseQuestionVO> getCreatedQuestions(LoginUser user, @PageableDefault(size = Integer.MAX_VALUE) Pageable pageable) {
  92. return PageResponse.of(questionService.getCreatedQuestions(user, pageable));
  93. }
  94. /**
  95. * 获取创建题目ID
  96. */
  97. @Auth(roles = UserRole.TEACHER)
  98. @GetMapping("/created/ids")
  99. public Set<String> getCreatedQuestionIds(LoginUser user) {
  100. return questionService.getIdsOfCreatedQuestions(user);
  101. }
  102. /**
  103. * 收藏题目
  104. */
  105. @Auth(roles = {UserRole.TEACHER, UserRole.STUDENT}, message = "收藏题目")
  106. @PostMapping("/{questionId}/collect")
  107. public void starQuestion(LoginUser user, @PathVariable String questionId, @Validated @RequestBody CollectDTO collectDTO) {
  108. questionService.collectQuestion(user, questionId, collectDTO);
  109. }
  110. /**
  111. * 评价题目
  112. */
  113. @Auth(roles = {UserRole.TEACHER, UserRole.STUDENT}, message = "评价题目")
  114. @PostMapping("/{questionId}/rate")
  115. public void rateQuestion(LoginUser user, @PathVariable String questionId, @Validated @RequestBody RateDTO rateDTO) {
  116. questionService.rateQuestion(user, questionId, rateDTO);
  117. }
  118. /**
  119. * 取得收藏评价的题目
  120. */
  121. @Auth(roles = {UserRole.TEACHER, UserRole.STUDENT}, message = "取得收藏评价的题目")
  122. @GetMapping("/collected")
  123. public PageResponse<BaseQuestionVO> getCollectedQuestions(LoginUser user, @PageableDefault(size = Integer.MAX_VALUE) Pageable pageable) {
  124. return PageResponse.of(questionService.getCollectedAndRatedQuestions(user, true, null, pageable));
  125. }
  126. @Auth(roles = {UserRole.TEACHER, UserRole.STUDENT}, message = "取得收藏评价的题目")
  127. @GetMapping("/rated")
  128. public PageResponse<BaseQuestionVO> getRatedQuestions(LoginUser user, @PageableDefault(size = Integer.MAX_VALUE) Pageable pageable) {
  129. return PageResponse.of(questionService.getCollectedAndRatedQuestions(user, null, true, pageable));
  130. }
  131. /**
  132. * 取得完成的题目
  133. */
  134. @Auth(roles = UserRole.STUDENT, message = "取得完成的题目")
  135. @GetMapping("/completed")
  136. public PageResponse<BaseQuestionVO> getCompletedQuestions(LoginUser user, @PageableDefault(size = Integer.MAX_VALUE) Pageable pageable) {
  137. return PageResponse.of(questionService.getCompletedQuestions(user, pageable));
  138. }
  139. /**
  140. * 取得与题目的交互信息
  141. */
  142. @Auth(roles = {UserRole.TEACHER, UserRole.STUDENT}, message = "取得与题目的交互信息")
  143. @GetMapping("/interactive")
  144. public List<InteractiveVO> getInteractiveList(LoginUser user, String questionId) {
  145. return questionService.getInteractiveList(user, questionId);
  146. }
  147. }