当前位置: 首页 > wzjs >正文

辽宁住房城乡建设部官方网站哈尔滨网站建设

辽宁住房城乡建设部官方网站,哈尔滨网站建设,北京品牌网站建设公司哪家好,做公众号关注网站《深入剖析题目接口控制器:功能、实现与应用》 一、引言 在在线编程平台或竞赛系统中,题目管理和提交是核心功能之一。QuestionController 类作为控制器层,承担着处理与题目相关的各种请求的重要职责,包括题目的增删改查、题目提…

《深入剖析题目接口控制器:功能、实现与应用》

一、引言

在在线编程平台或竞赛系统中,题目管理和提交是核心功能之一。QuestionController 类作为控制器层,承担着处理与题目相关的各种请求的重要职责,包括题目的增删改查、题目提交等操作。本文将详细剖析该类的代码,深入理解其功能和实现细节。

二、类概述

QuestionController 类使用 Spring 的 @RestController 注解,表明它是一个 RESTful 风格的控制器,用于处理 HTTP 请求。它位于 / 根路径下,提供了一系列与题目相关的接口。该类依赖于 QuestionServiceUserFeignClientQuestionSubmitService 等服务,通过这些服务来完成具体的业务逻辑。

@RestController
@RequestMapping("/")
@Slf4j
public class QuestionController {@Resourceprivate QuestionService questionService;@Resourceprivate UserFeignClient userFeignClient;@Resourceprivate QuestionSubmitService questionSubmitService;private final static Gson GSON = new Gson();// ... 具体方法 ...
}

三、增删改查功能实现

3.1 创建题目

@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);User loginUser = userFeignClient.getLoginUser(request);question.setUserId(loginUser.getId());question.setFavourNum(0);question.setThumbNum(0);boolean result = questionService.save(question);ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);long newQuestionId = question.getId();return ResultUtils.success(newQuestionId);
}
  • 功能:处理创建题目的请求,将用户提交的题目信息保存到数据库中。
  • 实现步骤
    1. 检查请求参数是否为空,若为空则抛出参数错误异常。
    2. QuestionAddRequest 对象的属性复制到 Question 对象中。
    3. 对题目标签、测试用例和判题配置进行 JSON 序列化处理。
    4. 调用 questionServicevalidQuestion 方法对题目信息进行校验。
    5. 获取当前登录用户的信息,并将用户 ID 关联到题目中。
    6. 初始化题目点赞数和收藏数为 0。
    7. 调用 questionServicesave 方法保存题目信息,若保存失败则抛出操作错误异常。
    8. 返回新创建题目的 ID。

3.2 删除题目

