Browse Source

fix : 修改已经开始的测试返回错误信息 todo:未校验权限

XuShengTao 6 years ago
parent
commit
93d106c55b

+ 11 - 7
src/main/java/nju/seec/helper/service/impl/QuestionServiceImpl.java

@@ -12,7 +12,6 @@ import nju.seec.helper.util.exception.HelperException;
 import nju.seec.helper.vo.quiz.ChoiceQuestionVO;
 import nju.seec.helper.vo.quiz.QuestionVO;
 import nju.seec.helper.vo.quiz.TrueOrFalseQuestionVO;
-import org.omg.CORBA.portable.UnknownException;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.domain.Page;
 import org.springframework.data.domain.PageImpl;
@@ -23,26 +22,31 @@ import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
-import java.util.stream.Collectors;
 
 @Service
 public class QuestionServiceImpl implements QuestionService {
+    private final BOKUtil bokUtil;
+
     @Autowired
-    private BOKUtil bokUtil;
+    public QuestionServiceImpl(BOKUtil bokUtil) {
+        this.bokUtil = bokUtil;
+    }
 
     @Override
-    public Page<QuestionVO> getQuestionList(LoginUser user, String stem, Pageable pageable) throws JsonProcessingException {
-        List<QuestionVO> Questions = new ArrayList<QuestionVO>();
+    public Page<QuestionVO> getQuestionList(LoginUser user, String stem, Pageable pageable) {
+        //应该对象化
+        List<QuestionVO> Questions = new ArrayList<>();
         Map page = new HashMap();
+
         bokUtil.BokFindByStemLike(stem, Questions,page,pageable);
-        Page<QuestionVO> ret =new PageImpl<QuestionVO>(Questions,pageable,Long.valueOf((Integer)page.get("totalElements")));
-        return ret;
+        return new PageImpl<>(Questions,pageable,Long.valueOf((Integer)page.get("totalElements")));
     }
 
     @Override
     public QuestionVO getQuestion(String questionId) {
         return bokUtil.BokFindById(questionId);
     }
+
     @Override
     public QuestionVO newQuestion(QuestionVO newQuestion) {
         try {

+ 11 - 9
src/main/java/nju/seec/helper/service/impl/QuizServiceImpl.java

@@ -5,9 +5,11 @@ import nju.seec.helper.dto.QuestionStudentAnswerDTO;
 import nju.seec.helper.entity.*;
 import nju.seec.helper.service.QuizService;
 import nju.seec.helper.util.BOKUtil;
+import nju.seec.helper.util.enums.ExceptionType;
 import nju.seec.helper.util.enums.QuizState;
 import nju.seec.helper.util.enums.SlideState;
 import nju.seec.helper.util.enums.UserType;
+import nju.seec.helper.util.exception.HelperException;
 import nju.seec.helper.vo.quiz.QuestionVO;
 import nju.seec.helper.vo.quiz.QuizResultVO;
 import nju.seec.helper.vo.quiz.QuizVO;
@@ -68,7 +70,7 @@ public class QuizServiceImpl implements QuizService {
     @Override
     public String deleteQuiz(Long quizId) {
         Quiz quiz =quizDAO.findQuizById(quizId);
-        if(quiz.getState()!=QuizState.NOT_STARTED){return "can't delete started quiz!";}
+        if(quiz.getState()!=QuizState.NOT_STARTED){throw HelperException.of(ExceptionType.FORBIDDEN,"不能删除已经开始的测试!");}
         quizDAO.delete(quiz);
         return "delete success";
     }
@@ -78,7 +80,7 @@ public class QuizServiceImpl implements QuizService {
     public Quiz modifyQuiz(Long quizId, String name, List<String> questions, SlideState quizTime, String slideId) {
 
         Quiz quiz =quizDAO.findQuizById(quizId);
-        if(quiz.getState()!=QuizState.NOT_STARTED){throw new RuntimeException("can't delete started quiz!");}
+        if(quiz.getState()!=QuizState.NOT_STARTED){throw HelperException.of(ExceptionType.FORBIDDEN,"不能修改已经开始的测试!");}
         if(name!=null) quiz.setName(name);
         if(questions!=null) quiz.setQuestions(questions);
         if(quizTime!=null) {
@@ -118,9 +120,9 @@ public class QuizServiceImpl implements QuizService {
 
     @Override
     public List<QuizResultVO> getQuizResult(Long quizId) {
-        Assert.notNull(quizId,"QUIZ ID can't be null!");
+        Assert.notNull(quizId,"错误的测试!");
         final Quiz quiz = quizDAO.findQuizById(quizId);
-        if(quiz.getState()!=QuizState.CLOSED){throw new RuntimeException("quiz not close!");}
+        if(quiz.getState()!=QuizState.CLOSED){throw new RuntimeException("测试未关闭,无法得到测试结果");}
         List<QuizResultVO> retList=new ArrayList<>();
         final List<StudentScore> studentScoreList = quiz.getStudentScore();
         Map<Integer,List<QuizStudentAnswer>> IdQuizStudentAnswerMap=new HashMap<>();
@@ -145,9 +147,9 @@ public class QuizServiceImpl implements QuizService {
     @Override
     @Transactional
     public Integer upStudentAnswer(Long quizId, Integer uid, List<QuestionStudentAnswerDTO> answers) {
-        Assert.notNull(quizId,"QUIZ ID can't be null!");
+        Assert.notNull(quizId,"错误的测试!");
         final Quiz quiz = quizDAO.findQuizById(quizId);
-        if(quiz.getState()!=QuizState.ONGOING){throw new RuntimeException("can't put not ONGOING quiz answer!");}
+        if(quiz.getState()!=QuizState.ONGOING){throw HelperException.of(ExceptionType.FORBIDDEN,"不能提交尚未开始的作业!");}
         User student =new User();student.setId(uid);
         Set<QuizStudentAnswer> QuizAnswers = quizStudentAnswerDAO.findByStudentAndQuiz(student,quiz);
         Set<String> questionIdSet = new HashSet<>(quiz.getQuestions());
@@ -178,7 +180,7 @@ public class QuizServiceImpl implements QuizService {
 
     @Override
     public Page<Quiz> getAllBasicQuizBySlide(Integer slideId, Pageable pageable, QuizState state, Integer uid) {
-        Assert.notNull(slideId,"slide can't be null");
+        Assert.notNull(slideId,"错误的幻灯片!");
         Slide slide=new Slide();
         slide.setId(slideId);
         if(state!=null)
@@ -292,14 +294,14 @@ public class QuizServiceImpl implements QuizService {
     }
     @Override
     public QuizVO combineQuiz(Long quizId){
-        Assert.notNull(quizId,"QUIZ ID can't be null!");
+        Assert.notNull(quizId,"错误的测试!");
         final Quiz quizById = quizDAO.findQuizById(quizId);
         final List<QuestionVO> questionVOS = bokUtil.BokFindByIdIn(quizById.getQuestions());
         return new QuizVO(quizById,questionVOS);
     }
     @Override
     public QuizVO combineQuizWithAnswer(Long quizId, Integer uid,UserType userType){
-        Assert.notNull(quizId,"QUIZ ID can't be null!");
+        Assert.notNull(quizId,"错误的测试!");
         final Quiz quizById = quizDAO.findQuizById(quizId);
         User student =new User();
         student.setId(uid);