Default Changelist

feature/qzw
秦泽旺 1 week ago
parent 1d1188a4c1
commit 885afb5fc0

@ -8,114 +8,130 @@ package lsgwr.exam.controller;
import lsgwr.exam.entity.Exam; import lsgwr.exam.entity.Exam;
import lsgwr.exam.entity.ExamRecord; import lsgwr.exam.entity.ExamRecord;
import lsgwr.exam.service.ExamService; import lsgwr.exam.service.ExamService;//导入考试的一些包
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;//Swagger注解
import lsgwr.exam.vo.*; import lsgwr.exam.vo.*;// 引入所有视图对象VO
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;// Bean 属性拷贝工具类
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;//添加注解
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;// HTTP 请求对象
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
// 控制器注解,表示这是一个 RESTful 控制器
@RestController @RestController
@Api(tags = "Exam APIs") @Api(tags = "Exam APIs")// 为 Swagger 文档定义标签用于分组API接口
@RequestMapping("/api/exam") @RequestMapping("/api/exam")// 定义基础路径为 /api/exam 的请求映射
public class ExamController { public class ExamController {
@Autowired @Autowired
private ExamService examService; private ExamService examService;// 自动注入 ExamService 服务
/**
*
*/
@GetMapping("/question/all") @GetMapping("/question/all")
@ApiOperation("获取所有问题的列表") @ApiOperation("获取所有问题的列表")
ResultVO<List<QuestionVo>> getQuestionAll() { ResultVO<List<QuestionVo>> getQuestionAll() {
ResultVO<List<QuestionVo>> resultVO; ResultVO<List<QuestionVo>> resultVO;
try { try {
List<QuestionVo> questionAll = examService.getQuestionAll(); List<QuestionVo> questionAll = examService.getQuestionAll();// 调用服务层获取所有问题
resultVO = new ResultVO<>(0, "获取全部问题列表成功", questionAll); resultVO = new ResultVO<>(0, "获取全部问题列表成功", questionAll);// 返回成功结果
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
resultVO = new ResultVO<>(-1, "获取全部问题列表失败", null); resultVO = new ResultVO<>(-1, "获取全部问题列表失败", null);// 返回失败结果
} }
return resultVO; return resultVO;
} }
/**
*
*/
@PostMapping("/question/update") @PostMapping("/question/update")
@ApiOperation("更新问题") @ApiOperation("更新问题")
ResultVO<QuestionVo> questionUpdate(@RequestBody QuestionVo questionVo) { ResultVO<QuestionVo> questionUpdate(@RequestBody QuestionVo questionVo) {
// 完成问题的更新 // 完成问题的更新
System.out.println(questionVo); System.out.println(questionVo);// 打印接收到的问题信息以供调试
try { try {
QuestionVo questionVoResult = examService.updateQuestion(questionVo); QuestionVo questionVoResult = examService.updateQuestion(questionVo);// 调用服务层更新问题
return new ResultVO<>(0, "更新问题成功", questionVoResult); return new ResultVO<>(0, "更新问题成功", questionVoResult);// 返回成功结果
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
return new ResultVO<>(-1, "更新问题失败", null); return new ResultVO<>(-1, "更新问题失败", null);// 返回失败结果
} }
} }
/**
*
*/
@PostMapping("/question/create") @PostMapping("/question/create")
@ApiOperation("创建问题") @ApiOperation("创建问题")
ResultVO<String> questionCreate(@RequestBody QuestionCreateSimplifyVo questionCreateSimplifyVo, HttpServletRequest request) { ResultVO<String> questionCreate(@RequestBody QuestionCreateSimplifyVo questionCreateSimplifyVo, HttpServletRequest request) {
QuestionCreateVo questionCreateVo = new QuestionCreateVo(); QuestionCreateVo questionCreateVo = new QuestionCreateVo();
// 把能拷贝过来的属性都拷贝过来 // 把能拷贝过来的属性都拷贝过来
BeanUtils.copyProperties(questionCreateSimplifyVo, questionCreateVo); BeanUtils.copyProperties(questionCreateSimplifyVo, questionCreateVo);// 拷贝属性到新的对象
// 设置创建者信息 // 设置创建者信息
String userId = (String) request.getAttribute("user_id"); String userId = (String) request.getAttribute("user_id");// 从请求中获取用户ID
questionCreateVo.setQuestionCreatorId(userId); questionCreateVo.setQuestionCreatorId(userId);// 设置创建者ID
System.out.println(questionCreateVo); System.out.println(questionCreateVo);
try { try {
examService.questionCreate(questionCreateVo); examService.questionCreate(questionCreateVo);// 调用服务层创建新问题
return new ResultVO<>(0, "问题创建成功", null); return new ResultVO<>(0, "问题创建成功", null);// 返回成功结果
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
return new ResultVO<>(-1, "创建问题失败", null); return new ResultVO<>(-1, "创建问题失败", null);// 返回失败结果
} }
} }
/**
*
*/
@GetMapping("/question/selection") @GetMapping("/question/selection")
@ApiOperation("获取问题分类的相关选项") @ApiOperation("获取问题分类的相关选项")
ResultVO<QuestionSelectionVo> getSelections() { ResultVO<QuestionSelectionVo> getSelections() {
QuestionSelectionVo questionSelectionVo = examService.getSelections(); QuestionSelectionVo questionSelectionVo = examService.getSelections();
if (questionSelectionVo != null) { if (questionSelectionVo != null) {
return new ResultVO<>(0, "获取问题分类选项成功", questionSelectionVo); return new ResultVO<>(0, "获取问题分类选项成功", questionSelectionVo);// 成功返回分类选项
} else { } else {
return new ResultVO<>(-1, "获取问题分类选项失败", null); return new ResultVO<>(-1, "获取问题分类选项失败", null);// 分类选项为空时返回失败信息
} }
} }
/**
* ID
*/
@GetMapping("/question/detail/{id}") @GetMapping("/question/detail/{id}")
@ApiOperation("根据问题的id获取问题的详细信息") @ApiOperation("根据问题的id获取问题的详细信息")
ResultVO<QuestionDetailVo> getQuestionDetail(@PathVariable String id) { ResultVO<QuestionDetailVo> getQuestionDetail(@PathVariable String id) {
// 根据问题id获取问题的详细信息 // 根据问题id获取问题的详细信息
System.out.println(id); System.out.println(id);// 打印出请求的问题ID便于调试和日志记录
ResultVO<QuestionDetailVo> resultVO; ResultVO<QuestionDetailVo> resultVO;
try { try {
QuestionDetailVo questionDetailVo = examService.getQuestionDetail(id); QuestionDetailVo questionDetailVo = examService.getQuestionDetail(id);
resultVO = new ResultVO<>(0, "获取问题详情成功", questionDetailVo); resultVO = new ResultVO<>(0, "获取问题详情成功", questionDetailVo);// 构造成功响应对象状态码为0表示成功
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();// 打印异常堆栈以供调试
resultVO = new ResultVO<>(-1, "获取问题详情失败", null); resultVO = new ResultVO<>(-1, "获取问题详情失败", null);// 构造失败响应对象,状态码为-1表示失败
} }
return resultVO; return resultVO;// 返回结果对象
} }
/**
*
*/
@GetMapping("/all") @GetMapping("/all")
@ApiOperation("获取全部考试的列表") @ApiOperation("获取全部考试的列表")
ResultVO<List<ExamVo>> getExamAll() { ResultVO<List<ExamVo>> getExamAll() {
// 需要拼接前端需要的考试列表对象 // 需要拼接前端需要的考试列表对象s
ResultVO<List<ExamVo>> resultVO; ResultVO<List<ExamVo>> resultVO;
try { try {
List<ExamVo> examVos = examService.getExamAll(); List<ExamVo> examVos = examService.getExamAll(); // 调用服务层获取所有考试信息
resultVO = new ResultVO<>(0, "获取全部考试的列表成功", examVos); resultVO = new ResultVO<>(0, "获取全部考试的列表成功", examVos);// 构造成功响应对象状态码为0表示成功
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();// 打印异常堆栈以供调试
resultVO = new ResultVO<>(-1, "获取全部考试的列表失败", null); resultVO = new ResultVO<>(-1, "获取全部考试的列表失败", null);// 构造失败响应对象,状态码为-1表示失败
} }
return resultVO; return resultVO;// 返回结果对象
} }
/**
*
*
* @return ResultVO
*/
@GetMapping("/question/type/list") @GetMapping("/question/type/list")
@ApiOperation("获取问题列表,按照单选、多选和判断题分类返回") @ApiOperation("获取问题列表,按照单选、多选和判断题分类返回")
ResultVO<ExamQuestionTypeVo> getExamQuestionTypeList() { ResultVO<ExamQuestionTypeVo> getExamQuestionTypeList() {
@ -130,7 +146,13 @@ public class ExamController {
} }
return resultVO; return resultVO;
} }
/**
*
*
* @param examCreateVo
* @param request HTTP ID
* @return Result VO
*/
@PostMapping("/create") @PostMapping("/create")
@ApiOperation("创建考试") @ApiOperation("创建考试")
ResultVO<Exam> createExam(@RequestBody ExamCreateVo examCreateVo, HttpServletRequest request) { ResultVO<Exam> createExam(@RequestBody ExamCreateVo examCreateVo, HttpServletRequest request) {
@ -146,7 +168,13 @@ public class ExamController {
} }
return resultVO; return resultVO;
} }
/**
*
*
* @param examVo
* @param request HTTP ID
* @return
*/
@PostMapping("/update") @PostMapping("/update")
@ApiOperation("更新考试") @ApiOperation("更新考试")
ResultVO<Exam> updateExam(@RequestBody ExamVo examVo, HttpServletRequest request) { ResultVO<Exam> updateExam(@RequestBody ExamVo examVo, HttpServletRequest request) {
@ -162,7 +190,11 @@ public class ExamController {
} }
return resultVO; return resultVO;
} }
/**
*
*
* @return Resultsvo
*/
@GetMapping("/card/list") @GetMapping("/card/list")
@ApiOperation("获取考试列表,适配前端卡片列表") @ApiOperation("获取考试列表,适配前端卡片列表")
ResultVO<List<ExamCardVo>> getExamCardList() { ResultVO<List<ExamCardVo>> getExamCardList() {
@ -177,7 +209,12 @@ public class ExamController {
} }
return resultVO; return resultVO;
} }
/**
* ID
*
* @param id
* @return Resultsvo
*/
@GetMapping("/detail/{id}") @GetMapping("/detail/{id}")
@ApiOperation("根据考试的id获取考试详情") @ApiOperation("根据考试的id获取考试详情")
ResultVO<ExamDetailVo> getExamDetail(@PathVariable String id) { ResultVO<ExamDetailVo> getExamDetail(@PathVariable String id) {
@ -191,52 +228,71 @@ public class ExamController {
} }
return resultVO; return resultVO;
} }
/**
* &nbsp;&nbsp;*
*&nbsp;@param&nbsp;examId&nbsp; &nbsp;&nbsp;*
*&nbsp;@param&nbsp;answersMap &nbsp;&nbsp;*
*&nbsp;@param request ID HTTP &nbsp;&nbsp;&nbsp; *
*&nbsp;@return &nbsp;&nbsp;&nbsp; *
*/
@PostMapping("/finish/{examId}") @PostMapping("/finish/{examId}")
@ApiOperation("根据用户提交的答案对指定id的考试判分") @ApiOperation("根据用户提交的答案对指定id的考试判分")
ResultVO<ExamRecord> finishExam(@PathVariable String examId, @RequestBody HashMap<String, List<String>> answersMap, HttpServletRequest request) { ResultVO<ExamRecord> finishExam(@PathVariable String examId, @RequestBody HashMap<String, List<String>> answersMap, HttpServletRequest request) {
ResultVO<ExamRecord> resultVO; ResultVO<ExamRecord> resultVO;// 定义一个用于存放结果的数据结构
try { try {
// 拦截器里设置上的用户id // 拦截器里设置上的用户id
String userId = (String) request.getAttribute("user_id"); String userId = (String) request.getAttribute("user_id");
// 下面根据用户提交的信息进行判分,返回用户的得分情况 // 下面根据用户提交的信息进行判分,返回用户的得分情况
ExamRecord examRecord = examService.judge(userId, examId, answersMap); ExamRecord examRecord = examService.judge(userId, examId, answersMap);
resultVO = new ResultVO<>(0, "考卷提交成功", examRecord); resultVO = new ResultVO<>(0, "考卷提交成功", examRecord);// 封装成绩记录到最终结果中
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
resultVO = new ResultVO<>(-1, "考卷提交失败", null); resultVO = new ResultVO<>(-1, "考卷提交失败", null);
} }
return resultVO; return resultVO;
} }
/**
* &nbsp;&nbsp;&nbsp; *
*&nbsp; &nbsp; *@ param request ID &nbsp;&nbsp;&nbsp; *
*&nbsp; &nbsp; *@ return Resultsvo &nbsp;&nbsp;&nbsp;&nbsp; *
*/
@GetMapping("/record/list") @GetMapping("/record/list")
@ApiOperation("获取当前用户的考试记录") @ApiOperation("获取当前用户的考试记录")
ResultVO<List<ExamRecordVo>> getExamRecordList(HttpServletRequest request) { ResultVO<List<ExamRecordVo>> getExamRecordList(HttpServletRequest request) {
ResultVO<List<ExamRecordVo>> resultVO; ResultVO<List<ExamRecordVo>> resultVO;//定义一个用于存放结果的数据结构
try { try {
// 拦截器里设置上的用户id // 拦截器里设置上的用户id
String userId = (String) request.getAttribute("user_id"); String userId = (String) request.getAttribute("user_id");
// 下面根据用户账号拿到他(她所有的考试信息)注意要用VO封装下 // 下面根据用户账号拿到他(她所有的考试信息)注意要用VO封装下
List<ExamRecordVo> examRecordVoList = examService.getExamRecordList(userId); List<ExamRecordVo> examRecordVoList = examService.getExamRecordList(userId);
resultVO = new ResultVO<>(0, "获取考试记录成功", examRecordVoList); resultVO = new ResultVO<>(0, "获取考试记录成功", examRecordVoList);//封装查询得到的信息到最终结果中;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
resultVO = new ResultVO<>(-1, "获取考试记录失败", null); resultVO = new ResultVO<>(-1, "获取考试记录失败", null);
} }
return resultVO; return resultVO;//返回封装好的 数据结构
} }
/**
recordId
*@ param recordId
*@ return Resulsvolumne
*/
@GetMapping("/record/detail/{recordId}") @GetMapping("/record/detail/{recordId}")
@ApiOperation("根据考试记录id获取考试记录详情") @ApiOperation("根据考试记录id获取考试记录详情")
ResultVO<RecordDetailVo> getExamRecordDetail(@PathVariable String recordId) { ResultVO<RecordDetailVo> getExamRecordDetail(@PathVariable String recordId) {
ResultVO<RecordDetailVo> resultVO; ResultVO<RecordDetailVo> resultVO;//定义一个用于存放结果的数据结构;
try { try {
RecordDetailVo recordDetailVo = examService.getRecordDetail(recordId); RecordDetailVo recordDetailVo = examService.getRecordDetail(recordId);
resultVO = new ResultVO<>(0, "获取考试记录详情成功", recordDetailVo); resultVO = new ResultVO<>(0, "获取考试记录详情成功", recordDetailVo);//封装查询得到的信息到最终结果中;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
resultVO = new ResultVO<>(-1, "获取考试记录详情失败", null); resultVO = new ResultVO<>(-1, "获取考试记录详情失败", null);
} }
return resultVO; return resultVO;//返回封装好的 数据结构
} }
} }

Loading…
Cancel
Save