QuestionController.java 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. package com.example.onlinejudge.controller;
  2. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  3. import com.example.onlinejudge.common.BaseResponse;
  4. import com.example.onlinejudge.common.DeleteRequest;
  5. import com.example.onlinejudge.common.ErrorCode;
  6. import com.example.onlinejudge.common.ResultUtils;
  7. import com.example.onlinejudge.exception.BusinessException;
  8. import com.example.onlinejudge.exception.ThrowUtils;
  9. import com.example.onlinejudge.model.VO.QuestionVO;
  10. import com.example.onlinejudge.model.dto.question.*;
  11. import com.example.onlinejudge.model.entity.Question;
  12. import com.example.onlinejudge.model.entity.User;
  13. import com.example.onlinejudge.service.QuestionService;
  14. import com.example.onlinejudge.service.UserService;
  15. import com.google.gson.Gson;
  16. import lombok.extern.slf4j.Slf4j;
  17. import org.springframework.beans.BeanUtils;
  18. import org.springframework.web.bind.annotation.*;
  19. import javax.annotation.Resource;
  20. import javax.servlet.http.HttpServletRequest;
  21. import java.util.List;
  22. @RestController
  23. @RequestMapping("/question")
  24. @Slf4j
  25. public class QuestionController {
  26. @Resource
  27. private QuestionService questionService;
  28. @Resource
  29. private UserService userService;
  30. private final static Gson GSON = new Gson();
  31. // region 增删改查
  32. /**
  33. * 创建
  34. *
  35. * @param questionAddRequest
  36. * @param request
  37. * @return
  38. */
  39. @PostMapping("/add")
  40. public BaseResponse<Long> addQuestion(@RequestBody QuestionAddRequest questionAddRequest, HttpServletRequest request) {
  41. if (questionAddRequest == null) {
  42. throw new BusinessException(ErrorCode.PARAMS_ERROR);
  43. }
  44. Question question = new Question();
  45. BeanUtils.copyProperties(questionAddRequest, question);
  46. List<String> tags = questionAddRequest.getTags();
  47. if (tags != null) {
  48. question.setTags(GSON.toJson(tags));
  49. }
  50. List<JudgeCase> judgeCase = questionAddRequest.getJudgeCase();
  51. if (judgeCase != null) {
  52. question.setJudgeCase(GSON.toJson(judgeCase));
  53. }
  54. JudgeConfig judgeConfig = questionAddRequest.getJudgeConfig();
  55. if (judgeConfig != null) {
  56. question.setJudgeConfig(GSON.toJson(judgeConfig));
  57. }
  58. questionService.validQuestion(question, true);
  59. // User loginUser = userService.getLoginUser(request);
  60. // question.setUserId(loginUser.getId());
  61. boolean result = questionService.save(question);
  62. ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
  63. long newQuestionId = question.getId();
  64. return ResultUtils.success(newQuestionId);
  65. }
  66. /**
  67. * 删除
  68. *
  69. * @param deleteRequest
  70. * @param request
  71. * @return
  72. */
  73. @PostMapping("/delete")
  74. public BaseResponse<Boolean> deleteQuestion(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
  75. if (deleteRequest == null || deleteRequest.getId() <= 0) {
  76. throw new BusinessException(ErrorCode.PARAMS_ERROR);
  77. }
  78. long id = deleteRequest.getId();
  79. // 判断是否存在
  80. Question oldQuestion = questionService.getById(id);
  81. ThrowUtils.throwIf(oldQuestion == null, ErrorCode.NOT_FOUND_ERROR);
  82. // 仅本人或管理员可删除
  83. if (false) {
  84. throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
  85. }
  86. boolean b = questionService.removeById(id);
  87. return ResultUtils.success(b);
  88. }
  89. /**
  90. * 根据 id 获取
  91. *
  92. * @param id
  93. * @return
  94. */
  95. @GetMapping("/get")
  96. public BaseResponse<Question> getQuestionById(long id, HttpServletRequest request) {
  97. if (id <= 0) {
  98. throw new BusinessException(ErrorCode.PARAMS_ERROR);
  99. }
  100. Question question = questionService.getById(id);
  101. if (question == null) {
  102. throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
  103. }
  104. // User loginUser = userService.getLoginUser(request);
  105. // // 不是本人或管理员,不能直接获取所有信息
  106. // if (!question.getUserId().equals(loginUser.getId()) && !userService.isAdmin(loginUser)) {
  107. // throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
  108. // }
  109. return ResultUtils.success(question);
  110. }
  111. /**
  112. * 根据 id 获取(脱敏)
  113. *
  114. * @param id
  115. * @return
  116. */
  117. @GetMapping("/get/vo")
  118. public BaseResponse<QuestionVO> getQuestionVOById(long id, HttpServletRequest request) {
  119. if (id <= 0) {
  120. throw new BusinessException(ErrorCode.PARAMS_ERROR);
  121. }
  122. Question question = questionService.getById(id);
  123. if (question == null) {
  124. throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
  125. }
  126. return ResultUtils.success(questionService.getQuestionVO(question, request));
  127. }
  128. /**
  129. * 分页获取列表(封装类)
  130. *
  131. * @param questionQueryRequest
  132. * @param request
  133. * @return
  134. */
  135. @PostMapping("/list/page/vo")
  136. public BaseResponse<Page<QuestionVO>> listQuestionVOByPage(@RequestBody QuestionQueryRequest questionQueryRequest,
  137. HttpServletRequest request) {
  138. long current = questionQueryRequest.getCurrent();
  139. long size = questionQueryRequest.getPageSize();
  140. // 限制爬虫
  141. ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
  142. Page<Question> questionPage = questionService.page(new Page<>(current, size),
  143. questionService.getQueryWrapper(questionQueryRequest));
  144. return ResultUtils.success(questionService.getQuestionVOPage(questionPage, request));
  145. }
  146. /**
  147. * 分页获取当前用户创建的资源列表
  148. *
  149. * @param questionQueryRequest
  150. * @param request
  151. * @return
  152. */
  153. @PostMapping("/my/list/page/vo")
  154. public BaseResponse<Page<QuestionVO>> listMyQuestionVOByPage(@RequestBody QuestionQueryRequest questionQueryRequest,
  155. HttpServletRequest request) {
  156. if (questionQueryRequest == null) {
  157. throw new BusinessException(ErrorCode.PARAMS_ERROR);
  158. }
  159. // User loginUser = userService.getLoginUser(request);
  160. // questionQueryRequest.setUserId(loginUser.getId());
  161. long current = questionQueryRequest.getCurrent();
  162. long size = questionQueryRequest.getPageSize();
  163. // 限制爬虫
  164. ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
  165. Page<Question> questionPage = questionService.page(new Page<>(current, size),
  166. questionService.getQueryWrapper(questionQueryRequest));
  167. return ResultUtils.success(questionService.getQuestionVOPage(questionPage, request));
  168. }
  169. /**
  170. * 分页获取题目列表(仅管理员)
  171. *
  172. * @param questionQueryRequest
  173. * @param request
  174. * @return
  175. */
  176. @PostMapping("/list/page")
  177. public BaseResponse<Page<Question>> listQuestionByPage(@RequestBody QuestionQueryRequest questionQueryRequest,
  178. HttpServletRequest request) {
  179. long current = questionQueryRequest.getCurrent();
  180. long size = questionQueryRequest.getPageSize();
  181. Page<Question> questionPage = questionService.page(new Page<>(current, size),
  182. questionService.getQueryWrapper(questionQueryRequest));
  183. return ResultUtils.success(questionPage);
  184. }
  185. // endregion
  186. /**
  187. * 编辑(用户)
  188. *
  189. * @param questionEditRequest
  190. * @param request
  191. * @return
  192. */
  193. // @PostMapping("/edit")
  194. // public BaseResponse<Boolean> editQuestion(@RequestBody QuestionEditRequest questionEditRequest, HttpServletRequest request) {
  195. // if (questionEditRequest == null || questionEditRequest.getId() <= 0) {
  196. // throw new BusinessException(ErrorCode.PARAMS_ERROR);
  197. // }
  198. // Question question = new Question();
  199. // BeanUtils.copyProperties(questionEditRequest, question);
  200. // List<String> tags = questionEditRequest.getTags();
  201. // if (tags != null) {
  202. // question.setTags(GSON.toJson(tags));
  203. // }
  204. // List<JudgeCase> judgeCase = questionEditRequest.getJudgeCase();
  205. // if (judgeCase != null) {
  206. // question.setJudgeCase(GSON.toJson(judgeCase));
  207. // }
  208. // JudgeConfig judgeConfig = questionEditRequest.getJudgeConfig();
  209. // if (judgeConfig != null) {
  210. // question.setJudgeConfig(GSON.toJson(judgeConfig));
  211. // }
  212. // // 参数校验
  213. // questionService.validQuestion(question, false);
  214. //// User loginUser = userService.getLoginUser(request);
  215. //// long id = questionEditRequest.getId();
  216. // // 判断是否存在
  217. //// Question oldQuestion = questionService.getById(id);
  218. //// ThrowUtils.throwIf(oldQuestion == null, ErrorCode.NOT_FOUND_ERROR);
  219. // // 仅本人或管理员可编辑
  220. //// if (!oldQuestion.getUserId().equals(loginUser.getId()) && !userService.isAdmin(loginUser)) {
  221. //// throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
  222. //// }
  223. // boolean result = questionService.updateById(question);
  224. // return ResultUtils.success(result);
  225. // }
  226. }