为进行前后端通信,需为项目编写controller类,具体实现了处理管理员对课程信息等进行管理的HTTP请求类,下面我展示一个功能的controller类的实现代码
package com.roncoo.education.course.service.admin;
import com.roncoo.education.common.core.base.Page;
import com.roncoo.education.common.core.base.Result;
import com.roncoo.education.common.config.ThreadContext;
import com.roncoo.education.course.service.admin.biz.AdminCourseBiz;
import com.roncoo.education.course.service.admin.req.AdminCourseEditReq;
import com.roncoo.education.course.service.admin.req.AdminCoursePageReq;
import com.roncoo.education.course.service.admin.req.AdminCourseSaveReq;
import com.roncoo.education.course.service.admin.resp.AdminCoursePageResp;
import com.roncoo.education.course.service.admin.resp.AdminCourseViewResp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
/**
* ADMIN-课程信息
*
*/
@Api(tags = "admin-课程信息")
@RestController
@RequiredArgsConstructor
@RequestMapping("/course/admin/course")
public class AdminCourseController {
@NotNull
private final AdminCourseBiz biz;
@ApiOperation(value = "课程信息分页", notes = "课程信息分页")
@PostMapping(value = "/page")
public Result<Page<AdminCoursePageResp>> page(@RequestBody AdminCoursePageReq req) {
long userId = ThreadContext.userId();
return biz.page(req,userId);
}
@ApiOperation(value = "课程信息添加", notes = "课程信息添加")
@PostMapping(value = "/save")
public Result<String> save(@RequestBody @Valid AdminCourseSaveReq req) {
long userId = ThreadContext.userId();
return biz.save(req, userId);
}
@ApiOperation(value = "课程信息查看", notes = "课程信息查看")
@ApiImplicitParam(name = "id", value = "主键ID", dataTypeClass = Long.class, paramType = "query", required = true)
@GetMapping(value = "/view")
public Result<AdminCourseViewResp> view(@RequestParam Long id) {
return biz.view(id);
}
@ApiOperation(value = "课程信息修改", notes = "课程信息修改")
@PutMapping(value = "/edit")
public Result<String> edit(@RequestBody @Valid AdminCourseEditReq req) {
return biz.edit(req);
}
@ApiOperation(value = "课程信息删除", notes = "课程信息删除")
@ApiImplicitParam(name = "id", value = "主键ID", dataTypeClass = Long.class, paramType = "query", required = true)
@DeleteMapping(value = "/delete")
public Result<String> delete(@RequestParam Long id) {
return biz.delete(id);
}
// @ApiOperation(value = "ES同步", notes = "将课程同步到ES")
// @GetMapping(value = "/es")
// public Result<String> syncEs() {
// return biz.syncEs();
// }
}
同时也对项目业务逻辑类进行设计和编写工作
以课程信息的业务逻辑代码为例:
@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 对象中返回。