Переглянути джерело

修改DAO层getCreatedQuestionIds方法(返回值类型改为List 允许传入sort参数作为排序依据),
修复获取当前用户创建的题目分页错误的问题

tubinyan 6 роки тому
батько
коміт
e9dd66957b

+ 1 - 1
src/main/java/nju/seec/helper/controller/QuestionController.java

@@ -122,7 +122,7 @@ public class QuestionController {
      */
     @Auth(roles = UserRole.TEACHER)
     @GetMapping("/created/ids")
-    public Set<String> getCreatedQuestionIds(LoginUser user) {
+    public List<String> getCreatedQuestionIds(LoginUser user) {
         return questionService.getIdsOfCreatedQuestions(user);
     }
 

+ 3 - 1
src/main/java/nju/seec/helper/dao/QuestionRecordDAO.java

@@ -4,9 +4,11 @@ import nju.seec.helper.entity.QuestionRecord;
 import nju.seec.helper.entity.User;
 import org.springframework.data.domain.Page;
 import org.springframework.data.domain.Pageable;
+import org.springframework.data.domain.Sort;
 import org.springframework.data.jpa.repository.JpaRepository;
 import org.springframework.data.jpa.repository.Query;
 
+import java.util.List;
 import java.util.Optional;
 import java.util.Set;
 
@@ -48,5 +50,5 @@ public interface QuestionRecordDAO extends JpaRepository<QuestionRecord, Long> {
      * @return
      */
     @Query("select record.questionId from QuestionRecord record where record.teacher=?1")
-    Set<String> findQuestionIdsByTeacher(User teacher);
+    List<String> findQuestionIdsByTeacher(User teacher, Sort sort);
 }

+ 1 - 1
src/main/java/nju/seec/helper/service/QuestionService.java

@@ -99,7 +99,7 @@ public interface QuestionService {
      * @param user
      * @return
      */
-    Set<String> getIdsOfCreatedQuestions(LoginUser user);
+    List<String> getIdsOfCreatedQuestions(LoginUser user);
 
     /**
      * 收藏题目

+ 8 - 6
src/main/java/nju/seec/helper/service/impl/QuestionServiceImpl.java

@@ -31,6 +31,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.domain.Page;
 import org.springframework.data.domain.PageImpl;
 import org.springframework.data.domain.Pageable;
+import org.springframework.data.domain.Sort;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
@@ -150,17 +151,18 @@ public class QuestionServiceImpl implements QuestionService {
     @Transactional(readOnly = true)
     @Override
     public Page<BaseQuestionVO> getCreatedQuestions(LoginUser user, String stem, String tag, String knowledgeId, Pageable pageable) {
-        Page<String> questionIdPage = questionRecordDAO.findQuestionIdsByTeacher(userDAO.findUserById(user.getId()), pageable);
-//        List<String> knowledgeIdList = (knowledgeId==null || knowledgeId.equals("")) ? new ArrayList<>() : Arrays.asList(knowledgeId.split("\\."));
+        List<String> questionIds = questionRecordDAO.findQuestionIdsByTeacher(userDAO.findUserById(user.getId()), pageable.getSort());
         // 目前仅支持单个知识点的匹配 多个知识点匹配仍需进一步完善
         // 这里对当前用户创建的所有问题 根据题干语句、知识点 进行筛选
-        List<BaseQuestionVO> ret = questionQueryApi.getQuestionListById(questionIdPage.getContent())
+        List<BaseQuestionVO> matchedQuestions = questionQueryApi.getQuestionListById(questionIds)
                 .stream()
                 .filter(questionVO -> questionVO.getStem().contains(stem) &&
                         questionVO.getKnowledgeId().stream().anyMatch(singleKnowledge -> singleKnowledge.contains(knowledgeId)))
                 .map(bokQuestion -> BaseQuestionVO.convertBokQuestionToVO(bokQuestion, true))
                 .collect(Collectors.toList());
-        return new PageImpl<>(ret, pageable, ret.size());
+        int fromIndex = pageable.getPageNumber()*pageable.getPageSize();
+        int toIndex = Math.min((fromIndex+pageable.getPageSize()), matchedQuestions.size());
+        return new PageImpl<>(matchedQuestions.subList(fromIndex, toIndex), pageable, matchedQuestions.size());
     }
 
 //    @Transactional(readOnly = true)
@@ -180,8 +182,8 @@ public class QuestionServiceImpl implements QuestionService {
 
     @Transactional(readOnly = true)
     @Override
-    public Set<String> getIdsOfCreatedQuestions(LoginUser user) {
-        return questionRecordDAO.findQuestionIdsByTeacher(userDAO.findUserById(user.getId()));
+    public List<String> getIdsOfCreatedQuestions(LoginUser user) {
+        return questionRecordDAO.findQuestionIdsByTeacher(userDAO.findUserById(user.getId()), (Sort) null);
     }
 
     @Override