查询所有课程

接口信息如下

路径地址 http://localhost:63040/content/course/list
请求方式 POST
请求参数 PageParam、QueryCourseParamsDTO
返回结果 PageResult

查询DTO

QueryCourseParamsDTO
/**
* 课程查询条件模型类
*/
@Data
public class QueryCourseParamsDTO implements Serializable {

private static final long serialVersionUID = 1L;

// 审核状态
private String auditStatus;
// 课程名称
private String courseName;
// 发布状态
private String publishStatus;
}

定义Service

找到CourseBaseService接口,定义课程查询接口

CourseBaseService
/**
* <p>
* 课程基本信息 服务类
* </p>
*
* @author sw-code
* @since 2023-08-18
*/
public interface CourseBaseService extends IService<CourseBase> {

/**
* 课程分页查询
*
* @param pageParam 分页参数
* @param dto 查询参数
*/
public PageResult<CourseBase> queryCourseBaseList(PageParam pageParam, QueryCourseParamsDTO dto);

}

实现该方法,找到其实现类CourseBaseServiceImp

CourseBaseServiceImpl
/**
* <p>
* 课程基本信息 服务实现类
* </p>
*
* @author sw-code
* @since 2023-08-18
*/
@Service
public class CourseBaseServiceImpl extends ServiceImpl<CourseBaseMapper, CourseBase> implements CourseBaseService {

/**
* 课程分页查询
*
* @param pageParam 分页参数
* @param dto 查询参数
*/
@Override
public PageResult<CourseBase> queryCourseBaseList(PageParam pageParam, QueryCourseParamsDTO dto) {
LambdaQueryWrapper<CourseBase> wrapper = Wrappers.<CourseBase>lambdaQuery()
.like(StringUtils.hasText(dto.getCourseName()), CourseBase::getName, dto.getCourseName())
.eq(StringUtils.hasText(dto.getAuditStatus()), CourseBase::getAuditStatus, dto.getAuditStatus())
.eq(StringUtils.hasText(dto.getPublishStatus()), CourseBase::getStatus, dto.getPublishStatus());

Page<CourseBase> page = new Page<>(pageParam.getPageNo(), pageParam.getPageSize());
Page<CourseBase> pageResult = page(page, wrapper);
return new PageResult<>(pageResult.getRecords(), pageResult.getTotal(), pageParam);
}
}

配置分页插件

learning-online-content-service工程下的com.swx.content.config包下创建分页配置类,其内容如下:

MybatisPlusConfig
@Configuration
@MapperScan("com.swx.content.mapper")
public class MybatisPlusConfig {

@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}

Mybatis分页插件的原理

首先分页参数放到ThreadLocal中,拦截执行的sql,根据数据库类型添加对应的分页语句重写sql,例如:(select * from table where a)转换为(select count(*) from table where a)和(select * from table where a limit ,),计算出total总条数,pageNum当前第几页,pageSize每页大小和当前页数据,是否为首页,是否为尾页,总页数等。

定义Controller

CourseBaseInfoController
@Api(value = "课程基本信息管理接口")
@RestController
@ResponseResult
@RequestMapping("/course")
public class CourseBaseInfoController {

private final CourseBaseService courseBaseService;

public CourseBaseInfoController(CourseBaseService courseBaseService) {
this.courseBaseService = courseBaseService;
}

@ApiOperation("课程查询接口")
@PostMapping("/list")
public PageResult<CourseBase> list(PageParam pageParam, @RequestBody(required = false) QueryCourseParamsDTO dto) {
return courseBaseService.queryCourseBaseList(pageParam, dto);
}
}

课程查询信息服务以提供,打开前端项目查看是否成功

Push到Git

commit "完成课程查询功能"

新增课程信息

接口信息如下

路径地址 http://localhost:63040/content/course
请求方式 POST
请求参数 AddCourseDTO
返回结果 CourseBaseInfoVO

新增DTO

