package cn.seecoder.seecoderbokserver.web.rest; import cn.seecoder.bok.common.QuestionState; import cn.seecoder.bok.common.QuestionType; import cn.seecoder.bok.common.UserRole; import cn.seecoder.bok.common.api.SeecoderBokQuestionApi; import cn.seecoder.bok.common.api.SeecoderBokQuestionQueryApi; import cn.seecoder.bok.common.vo.GraspVO; import cn.seecoder.bok.common.vo.QuestionVO; import cn.seecoder.seecoderbokserver.aspect.auth.Authorized; import cn.seecoder.seecoderbokserver.domain.Question; import cn.seecoder.seecoderbokserver.mapper.QuestionConvertor; import cn.seecoder.seecoderbokserver.mapper.QuestionMapper; import cn.seecoder.seecoderbokserver.service.GraspService; import cn.seecoder.seecoderbokserver.service.QuestionService; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; import org.springframework.web.bind.annotation.*; import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotNull; import java.util.ArrayList; import java.util.List; import java.util.Optional; import java.util.stream.Collector; import java.util.stream.Collectors; import java.util.stream.StreamSupport; /** * @author Kunduin */ @RestController @RequestMapping("/api") public class QuestionController implements SeecoderBokQuestionApi, SeecoderBokQuestionQueryApi { private final QuestionService questionService; private final QuestionMapper questionMapper; private final GraspService graspService; public QuestionController(QuestionService questionService, QuestionMapper questionMapper, GraspService graspService) { this.questionService = questionService; this.questionMapper = questionMapper; this.graspService = graspService; } @Override @GetMapping("/question") public Page getQuestionList( @RequestParam(name = "stem", required = false) String stem, @RequestParam(name = "tag", required = false) List tags, @RequestParam(name = "type", required = false) List questionTypes, @RequestParam(name = "weight", required = false) List weights, @RequestParam(name = "knowledgeId", required = false) List knowledgeIds, Pageable pageable) { return questionService.find(stem, tags, questionTypes, weights, knowledgeIds, pageable) .map(questionMapper::questionToQuestionVO); } @GetMapping("/searchQuestion") public Response getSearchQuestionList( @RequestParam(name = "stem", required = false) String stem, @RequestParam(name = "tag", required = false) List tags, @RequestParam(name = "type", required = false) List questionTypes, @RequestParam(name = "weight", required = false) List weights, @RequestParam(name = "knowledgeId", required = false) List knowledgeIds, @RequestParam(name = "offset", required = false) int offset, @RequestParam(name = "pageSize", required = false) int pageSize) { Pageable pageable = PageRequest.of(offset, pageSize); return Response.buildSuccess(questionService.find(stem, tags, questionTypes, weights, knowledgeIds, pageable) .map(questionMapper::questionToQuestionVO)); } @GetMapping("/pageQuestion") public Response getTargetQuestionList( @RequestParam(name = "offset", required = false) int offset, @RequestParam(name = "pageSize", required = false) int pageSize) { Sort sort = Sort.by("_id"); //添加sort排序,按照es中的id排序 Pageable pageable = PageRequest.of(offset, pageSize, sort); return Response.buildSuccess(questionService.find(null, null, null, null, null, pageable) .map(questionMapper::questionToQuestionVO)); } @PostMapping("/question") public Response createQuestion(@NotNull @RequestBody QuestionVO question) { Question rawQuestion = QuestionConvertor.convertToEntity(question); return Response.buildSuccess(QuestionConvertor.convertToVO( questionService.save(rawQuestion))); } @Override @PostMapping("/questions") public List createQuestions(@NotNull @RequestBody List questionVOS) { List questions = questionVOS.stream().map(questionMapper::questionVOTOQuestion) .collect(Collectors.toList()); return StreamSupport.stream(questionService.saveAll(questions).spliterator(), false) .map(questionMapper::questionToQuestionVO).collect(Collectors.toList()); } @Override @GetMapping("/question/{id}") public Optional getQuestionById( @NotBlank @PathVariable(name = "id") String questionId) { return questionService.findById(questionId) .map(questionMapper::questionToQuestionVO); } @GetMapping("/getQuestion/{id}") public Response getQuestionById1( @NotBlank @PathVariable(name = "id") String questionId) { return Response.buildSuccess(questionService.findById(questionId) .map(questionMapper::questionToQuestionVO)); } @PutMapping("/question/{id}") public Response updateQuestion( @NotNull @RequestBody QuestionVO question) { Question rawQuestion = QuestionConvertor.convertToEntity(question); return Response.buildSuccess( QuestionConvertor.convertToVO(questionService.save(rawQuestion))); } @DeleteMapping("/question/{id}") public Response deleteQuestionById( @NotBlank @PathVariable(name = "id") String questionId) { questionService.delete(questionId); return Response.buildSuccess("Delete Successfully"); } @Override @GetMapping("/question-query/id") public List getQuestionListByIds(@NotNull @RequestParam("id") List ids) { return StreamSupport.stream(questionService.findAllById(ids).spliterator(), false) .map(questionMapper::questionToQuestionVO).collect(Collectors.toList()); } @GetMapping("/state") @Authorized(roles = {UserRole.ADMIN, UserRole.HR, UserRole.TEACHER, UserRole.SEEC, UserRole.SEECX}) public Response findUncheckedQuestion( @RequestParam(name = "state") QuestionState questionState, @RequestParam(name = "offset", required = false) int offset, @RequestParam(name = "pageSize", required = false) int pageSize) { Pageable pageable = PageRequest.of(offset, pageSize); return Response.buildSuccess(questionService.findUncheckedQuestion(questionState, pageable)); } @GetMapping("/grasp") public Response getAllGrasp() { return Response.buildSuccess(graspService.getAll()); } @GetMapping("/grasp/{question}") public Response getAllGraspByQuestion(@PathVariable Integer question) { return Response.buildSuccess(graspService.getAllByQuestion(question)); } @PostMapping("/grasp") public Response updateGrasp(@RequestBody GraspVO graspVO) { return Response.buildSuccess(graspService.save(graspVO)); } }