@PostMapping("/delete")
public BaseResponse<Boolean> deleteQuestion(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {if (deleteRequest == null || deleteRequest.getId() <= 0) {throw new BusinessException(ErrorCode.PARAMS_ERROR);}User user = userFeignClient.getLoginUser(request);long id = deleteRequest.getId();// 判断是否存在Question oldQuestion = questionService.getById(id);ThrowUtils.throwIf(oldQuestion == null, ErrorCode.NOT_FOUND_ERROR);// 仅本人或管理员可删除if (!oldQuestion.getUserId().equals(user.getId()) && !userFeignClient.isAdmin(user)) {throw new BusinessException(ErrorCode.NO_AUTH_ERROR);}boolean b = questionService.removeById(id);return ResultUtils.success(b);
}
  • 功能:处理删除题目的请求,从数据库中删除指定 ID 的题目。
  • 实现步骤
    1. 检查请求参数是否合法,若不合法则抛出参数错误异常。
    2. 获取当前登录用户的信息。
    3. 根据题目 ID 查询题目信息,若题目不存在则抛出未找到错误异常。
    4. 检查当前用户是否有删除权限,只有题目创建者或管理员才能删除题目,若没有权限则抛出无权限错误异常。
    5. 调用 questionServiceremoveById 方法删除题目信息,并返回删除结果。

3.3 更新题目(仅管理员)

@PostMapping("/update")
@AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
public BaseResponse<Boolean> updateQuestion(@RequestBody QuestionUpdateRequest questionUpdateRequest) {if (questionUpdateRequest == null || questionUpdateRequest.getId() <= 0) {throw new BusinessException(ErrorCode.PARAMS_ERROR);}Question question = new Question();BeanUtils.copyProperties(questionUpdateRequest, question);List<String> tags = questionUpdateRequest.getTags();if (tags != null) {question.setTags(GSON.toJson(tags));}List<JudgeCase> judgeCase = questionUpdateRequest.getJudgeCase();if (judgeCase != null) {question.setJudgeCase(GSON.toJson(judgeCase));}JudgeConfig judgeConfig = questionUpdateRequest.getJudgeConfig();if (judgeConfig != null) {question.setJudgeConfig(GSON.toJson(judgeConfig));}// 参数校验questionService.validQuestion(question, false);long id = questionUpdateRequest.getId();// 判断是否存在Question oldQuestion = questionService.getById(id);ThrowUtils.throwIf(oldQuestion == null, ErrorCode.NOT_FOUND_ERROR);boolean result = questionService.updateById(question);return ResultUtils.success(result);
}
  • 功能:处理更新题目的请求,只有管理员才能执行此操作。
  • 实现步骤
    1. 检查请求参数是否合法,若不合法则抛出参数错误异常。
    2. QuestionUpdateRequest 对象的属性复制到 Question 对象中。
    3. 对题目标签、测试用例和判题配置进行 JSON 序列化处理。
    4. 调用 questionServicevalidQuestion 方法对题目信息进行校验。
    5. 根据题目 ID 查询题目信息,若题目不存在则抛出未找到错误异常。
    6. 调用 questionServiceupdateById 方法更新题目信息,并返回更新结果。

3.4 根据 ID 获取题目

@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);}User loginUser = userFeignClient.getLoginUser(request);// 不是本人或管理员,不能直接获取所有信息if (!question.getUserId().equals(loginUser.getId()) && !userFeignClient.isAdmin(loginUser)) {throw new BusinessException(ErrorCode.NO_AUTH_ERROR);}return ResultUtils.success(question);
}
  • 功能:根据题目 ID 获取题目信息,只有题目创建者或管理员才能获取完整信息。
  • 实现步骤
    1. 检查题目 ID 是否合法,若不合法则抛出参数错误异常。
    2. 根据题目 ID 查询题目信息,若题目不存在则抛出未找到错误异常。
    3. 获取当前登录用户的信息。
    4. 检查当前用户是否有获取完整信息的权限,若没有权限则抛出无权限错误异常。
    5. 返回题目信息。

3.5 根据 ID 获取脱敏后的题目信息

@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));
}
  • 功能:根据题目 ID 获取脱敏后的题目信息,适用于普通用户查看。
  • 实现步骤
    1. 检查题目 ID 是否合法,若不合法则抛出参数错误异常。
    2. 根据题目 ID 查询题目信息,若题目不存在则抛出未找到错误异常。
    3. 调用 questionServicegetQuestionVO 方法获取脱敏后的题目信息并返回。

3.6 分页获取题目列表(封装类)

@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));
}
  • 功能:分页获取题目列表,并返回封装后的题目信息(QuestionVO)。
  • 实现步骤
    1. 从请求参数中获取当前页码和每页数量。
    2. 检查每页数量是否超过限制,若超过则抛出参数错误异常,防止爬虫过度请求。
    3. 调用 questionServicepage 方法进行分页查询。
    4. 调用 questionServicegetQuestionVOPage 方法将查询结果封装为 QuestionVO 并返回。

3.7 分页获取当前用户创建的题目列表

@PostMapping("/my/list/page/vo")
public BaseResponse<Page<QuestionVO>> listMyQuestionVOByPage(@RequestBody QuestionQueryRequest questionQueryRequest,HttpServletRequest request) {if (questionQueryRequest == null) {throw new BusinessException(ErrorCode.PARAMS_ERROR);}User loginUser = userFeignClient.getLoginUser(request);questionQueryRequest.setUserId(loginUser.getId());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));
}
  • 功能:分页获取当前用户创建的题目列表,并返回封装后的题目信息(QuestionVO)。
  • 实现步骤
    1. 检查请求参数是否为空,若为空则抛出参数错误异常。
    2. 获取当前登录用户的信息,并将用户 ID 设置到查询请求中。
    3. 从请求参数中获取当前页码和每页数量。
    4. 检查每页数量是否超过限制,若超过则抛出参数错误异常,防止爬虫过度请求。
    5. 调用 questionServicepage 方法进行分页查询。
    6. 调用 questionServicegetQuestionVOPage 方法将查询结果封装为 QuestionVO 并返回。

3.8 分页获取题目列表(仅管理员)

@PostMapping("/list/page")
@AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
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);
}
  • 功能:分页获取题目列表,只有管理员才能执行此操作。
  • 实现步骤
    1. 从请求参数中获取当前页码和每页数量。
    2. 调用 questionServicepage 方法进行分页查询。
    3. 返回查询结果。

四、其他功能实现

4.1 编辑题目(用户)