QueryCourseParamsDTO
@Data
@ApiModel(value = "AddCourseDTO", description = "新增课程基本信息")
public class AddCourseDTO {

@NotEmpty(message = "课程名称不能为空")
@ApiModelProperty(value = "课程名称", required = true)
private String name;

@NotEmpty(message = "适用人群不能为空")
@Size(message = "适用人群内容过少", min = 10)
@ApiModelProperty(value = "适用人群", required = true)
private String users;

@ApiModelProperty(value = "课程标签")
private String tags;

@NotEmpty(message = "课程分类不能为空")
@ApiModelProperty(value = "课程大分类", required = true)
private String mt;

@NotEmpty(message = "课程分类不能为空")
@ApiModelProperty(value = "课程小分类", required = true)
private String st;

@NotEmpty(message = "课程等级不能为空")
@ApiModelProperty(value = "课程等级", required = true)
private String grade;

@NotEmpty(message = "课程等级不能为空")
@ApiModelProperty(value = "教学模式(普通,录播,直播等)", required = true)
private String teachmode;

@ApiModelProperty(value = "课程介绍")
private String description;

@ApiModelProperty(value = "课程图片")
private String pic;

@NotEmpty(message = "收费规则不能为空")
@ApiModelProperty(value = "收费规则,对应数据字典", required = true)
private String charge;

@ApiModelProperty(value = "价格")
private Float price;

@ApiModelProperty(value = "原价")
private Float originPrice;

@ApiModelProperty(value = "QQ")
private String qq;

@ApiModelProperty(value = "微信")
private String wechat;

@ApiModelProperty(value = "电话")
private String phone;

@ApiModelProperty(value = "有效期")
private Integer validDays;

}

视图VO

CourseBaseInfoVO
@Data
@EqualsAndHashCode(callSuper = true)
public class CourseBaseInfoVO extends CourseBase {


private String users;

private String tags;

private String mt;

private String st;

private String mtName;

private String stName;

private String grade;

private String teachmode;

private String description;

private String pic;

private String charge;

private Float price;

private Float originPrice;

private String qq;

private String wechat;

private String phone;

private String validDays;

}

定义Service

找到CourseBaseService接口,定义课程新增接口

CourseBaseService
/**
* 新增课程
*
* @param companyId 机构ID
* @param dto 课程信息
*/
public CourseBaseInfoVO createCourseBase(Long companyId, AddCourseDTO dto);

实现该方法,找到其实现类CourseBaseServiceImp,添加如下代码:

CourseBaseServiceImpl
private final CourseMarketService courseMarketService;
private final CourseCategoryService courseCategoryService;

public CourseBaseServiceImpl(CourseMarketService courseMarketService, CourseCategoryService courseCategoryService) {
this.courseMarketService = courseMarketService;
this.courseCategoryService = courseCategoryService;
}

/**
* 新增课程
*
* @param companyId 机构ID
* @param dto 课程信息
*/
@Override
@Transactional(rollbackFor = Exception.class)
public CourseBaseInfoVO createCourseBase(Long companyId, AddCourseDTO dto) {

// 新增course_base数据
CourseBase courseBase = new CourseBase();
BeanUtils.copyProperties(dto, courseBase);

courseBase.setCompanyId(companyId);
courseBase.setCreateDate(LocalDateTime.now());
// TODO 添加创建人信息

courseBase.setStatus("203001");
courseBase.setAuditStatus("202002");
boolean save = save(courseBase);
if (!save) {
throw new BizException("添加课程失败");
}

// 新增course_market数据
CourseMarket courseMarket = new CourseMarket();
BeanUtils.copyProperties(dto, courseMarket);
courseMarket.setId(courseBase.getId());
courseMarketService.saveOrUpdateCourseMarket(courseMarket);

// 查询详细信息
return getCourseBaseInfo(courseMarket.getId());
}

