|
@@ -1,19 +1,21 @@
|
|
|
package com.example.onlinejudge.controller;
|
|
|
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
-import com.example.onlinejudge.common.BaseResponse;
|
|
|
+
|
|
|
import com.example.onlinejudge.common.DeleteRequest;
|
|
|
-import com.example.onlinejudge.common.ErrorCode;
|
|
|
-import com.example.onlinejudge.common.ResultUtils;
|
|
|
-import com.example.onlinejudge.exception.BusinessException;
|
|
|
-import com.example.onlinejudge.exception.ThrowUtils;
|
|
|
+
|
|
|
+
|
|
|
import com.example.onlinejudge.model.VO.QuestionVO;
|
|
|
import com.example.onlinejudge.model.dto.question.*;
|
|
|
import com.example.onlinejudge.model.entity.Question;
|
|
|
-import com.example.onlinejudge.model.entity.User;
|
|
|
+import com.example.onlinejudge.model.entity.result.Result;
|
|
|
+import com.example.onlinejudge.model.entity.result.ResultCode;
|
|
|
import com.example.onlinejudge.service.QuestionService;
|
|
|
import com.example.onlinejudge.service.UserService;
|
|
|
import com.google.gson.Gson;
|
|
|
+import io.swagger.annotations.ApiImplicitParam;
|
|
|
+import io.swagger.annotations.ApiImplicitParams;
|
|
|
+import io.swagger.annotations.ApiOperation;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
@@ -38,16 +40,16 @@ public class QuestionController {
|
|
|
// region 增删改查
|
|
|
|
|
|
/**
|
|
|
- * 创建
|
|
|
+ * 创建题目
|
|
|
*
|
|
|
* @param questionAddRequest
|
|
|
* @param request
|
|
|
* @return
|
|
|
*/
|
|
|
@PostMapping("/add")
|
|
|
- public BaseResponse<Long> addQuestion(@RequestBody QuestionAddRequest questionAddRequest, HttpServletRequest request) {
|
|
|
+ public Result<Long> addQuestion(@RequestBody QuestionAddRequest questionAddRequest, HttpServletRequest request) {
|
|
|
if (questionAddRequest == null) {
|
|
|
- throw new BusinessException(ErrorCode.PARAMS_ERROR);
|
|
|
+ return Result.error(ResultCode.PARAM_IS_BLANK);
|
|
|
}
|
|
|
Question question = new Question();
|
|
|
BeanUtils.copyProperties(questionAddRequest, question);
|
|
@@ -64,12 +66,16 @@ public class QuestionController {
|
|
|
question.setJudgeConfig(GSON.toJson(judgeConfig));
|
|
|
}
|
|
|
questionService.validQuestion(question, true);
|
|
|
+ //TODO:记录谁创建了这个题目
|
|
|
// User loginUser = userService.getLoginUser(request);
|
|
|
// question.setUserId(loginUser.getId());
|
|
|
boolean result = questionService.save(question);
|
|
|
- ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
|
|
|
+ if(!result){
|
|
|
+ return Result.error(ResultCode.PARAM_IS_INVALID);
|
|
|
+ }
|
|
|
+// ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
|
|
|
long newQuestionId = question.getId();
|
|
|
- return ResultUtils.success(newQuestionId);
|
|
|
+ return Result.success(newQuestionId);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -80,20 +86,24 @@ public class QuestionController {
|
|
|
* @return
|
|
|
*/
|
|
|
@PostMapping("/delete")
|
|
|
- public BaseResponse<Boolean> deleteQuestion(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
|
|
|
+ public Result<Boolean> deleteQuestion(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
|
|
|
if (deleteRequest == null || deleteRequest.getId() <= 0) {
|
|
|
- throw new BusinessException(ErrorCode.PARAMS_ERROR);
|
|
|
+ return Result.error(ResultCode.PARAM_IS_BLANK);
|
|
|
}
|
|
|
long id = deleteRequest.getId();
|
|
|
// 判断是否存在
|
|
|
Question oldQuestion = questionService.getById(id);
|
|
|
- ThrowUtils.throwIf(oldQuestion == null, ErrorCode.NOT_FOUND_ERROR);
|
|
|
+ if(oldQuestion == null){
|
|
|
+ return Result.error(ResultCode.PARAM_IS_INVALID);
|
|
|
+ }
|
|
|
+ //ThrowUtils.throwIf(oldQuestion == null, ErrorCode.NOT_FOUND_ERROR);
|
|
|
// 仅本人或管理员可删除
|
|
|
+ //TODO:判断是否是管理员
|
|
|
if (false) {
|
|
|
- throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
|
|
|
+ return Result.error(ResultCode.PERMISSION_DENIED);
|
|
|
}
|
|
|
boolean b = questionService.removeById(id);
|
|
|
- return ResultUtils.success(b);
|
|
|
+ return Result.success(b);
|
|
|
}
|
|
|
|
|
|
|
|
@@ -104,98 +114,94 @@ public class QuestionController {
|
|
|
* @return
|
|
|
*/
|
|
|
@GetMapping("/get")
|
|
|
- public BaseResponse<Question> getQuestionById(long id, HttpServletRequest request) {
|
|
|
+ public Result<Question> getQuestionById(long id, HttpServletRequest request) {
|
|
|
if (id <= 0) {
|
|
|
- throw new BusinessException(ErrorCode.PARAMS_ERROR);
|
|
|
+ return Result.error(ResultCode.PARAM_IS_INVALID);
|
|
|
}
|
|
|
Question question = questionService.getById(id);
|
|
|
if (question == null) {
|
|
|
- throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
|
|
|
+ return Result.error(ResultCode.NOT_FOUND_ERROR);
|
|
|
}
|
|
|
+ //TODO:判断是否是管理员
|
|
|
// User loginUser = userService.getLoginUser(request);
|
|
|
// // 不是本人或管理员,不能直接获取所有信息
|
|
|
// if (!question.getUserId().equals(loginUser.getId()) && !userService.isAdmin(loginUser)) {
|
|
|
// throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
|
|
|
// }
|
|
|
- return ResultUtils.success(question);
|
|
|
+ return Result.success(question);
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 根据 id 获取(脱敏)
|
|
|
- *
|
|
|
- * @param id
|
|
|
- * @return
|
|
|
- */
|
|
|
+ @ApiOperation(value = "根据id获取题目(封装类)")
|
|
|
+ @ApiImplicitParams({
|
|
|
+ @ApiImplicitParam(name = "id", value = "题目id", required = true, dataType = "long"),
|
|
|
+ })
|
|
|
@GetMapping("/get/vo")
|
|
|
- public BaseResponse<QuestionVO> getQuestionVOById(long id, HttpServletRequest request) {
|
|
|
+ public Result<QuestionVO> getQuestionVOById(long id, HttpServletRequest request) {
|
|
|
if (id <= 0) {
|
|
|
- throw new BusinessException(ErrorCode.PARAMS_ERROR);
|
|
|
+ return Result.error(ResultCode.PARAM_IS_INVALID);
|
|
|
}
|
|
|
Question question = questionService.getById(id);
|
|
|
if (question == null) {
|
|
|
- throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
|
|
|
+ return Result.error(ResultCode.NOT_FOUND_ERROR);
|
|
|
}
|
|
|
- return ResultUtils.success(questionService.getQuestionVO(question, request));
|
|
|
+ return Result.success(questionService.getQuestionVO(question, request));
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 分页获取列表(封装类)
|
|
|
- *
|
|
|
- * @param questionQueryRequest
|
|
|
- * @param request
|
|
|
- * @return
|
|
|
- */
|
|
|
+
|
|
|
+ @ApiOperation(value = "分页获取题目列表(封装类)")
|
|
|
+ @ApiImplicitParams(
|
|
|
+ @ApiImplicitParam(name = "questionQueryRequest", value = "查询条件", required = true, dataType = "QuestionQueryRequest")
|
|
|
+ )
|
|
|
@PostMapping("/list/page/vo")
|
|
|
- public BaseResponse<Page<QuestionVO>> listQuestionVOByPage(@RequestBody QuestionQueryRequest questionQueryRequest,
|
|
|
+ public Result<Page<QuestionVO>> listQuestionVOByPage(@RequestBody QuestionQueryRequest questionQueryRequest,
|
|
|
HttpServletRequest request) {
|
|
|
long current = questionQueryRequest.getCurrent();
|
|
|
long size = questionQueryRequest.getPageSize();
|
|
|
// 限制爬虫
|
|
|
- ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
|
|
|
+ if(size > 20){
|
|
|
+ return Result.error(ResultCode.PARAM_IS_INVALID);
|
|
|
+ }
|
|
|
+ //ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
|
|
|
Page<Question> questionPage = questionService.page(new Page<>(current, size),
|
|
|
questionService.getQueryWrapper(questionQueryRequest));
|
|
|
- return ResultUtils.success(questionService.getQuestionVOPage(questionPage, request));
|
|
|
+ return Result.success(questionService.getQuestionVOPage(questionPage, request));
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 分页获取当前用户创建的资源列表
|
|
|
- *
|
|
|
- * @param questionQueryRequest
|
|
|
- * @param request
|
|
|
- * @return
|
|
|
- */
|
|
|
+ @ApiOperation(value = "分页获取当前用户创建的题目列表(封装类)")
|
|
|
+ @ApiImplicitParams(
|
|
|
+ @ApiImplicitParam(name = "questionQueryRequest", value = "查询条件", required = true, dataType = "QuestionQueryRequest")
|
|
|
+ )
|
|
|
@PostMapping("/my/list/page/vo")
|
|
|
- public BaseResponse<Page<QuestionVO>> listMyQuestionVOByPage(@RequestBody QuestionQueryRequest questionQueryRequest,
|
|
|
+ public Result<Page<QuestionVO>> listMyQuestionVOByPage(@RequestBody QuestionQueryRequest questionQueryRequest,
|
|
|
HttpServletRequest request) {
|
|
|
if (questionQueryRequest == null) {
|
|
|
- throw new BusinessException(ErrorCode.PARAMS_ERROR);
|
|
|
+ return Result.error(ResultCode.PARAM_IS_BLANK);
|
|
|
}
|
|
|
// User loginUser = userService.getLoginUser(request);
|
|
|
// questionQueryRequest.setUserId(loginUser.getId());
|
|
|
long current = questionQueryRequest.getCurrent();
|
|
|
long size = questionQueryRequest.getPageSize();
|
|
|
// 限制爬虫
|
|
|
- ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
|
|
|
+ if(size > 20){
|
|
|
+ return Result.error(ResultCode.PARAM_IS_INVALID);
|
|
|
+ }
|
|
|
Page<Question> questionPage = questionService.page(new Page<>(current, size),
|
|
|
questionService.getQueryWrapper(questionQueryRequest));
|
|
|
- return ResultUtils.success(questionService.getQuestionVOPage(questionPage, request));
|
|
|
+ return Result.success(questionService.getQuestionVOPage(questionPage, request));
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 分页获取题目列表(仅管理员)
|
|
|
- *
|
|
|
- * @param questionQueryRequest
|
|
|
- * @param request
|
|
|
- * @return
|
|
|
- */
|
|
|
+ @ApiOperation(value = "分页获取题目列表(仅管理员)")
|
|
|
+ @ApiImplicitParams(
|
|
|
+ @ApiImplicitParam(name = "questionQueryRequest", value = "查询条件", required = true, dataType = "QuestionQueryRequest")
|
|
|
+ )
|
|
|
@PostMapping("/list/page")
|
|
|
- public BaseResponse<Page<Question>> listQuestionByPage(@RequestBody QuestionQueryRequest questionQueryRequest,
|
|
|
+ public Result<Page<Question>> listQuestionByPage(@RequestBody QuestionQueryRequest questionQueryRequest,
|
|
|
HttpServletRequest request) {
|
|
|
long current = questionQueryRequest.getCurrent();
|
|
|
long size = questionQueryRequest.getPageSize();
|
|
|
Page<Question> questionPage = questionService.page(new Page<>(current, size),
|
|
|
questionService.getQueryWrapper(questionQueryRequest));
|
|
|
- return ResultUtils.success(questionPage);
|
|
|
+ return Result.success(questionPage);
|
|
|
}
|
|
|
|
|
|
// endregion
|
|
@@ -207,38 +213,38 @@ public class QuestionController {
|
|
|
* @param request
|
|
|
* @return
|
|
|
*/
|
|
|
-// @PostMapping("/edit")
|
|
|
-// public BaseResponse<Boolean> editQuestion(@RequestBody QuestionEditRequest questionEditRequest, HttpServletRequest request) {
|
|
|
-// if (questionEditRequest == null || questionEditRequest.getId() <= 0) {
|
|
|
-// throw new BusinessException(ErrorCode.PARAMS_ERROR);
|
|
|
-// }
|
|
|
-// Question question = new Question();
|
|
|
-// BeanUtils.copyProperties(questionEditRequest, question);
|
|
|
-// List<String> tags = questionEditRequest.getTags();
|
|
|
-// if (tags != null) {
|
|
|
-// question.setTags(GSON.toJson(tags));
|
|
|
-// }
|
|
|
-// List<JudgeCase> judgeCase = questionEditRequest.getJudgeCase();
|
|
|
-// if (judgeCase != null) {
|
|
|
-// question.setJudgeCase(GSON.toJson(judgeCase));
|
|
|
-// }
|
|
|
-// JudgeConfig judgeConfig = questionEditRequest.getJudgeConfig();
|
|
|
-// if (judgeConfig != null) {
|
|
|
-// question.setJudgeConfig(GSON.toJson(judgeConfig));
|
|
|
+ @PostMapping("/edit")
|
|
|
+ public Result<Boolean> editQuestion(@RequestBody QuestionEditRequest questionEditRequest, HttpServletRequest request) {
|
|
|
+ if (questionEditRequest == null || questionEditRequest.getId() <= 0) {
|
|
|
+ return Result.error(ResultCode.PARAM_IS_BLANK);
|
|
|
+ }
|
|
|
+ Question question = new Question();
|
|
|
+ BeanUtils.copyProperties(questionEditRequest, question);
|
|
|
+ List<String> tags = questionEditRequest.getTags();
|
|
|
+ if (tags != null) {
|
|
|
+ question.setTags(GSON.toJson(tags));
|
|
|
+ }
|
|
|
+ List<JudgeCase> judgeCase = questionEditRequest.getJudgeCase();
|
|
|
+ if (judgeCase != null) {
|
|
|
+ question.setJudgeCase(GSON.toJson(judgeCase));
|
|
|
+ }
|
|
|
+ JudgeConfig judgeConfig = questionEditRequest.getJudgeConfig();
|
|
|
+ if (judgeConfig != null) {
|
|
|
+ question.setJudgeConfig(GSON.toJson(judgeConfig));
|
|
|
+ }
|
|
|
+ // 参数校验
|
|
|
+ questionService.validQuestion(question, false);
|
|
|
+// User loginUser = userService.getLoginUser(request);
|
|
|
+// long id = questionEditRequest.getId();
|
|
|
+ // 判断是否存在
|
|
|
+// Question oldQuestion = questionService.getById(id);
|
|
|
+// ThrowUtils.throwIf(oldQuestion == null, ErrorCode.NOT_FOUND_ERROR);
|
|
|
+ // 仅本人或管理员可编辑
|
|
|
+// if (!oldQuestion.getUserId().equals(loginUser.getId()) && !userService.isAdmin(loginUser)) {
|
|
|
+// throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
|
|
|
// }
|
|
|
-// // 参数校验
|
|
|
-// questionService.validQuestion(question, false);
|
|
|
-//// User loginUser = userService.getLoginUser(request);
|
|
|
-//// long id = questionEditRequest.getId();
|
|
|
-// // 判断是否存在
|
|
|
-//// Question oldQuestion = questionService.getById(id);
|
|
|
-//// ThrowUtils.throwIf(oldQuestion == null, ErrorCode.NOT_FOUND_ERROR);
|
|
|
-// // 仅本人或管理员可编辑
|
|
|
-//// if (!oldQuestion.getUserId().equals(loginUser.getId()) && !userService.isAdmin(loginUser)) {
|
|
|
-//// throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
|
|
|
-//// }
|
|
|
-// boolean result = questionService.updateById(question);
|
|
|
-// return ResultUtils.success(result);
|
|
|
-// }
|
|
|
+ boolean result = questionService.updateById(question);
|
|
|
+ return Result.success(result);
|
|
|
+ }
|
|
|
|
|
|
}
|