@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));}// 参数校验questionService.validQuestion(question, false);User loginUser = userFeignClient.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()) && !userFeignClient.isAdmin(loginUser)) {throw new BusinessException(ErrorCode.NO_AUTH_ERROR);}boolean result = questionService.updateById(question);return ResultUtils.success(result);
}
  • 功能:处理编辑题目的请求,只有题目创建者或管理员才能执行此操作。
  • 实现步骤
    1. 检查请求参数是否合法,若不合法则抛出参数错误异常。
    2. QuestionEditRequest 对象的属性复制到 Question 对象中。
    3. 对题目标签、测试用例和判题配置进行 JSON 序列化处理。
    4. 调用 questionServicevalidQuestion 方法对题目信息进行校验。
    5. 获取当前登录用户的信息。
    6. 根据题目 ID 查询题目信息,若题目不存在则抛出未找到错误异常。
    7. 检查当前用户是否有编辑权限,若没有权限则抛出无权限错误异常。
    8. 调用 questionServiceupdateById 方法更新题目信息,并返回更新结果。

4.2 提交题目

@PostMapping("/question_submit/do")
public BaseResponse<Long> doQuestionSubmit(@RequestBody QuestionSubmitAddRequest questionSubmitAddRequest,HttpServletRequest request) {if (questionSubmitAddRequest == null || questionSubmitAddRequest.getQuestionId() <= 0) {throw new BusinessException(ErrorCode.PARAMS_ERROR);}// 登录才能提交final User loginUser = userFeignClient.getLoginUser(request);long questionSubmitId = questionSubmitService.doQuestionSubmit(questionSubmitAddRequest, loginUser);return ResultUtils.success(questionSubmitId);
}
  • 功能:处理提交题目的请求,用户必须登录才能提交。
  • 实现步骤
    1. 检查请求参数是否合法,若不合法则抛出参数错误异常。
    2. 获取当前登录用户的信息。
    3. 调用 questionSubmitServicedoQuestionSubmit 方法处理题目提交,并返回提交记录的 ID。

4.3 分页获取题目提交列表

@PostMapping("/question_submit/list/page")
public BaseResponse<Page<QuestionSubmitVO>> listQuestionSubmitByPage(@RequestBody QuestionSubmitQueryRequest questionSubmitQueryRequest,HttpServletRequest request) {long current = questionSubmitQueryRequest.getCurrent();long size = questionSubmitQueryRequest.getPageSize();// 从数据库中查询原始的题目提交分页信息Page<QuestionSubmit> questionSubmitPage = questionSubmitService.page(new Page<>(current, size),questionSubmitService.getQueryWrapper(questionSubmitQueryRequest));final User loginUser = userFeignClient.getLoginUser(request);// 返回脱敏信息return ResultUtils.success(questionSubmitService.getQuestionSubmitVOPage(questionSubmitPage, loginUser));
}
  • 功能:分页获取题目提交列表
http://www.dtcms.com/wzjs/196079.html

相关文章:

  • 北京十大最靠谱it培训机构seo搜索引擎优化到底是什么
  • 想制作自己的网站对网站和网页的认识
  • 江门网站关键词推广排名nba
  • 网站海外推广哪家好web成品网站源码免费
  • 一个企业的网站建设网站宣传推广方案
  • wordpress 七牛oss北京seo学校
  • 厦门高端网站建设公司网店营销策划方案范文
  • java兼职网站开发谷歌搜索引擎入口2023
  • 临沂怎么做网站sem是什么意思呢
  • 做网站用啥软件好网络推广公司加盟
  • 教育公司网站建设方案苏州百度推广公司地址
  • 长春网站制作优势吉网传媒google 优化推广
  • thinkphp5 做网站网络推广的公司更可靠
  • 前端基础知识南安seo
  • 网站建设公司怎么做好企业网站优化价格
  • 广东省住房城乡建设厅门户网站在百度怎么创建自己的网站
  • 帮非法集资公司做网站违法吗广州私人做网站
  • 网站建设靠谱公司鹤壁网站推广公司
  • 网站备案跟域名有什么关系好搜搜索引擎
  • wordpress 复制插件上海seo优化外包公司
  • 电子商务网站模板html营销网站优化推广
  • 网站建设胶州家园免费优化网站排名
  • 喀什网站制作重庆网站网络推广
  • b2b网站建设步骤营销型网站建设推荐
  • 禾天姿网站开发河北seo基础知识
  • dedecms仿站域名注册阿里云
  • 自定义网站建设团队24小时免费看的视频哔哩哔哩
  • 免展网站后台注册快速排名刷
  • ecs怎么做多个网站上海网站营销推广
  • 专业定制网站在线seo关键词排名优化