/**
* 查询课程信息
*
* @param courseId 课程ID
* @return 课程信息
*/
public CourseBaseInfoVO getCourseBaseInfo(long courseId) {
// 查询基本信息
CourseBase courseBase = getById(courseId);
if (courseBase == null) {
return null;
}
// 查询营销信息
CourseMarket courseMarket = courseMarketService.getById(courseId);
CourseBaseInfoVO courseBaseInfoVO = new CourseBaseInfoVO();
BeanUtils.copyProperties(courseBase, courseBaseInfoVO);
BeanUtils.copyProperties(courseMarket, courseBaseInfoVO);

// 查询分类信息
CourseCategory mtCategory = courseCategoryService.getById(courseBase.getMt());
CourseCategory stCategory = courseCategoryService.getById(courseBase.getSt());
courseBaseInfoVO.setMtName(mtCategory == null ? "" : mtCategory.getName());
courseBaseInfoVO.setStName(stCategory == null ? "" : stCategory.getName());
return courseBaseInfoVO;
}

定义Controller

找到CourseBaseInfoController,添加新增课程服务

CourseBaseInfoController
@ApiOperation("新增课程")
@PostMapping("")
public CourseBaseInfoVO createCourseBase(@RequestBody @Validated(ValidationGroup.Insert.class) AddCourseDTO dto) {
// TODO 暂时使用固定值
Long companyId = 1232141425L;
return courseBaseService.createCourseBase(companyId, dto);
}

新增课程信息服务以提供,打开前端项目查看是否成功

Push到Git

commit "完成新增课程功能"

修改课程信息

接口信息如下

路径地址 http://localhost:63040/content/course
请求方式 PUT
请求参数 AddCourseDTO
返回结果 CourseBaseInfoVO

定义Service

找到CourseBaseService接口,定义课程新增接口

CourseBaseService
/**
* 更新课程信息
*
* @param companyId 公司ID
* @param dto 修改信息
* @return 更新后的课程详细信息
*/
CourseBaseInfoVO updateCourseBase(Long companyId, EditCourseDTO dto);

实现该方法,找到其实现类CourseBaseServiceImp,添加如下代码:

CourseBaseServiceImpl
/**
* 更新课程信息
*
* @param companyId 公司ID
* @param dto 修改信息
* @return 更新后的课程详细信息
*/
@Override
@Transactional(rollbackFor = Exception.class)
public CourseBaseInfoVO updateCourseBase(Long companyId, EditCourseDTO dto) {
CourseBase dbCourseBase = getById(dto.getId());
if (dbCourseBase == null) {
throw new BizException(ResultCodeEnum.DATA_NOT_EXIST);
}

if (!companyId.equals(dbCourseBase.getCompanyId())) {
throw new BizException("本机构只能修改本机构的课程");
}

BeanUtils.copyProperties(dto, dbCourseBase);
dbCourseBase.setCreateDate(LocalDateTime.now());
// TODO 添加修改人信息

// 更新课程信息
boolean updateBase = updateById(dbCourseBase);
// 更新营销信息
CourseMarket courseMarket = new CourseMarket();
BeanUtils.copyProperties(dto, courseMarket);
boolean updateMarket = courseMarketService.saveOrUpdateCourseMarket(courseMarket);

if (!updateBase || !updateMarket) {
throw new BizException("修改课程失败");
}
return getCourseBaseInfo(dbCourseBase.getId());
}

定义Controller

找到CourseBaseInfoController,添加新增课程服务

CourseBaseInfoController
@ApiOperation("修改课程")
@PutMapping("")
public CourseBaseInfoVO updateCourseBase(@RequestBody @Validated EditCourseDTO dto) {
// TODO 暂时使用固定值
Long companyId = 1232141425L;
return courseBaseService.updateCourseBase(companyId, dto);
}

新增课程信息服务以提供,打开前端项目查看是否成功

Push到Git

commit "完成新增课程功能"

查询具体课程

接口信息如下

路径地址 http://localhost:63040/content/course/{courseId}
请求方式 GET
请求参数 Long
返回结果 CourseBaseInfoVO

定义Service

找到CourseBaseService接口,定义课程新增接口

