123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- 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.service.QuestionService;
- import com.example.onlinejudge.service.UserService;
- import com.google.gson.Gson;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.beans.BeanUtils;
- import org.springframework.web.bind.annotation.*;
- import javax.annotation.Resource;
- import javax.servlet.http.HttpServletRequest;
- import java.util.List;
- @RestController
- @RequestMapping("/question")
- @Slf4j
- public class QuestionController {
- @Resource
- private QuestionService questionService;
- @Resource
- private UserService userService;
- private final static Gson GSON = new Gson();
-
-
- @PostMapping("/add")
- public BaseResponse<Long> addQuestion(@RequestBody QuestionAddRequest questionAddRequest, HttpServletRequest request) {
- if (questionAddRequest == null) {
- throw new BusinessException(ErrorCode.PARAMS_ERROR);
- }
- Question question = new Question();
- BeanUtils.copyProperties(questionAddRequest, question);
- List<String> tags = questionAddRequest.getTags();
- if (tags != null) {
- question.setTags(GSON.toJson(tags));
- }
- List<JudgeCase> judgeCase = questionAddRequest.getJudgeCase();
- if (judgeCase != null) {
- question.setJudgeCase(GSON.toJson(judgeCase));
- }
- JudgeConfig judgeConfig = questionAddRequest.getJudgeConfig();
- if (judgeConfig != null) {
- question.setJudgeConfig(GSON.toJson(judgeConfig));
- }
- questionService.validQuestion(question, true);
- boolean result = questionService.save(question);
- ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
- long newQuestionId = question.getId();
- return ResultUtils.success(newQuestionId);
- }
-
- @PostMapping("/delete")
- public BaseResponse<Boolean> deleteQuestion(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
- if (deleteRequest == null || deleteRequest.getId() <= 0) {
- throw new BusinessException(ErrorCode.PARAMS_ERROR);
- }
- long id = deleteRequest.getId();
-
- Question oldQuestion = questionService.getById(id);
- ThrowUtils.throwIf(oldQuestion == null, ErrorCode.NOT_FOUND_ERROR);
-
- if (false) {
- throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
- }
- boolean b = questionService.removeById(id);
- return ResultUtils.success(b);
- }
-
- @GetMapping("/get")
- public BaseResponse<Question> getQuestionById(long id, HttpServletRequest request) {
- if (id <= 0) {
- throw new BusinessException(ErrorCode.PARAMS_ERROR);
- }
- Question question = questionService.getById(id);
- if (question == null) {
- throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
- }
- return ResultUtils.success(question);
- }
-
- @GetMapping("/get/vo")
- public BaseResponse<QuestionVO> getQuestionVOById(long id, HttpServletRequest request) {
- if (id <= 0) {
- throw new BusinessException(ErrorCode.PARAMS_ERROR);
- }
- Question question = questionService.getById(id);
- if (question == null) {
- throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
- }
- return ResultUtils.success(questionService.getQuestionVO(question, request));
- }
-
- @PostMapping("/list/page/vo")
- public BaseResponse<Page<QuestionVO>> listQuestionVOByPage(@RequestBody QuestionQueryRequest questionQueryRequest,
- HttpServletRequest request) {
- long current = questionQueryRequest.getCurrent();
- long size = questionQueryRequest.getPageSize();
-
- 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));
- }
-
- @PostMapping("/my/list/page/vo")
- public BaseResponse<Page<QuestionVO>> listMyQuestionVOByPage(@RequestBody QuestionQueryRequest questionQueryRequest,
- HttpServletRequest request) {
- if (questionQueryRequest == null) {
- throw new BusinessException(ErrorCode.PARAMS_ERROR);
- }
- long current = questionQueryRequest.getCurrent();
- long size = questionQueryRequest.getPageSize();
-
- 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));
- }
-
- @PostMapping("/list/page")
- public BaseResponse<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);
- }
-
-
- }
|