天机学堂-分页查询

需求

分页查询我的课表

返回:

总条数、总页数、当前页的课表信息的集合

返回的VO(已经封装成统一的LearningLessonsVO)

定义Controller

@RestController
@RequestMapping("/lessons")
@RequiredArgsConstructor
public class LearningLessonController {

    private final ILearningLessonService lessonService;


    @GetMapping("/page")
    public PageDTO<LearningLessonVO> queryMyLesson(PageQuery query) {
        return lessonService.querMyLesson(query);
    }
}

2.service实现类

代码逻辑:

(1).因为要查询当前用户下的所有课程分页查询信息,所以首先要拿到用户userId

Long userId = UserContext.getUser();

(2)分页查询并作非空判断,将userId当作条件传给eq,传一个page对象给page方法.

//2.分页查询
        Page<LearningLesson> page = lambdaQuery()
                .eq(LearningLesson::getUserId, userId)
                .page(query.toMpPage("latest_learn_time", false));
        List<LearningLesson> records = page.getRecords();
        if (CollUtils.isEmpty(records)) {
            return PageDTO.empty(page);
        }

(3)

先根据从数据库里面查询出来的records拿到每个课程的id。

再根据id调用课程的接口查询每个id对应的课程信息。

判空处理

用stream流的方式将每个课程的id和对应的课程数据放在map中。

//3.查询课程信息
        //3.1获取课程id
        Set<Long> Ids = records.stream().map(LearningLesson::getCourseId).collect(Collectors.toSet());
        //3.2查询课程信息
        List<CourseSimpleInfoDTO> infoList = courseClient.getSimpleInfoList(Ids);
        if (CollUtils.isEmpty(infoList)) {
            throw new BadRequestException("课程信息不存在");
        }
        //3.3把课程集合处理成map,key是courseId,value是course本身
        Map<Long, CourseSimpleInfoDTO> cMap = infoList.stream()
                .collect(Collectors.toMap(CourseSimpleInfoDTO::getId, c -> c));

 (4)

创建一个VO空数组

将po拷贝到VO中

将课程信息set到VO中

最后返回分页查询信息。

//4.封装vo返回
        List<LearningLessonVO> list = new ArrayList<>(records.size());
        //循环遍历,吧LearningLesson转变成VO
        for (LearningLesson r : records) {
            //4.1拷贝基础属性到LearningLessonVO
            LearningLessonVO learningLessonVO = BeanUtils.copyProperties(r, LearningLessonVO.class);
            //4.2获取课程信息,填充到vo
            CourseSimpleInfoDTO courseSimpleInfoDTO = cMap.get(r.getCourseId());
            learningLessonVO.setCourseName(courseSimpleInfoDTO.getName());
            learningLessonVO.setCourseCoverUrl(courseSimpleInfoDTO.getCoverUrl());
            learningLessonVO.setSections(courseSimpleInfoDTO.getSectionNum());
            list.add(learningLessonVO);

        }
        return PageDTO.of(page, list);

 

 @Override
    public PageDTO<LearningLessonVO> querMyLesson(PageQuery query) {
        //1.获取当前登录用户
        Long userId = UserContext.getUser();

        //2.分页查询
        Page<LearningLesson> page = lambdaQuery()
                .eq(LearningLesson::getUserId, userId)
                .page(query.toMpPage("latest_learn_time", false));
        List<LearningLesson> records = page.getRecords();
        if (CollUtils.isEmpty(records)) {
            return PageDTO.empty(page);
        }
        //3.查询课程信息
        //3.1获取课程id
        Set<Long> Ids = records.stream().map(LearningLesson::getCourseId).collect(Collectors.toSet());
        //3.2查询课程信息
        List<CourseSimpleInfoDTO> infoList = courseClient.getSimpleInfoList(Ids);
        if (CollUtils.isEmpty(infoList)) {
            throw new BadRequestException("课程信息不存在");
        }
        //3.3把课程集合处理成map,key是courseId,value是course本身
        Map<Long, CourseSimpleInfoDTO> cMap = infoList.stream()
                .collect(Collectors.toMap(CourseSimpleInfoDTO::getId, c -> c));

        //4.封装vo返回
        List<LearningLessonVO> list = new ArrayList<>(records.size());
        //循环遍历,吧LearningLesson转变成VO
        for (LearningLesson r : records) {
            //4.1拷贝基础属性到LearningLessonVO
            LearningLessonVO learningLessonVO = BeanUtils.copyProperties(r, LearningLessonVO.class);
            //4.2获取课程信息,填充到vo
            CourseSimpleInfoDTO courseSimpleInfoDTO = cMap.get(r.getCourseId());
            learningLessonVO.setCourseName(courseSimpleInfoDTO.getName());
            learningLessonVO.setCourseCoverUrl(courseSimpleInfoDTO.getCoverUrl());
            learningLessonVO.setSections(courseSimpleInfoDTO.getSectionNum());
            list.add(learningLessonVO);

        }
        return PageDTO.of(page, list);
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值