package nju.seec.helper.controller;
import nju.seec.helper.api.bok.dto.CollectDTO;
import nju.seec.helper.api.bok.dto.RateDTO;
import nju.seec.helper.api.bok.vo.InteractiveVO;
import nju.seec.helper.aspect.auth.Auth;
import nju.seec.helper.aspect.auth.LoginUser;
import nju.seec.helper.controller.response.PageResponse;
import nju.seec.helper.dto.groups.Create;
import nju.seec.helper.dto.groups.Modify;
import nju.seec.helper.dto.question.BaseQuestionDTO;
import nju.seec.helper.enums.UserRole;
import nju.seec.helper.service.QuestionService;
import nju.seec.helper.vo.question.BaseQuestionVO;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Set;
/**
* 题库
*
* @author xst
*
* updated by cst
*/
@RestController
@RequestMapping("/api/question")
public class QuestionController {
private final QuestionService questionService;
public QuestionController(QuestionService questionService) {
this.questionService = questionService;
}
/**
* 获得题目列表
*/
@Auth(roles = {UserRole.TEACHER}, message = "获得题目列表")
@GetMapping
public PageResponse getQuestionList(LoginUser user,
@RequestParam(required = false, defaultValue = "") String stem,
@RequestParam(required = false, defaultValue = "") String tag,
@RequestParam(required = false, defaultValue = "") String knowledgeId,
@PageableDefault(Integer.MAX_VALUE) Pageable pageable) {
return PageResponse.of(questionService.getQuestions(user, stem, tag, knowledgeId, pageable));
}
/**
* 获取某一测试题目
*/
@Auth(roles = {UserRole.TEACHER, UserRole.STUDENT}, message = "获得测试题目")
@GetMapping("/quiz/{quizId}")
public List getQuestionsByQuiz(LoginUser user, @PathVariable Long quizId) {
return questionService.getQuestionsByQuiz(user, quizId);
}
/**
* 获取某一题目信息
*/
@Auth(roles = {UserRole.TEACHER}, message = "获取题目信息")
@GetMapping("/{questionId}")
public BaseQuestionVO getOneQuestion(LoginUser user, @PathVariable String questionId) {
return questionService.getOneQuestion(user, questionId);
}
/**
* 创建题目
*/
@Auth(roles = UserRole.TEACHER, message = "创建题目")
@PostMapping
public BaseQuestionVO createQuestion(LoginUser user, @Validated(Create.class) @RequestBody BaseQuestionDTO baseQuestionDTO) {
return questionService.createQuestion(user, baseQuestionDTO);
}
/**
* 修改题目
*/
@Auth(roles = UserRole.TEACHER, message = "修改题目")
@PutMapping("/{questionId}")
public BaseQuestionVO modifyQuestion(LoginUser user, @PathVariable String questionId, @Validated(Modify.class) @RequestBody BaseQuestionDTO baseQuestionDTO) {
return questionService.modifyQuestion(user, questionId, baseQuestionDTO);
}
/**
* 删除题目
*/
@Auth(roles = UserRole.TEACHER, message = "删除题目")
@DeleteMapping("/{questionId}")
public void deleteQuestion(LoginUser user, @PathVariable String questionId) {
questionService.deleteQuestion(user, questionId);
}
/**
* 获取创建题目
*/
@Auth(roles = UserRole.TEACHER, message = "获取创建的题目")
@GetMapping("/created")
public PageResponse getCreatedQuestions(LoginUser user, @PageableDefault(size = Integer.MAX_VALUE) Pageable pageable) {
return PageResponse.of(questionService.getCreatedQuestions(user, pageable));
}
/**
* 获取创建题目ID
*/
@Auth(roles = UserRole.TEACHER)
@GetMapping("/created/ids")
public Set getCreatedQuestionIds(LoginUser user) {
return questionService.getIdsOfCreatedQuestions(user);
}
/**
* 收藏题目
*/
@Auth(roles = {UserRole.TEACHER, UserRole.STUDENT}, message = "收藏题目")
@PostMapping("/{questionId}/collect")
public void starQuestion(LoginUser user, @PathVariable String questionId, @Validated @RequestBody CollectDTO collectDTO) {
questionService.collectQuestion(user, questionId, collectDTO);
}
/**
* 评价题目
*/
@Auth(roles = {UserRole.TEACHER, UserRole.STUDENT}, message = "评价题目")
@PostMapping("/{questionId}/rate")
public void rateQuestion(LoginUser user, @PathVariable String questionId, @Validated @RequestBody RateDTO rateDTO) {
questionService.rateQuestion(user, questionId, rateDTO);
}
/**
* 取得收藏评价的题目
*/
@Auth(roles = {UserRole.TEACHER, UserRole.STUDENT}, message = "取得收藏评价的题目")
@GetMapping("/collected")
public PageResponse getCollectedQuestions(LoginUser user, @PageableDefault(size = Integer.MAX_VALUE) Pageable pageable) {
return PageResponse.of(questionService.getCollectedAndRatedQuestions(user, true, null, pageable));
}
@Auth(roles = {UserRole.TEACHER, UserRole.STUDENT}, message = "取得收藏评价的题目")
@GetMapping("/rated")
public PageResponse getRatedQuestions(LoginUser user, @PageableDefault(size = Integer.MAX_VALUE) Pageable pageable) {
return PageResponse.of(questionService.getCollectedAndRatedQuestions(user, null, true, pageable));
}
/**
* 取得完成的题目
*/
@Auth(roles = UserRole.STUDENT, message = "取得完成的题目")
@GetMapping("/completed")
public PageResponse getCompletedQuestions(LoginUser user, @PageableDefault(size = Integer.MAX_VALUE) Pageable pageable) {
return PageResponse.of(questionService.getCompletedQuestions(user, pageable));
}
/**
* 取得与题目的交互信息
*/
@Auth(roles = {UserRole.TEACHER, UserRole.STUDENT}, message = "取得与题目的交互信息")
@GetMapping("/interactive")
public List getInteractiveList(LoginUser user, String questionId) {
return questionService.getInteractiveList(user, questionId);
}
}