CourseBaseService
/**
* 根据ID获取课程信息
*
* @param courseId 课程ID
*/
public CourseBaseInfoVO getCourseBaseInfo(Long courseId);

实现该方法,找到其实现类CourseBaseServiceImp,修改代码如下:

CourseBaseServiceImpl
/**
* 查询课程信息
*
* @param courseId 课程ID
* @return 课程信息
*/
@Override
public CourseBaseInfoVO getCourseBaseInfo(Long courseId) {
// 查询基本信息
CourseBase courseBase = getById(courseId);
if (courseBase == null) {
return null;
}
// 查询营销信息
CourseMarket courseMarket = Optional.ofNullable(courseMarketService.getById(courseId)).orElse(new CourseMarket());
CourseBaseInfoVO courseBaseInfoVO = new CourseBaseInfoVO();
BeanUtils.copyProperties(courseMarket, courseBaseInfoVO);
BeanUtils.copyProperties(courseBase, courseBaseInfoVO);

// 查询分类信息
CourseCategory mtCategory = courseCategoryService.getById(courseBase.getMt());
CourseCategory stCategory = courseCategoryService.getById(courseBase.getSt());
courseBaseInfoVO.setMtName(mtCategory == null ? "" : mtCategory.getName());
courseBaseInfoVO.setStName(stCategory == null ? "" : stCategory.getName());
return courseBaseInfoVO;
}

定义Controller接口信息

找到CourseBaseInfoController,添加新增课程服务

CourseBaseInfoController
@ApiOperation("根据课程id查询接口")
@GetMapping("/{courseId}")
public CourseBaseInfoVO list(@PathVariable("courseId") Long courseId) {
return courseBaseService.getCourseBaseInfo(courseId);
}

更新课程信息

接口信息如下

路径地址 http://localhost:63040/content/course
请求方式 PUT
请求参数 EditCourseDTO
返回结果 CourseBaseInfoVO

定义Service

找到CourseBaseService接口,定义课程新增接口

CourseBaseService
/**
* 更新课程信息
*
* @param companyId 公司ID
* @param dto 修改信息
* @return 更新后的课程详细信息
*/
CourseBaseInfoVO updateCourseBase(Long companyId, EditCourseDTO dto);

实现该方法,找到其实现类CourseBaseServiceImp,添加如下代码:

CourseBaseServiceImpl
/**
* 更新课程信息
*
* @param companyId 公司ID
* @param dto 修改信息
* @return 更新后的课程详细信息
*/
@Override
@Transactional(rollbackFor = Exception.class)
public CourseBaseInfoVO updateCourseBase(Long companyId, EditCourseDTO dto) {
CourseBase dbCourseBase = getById(dto.getId());
if (dbCourseBase == null) {
throw new BizException(ResultCodeEnum.DATA_NOT_EXIST);
}

if (!companyId.equals(dbCourseBase.getCompanyId())) {
throw new BizException("本机构只能修改本机构的课程");
}

BeanUtils.copyProperties(dto, dbCourseBase);
dbCourseBase.setChangeDate(LocalDateTime.now());
// TODO 添加修改人信息

// 更新课程信息
boolean updateBase = updateById(dbCourseBase);
// 更新营销信息
CourseMarket courseMarket = new CourseMarket();
BeanUtils.copyProperties(dto, courseMarket);
boolean updateMarket = courseMarketService.saveOrUpdateCourseMarket(courseMarket);

if (!updateBase || !updateMarket) {
throw new BizException("修改课程失败");
}
return getCourseBaseInfo(dbCourseBase.getId());
}

定义Controller

找到CourseBaseInfoController,添加新增课程服务

CourseBaseInfoController
@ApiOperation("修改课程")
@PutMapping("")
public CourseBaseInfoVO updateCourseBase(@RequestBody @Validated EditCourseDTO dto) {
// TODO 暂时使用固定值
Long companyId = 1232141425L;
return courseBaseService.updateCourseBase(companyId, dto);
}

Push到Git

commit "完成课程查询修改功能"