为进行前后端通信,需为项目编写controller类,具体实现了处理管理员对课程信息等进行管理的HTTP请求类,下面我展示一个功能的controller类的实现代码
Api(tags = "auth-课程信息")
@RestController
@RequiredArgsConstructor
@RequestMapping("/course/auth/course")
public class AuthCourseController {
@NotNull
private final AuthCourseBiz biz;
@NotNull
private final CourseBiz courseBiz;
/**
* 课程详情接口
*/
@ApiOperation(value = "课程详情", notes = "校验课程是否可以学习")
@RequestMapping(value = "/view", method = RequestMethod.POST)
public Result<CourseResp> view(@RequestBody CourseReq req) {
return courseBiz.view(req, ThreadContext.userId());
}
/**
* 课程信息列表接口
*
* @author fengyw
*/
@ApiOperation(value = "学习配置", notes = "获取课时学习需要的配置参数")
@RequestMapping(value = "/sign", method = RequestMethod.POST)
public Result<AuthCourseSignResp> sign(@RequestBody AuthCourseSignReq req) {
return biz.sign(req);
}
}
同时也对项目业务逻辑类进行设计和编写工作
以课程信息的业务逻辑代码为例:
@Component
@CacheConfig(cacheNames = {"course"})
@RequiredArgsConstructor
public class ApiCourseBiz extends BaseBiz {
@NotNull
private final CourseDao dao;
@NotNull
private final CourseChapterDao chapterDao;
@NotNull
private final CourseChapterPeriodDao periodDao;
@NotNull
private final CategoryDao categoryDao;
@NotNull
private final IFeignLecturer feignLecturer;
public Result<Page<ApiCoursePageResp>> searchForPage(ApiCoursePageReq req) {
// 直接查库
CourseExample example = new CourseExample();
CourseExample.Criteria c = example.createCriteria();
if (ObjectUtil.isNotEmpty(req.getCategoryId())) {
c.andCategoryIdEqualTo(req.getCategoryId());
}
if (ObjectUtil.isNotEmpty(req.getIsFree())) {
c.andIsFreeEqualTo(req.getIsFree());
}
if (StringUtils.hasText(req.getCourseName())) {
c.andCourseNameLike(PageUtil.like(req.getCourseName()));
}
c.andStatusIdEqualTo(1);
example.setOrderByClause("course_sort asc, id desc");
Page<Course> page = dao.page(req.getPageCurrent(), req.getPageSize(), example);
return Result.success(PageUtil.transform(page, ApiCoursePageResp.class));
}
// }
private List<Long> listCategoryId(Long categoryId) {
CategoryExample example = new CategoryExample();
example.createCriteria().andStatusIdEqualTo(StatusIdEnum.YES.getCode());
List<Category> categories = categoryDao.listByExample(example);
List<Long> idList = new ArrayList<>();
// 需要查询的ID
idList.add(categoryId);
filter(idList, categories, categoryId);
return idList;
}
private List<ApiCategoryResp> filter(List<Long> idList, List<Category> categories, Long categoryId) {
List<Category> list = categories.stream().filter(item -> item.getParentId().compareTo(categoryId) == 0).collect(Collectors.toList());
if (CollUtil.isNotEmpty(list)) {
idList.addAll(list.stream().map(Category::getId).collect(Collectors.toList()));
List<ApiCategoryResp> resps = BeanUtil.copyProperties(list, ApiCategoryResp.class);
for (ApiCategoryResp resp : resps) {
resp.setList(filter(idList, categories, resp.getId()));
}
return resps;
}
return new ArrayList<>();
}
@Cacheable
public Result<CourseResp> view(CourseReq req) {
Course course = dao.getById(req.getCourseId());
if (course == null) {
return Result.error("找不到该课程信息");
}
if (!course.getStatusId().equals(StatusIdEnum.YES.getCode())) {
return Result.error("该课程已被禁用");
}
if (course.getIsPutaway().equals(PutawayEnum.DOWN.getCode())) {
return Result.error("该课程已下架");
}
CourseResp courseResp = BeanUtil.copyProperties(course, CourseResp.class);
// 获取讲师信息
LecturerViewVO lecturerViewVO = feignLecturer.getById(course.getLecturerId());
if (ObjectUtil.isNotEmpty(lecturerViewVO)) {
courseResp.setLecturerResp(BeanUtil.copyProperties(lecturerViewVO, CourseLecturerResp.class));
}
// 章节信息
List<CourseChapter> chapterList = chapterDao.listByCourseIdAndStatusId(course.getId(), StatusIdEnum.YES.getCode());
if (CollUtil.isNotEmpty(chapterList)) {
courseResp.setChapterRespList(BeanUtil.copyProperties(chapterList, CourseChapterResp.class));
// 课时信息
List<CourseChapterPeriod> periodList = periodDao.listByCourseIdAndStatusId(course.getId(), StatusIdEnum.YES.getCode());
if (CollUtil.isNotEmpty(periodList)) {
Map<Long, List<CourseChapterPeriod>> map = periodList.stream().collect(Collectors.groupingBy(CourseChapterPeriod::getChapterId, Collectors.toList()));
for (CourseChapterResp chapterResp : courseResp.getChapterRespList()) {
chapterResp.setPeriodRespList(BeanUtil.copyProperties(map.get(chapterResp.getId()), CourseChapterPeriodResp.class));
}
}
}
return Result.success(courseResp);
}
}
-
CourseDao dao
、CourseChapterDao chapterDao
、CourseChapterPeriodDao periodDao
、CategoryDao categoryDao
:分别是课程、课程章节、课程章节课时、分类等数据访问对象的引用。IFeignLecturer feignLecturer
:对外部讲师服务的Feign客户端的引用。
-
searchForPage(ApiCoursePageReq req)
方法:处理课程信息的分页查询请求,并返回分页结果。首先构建查询条件,然后调用dao.page()
方法从数据库中查询课程信息,最后使用PageUtil.transform()
方法将查询结果转换为ApiCoursePageResp
类型,并将其包装在Result
中返回。 -
listCategoryId(Long categoryId)
和filter(List<Long> idList, List<Category> categories, Long categoryId)
方法:这两个方法用于辅助搜索并列出特定分类的课程信息。listCategoryId
方法根据给定的分类ID查询出包含其所有子分类ID的列表,而filter
方法则用于过滤出所有满足条件的子分类,并将其放入结果列表中。 -
view(CourseReq req)
方法:处理课程详情查询请求,根据课程ID获取课程信息。首先根据课程ID从数据库中查询课程信息,然后进行相关的判断,如课程是否存在、是否被禁用、是否已下架等。接着获取课程的讲师信息,并将其组装成CourseResp
对象返回。最后,获取课程的章节信息和课时信息,并将其组装到CourseResp
对象中返回。