图片.png

一、后台api

1、定义form表单对象

CourseInfoForm.java

  1. package com.guli.edu.form;
  2. @ApiModel(value = "课程基本信息", description = "编辑课程基本信息的表单对象")
  3. @Data
  4. public class CourseInfoForm implements Serializable {
  5. private static final long serialVersionUID = 1L;
  6. @ApiModelProperty(value = "课程ID")
  7. private String id;
  8. @ApiModelProperty(value = "课程讲师ID")
  9. private String teacherId;
  10. @ApiModelProperty(value = "课程专业ID")
  11. private String subjectId;
  12. @ApiModelProperty(value = "课程标题")
  13. private String title;
  14. @ApiModelProperty(value = "课程销售价格,设置为0则可免费观看")
  15. private BigDecimal price;
  16. @ApiModelProperty(value = "总课时")
  17. private Integer lessonNum;
  18. @ApiModelProperty(value = "课程封面图片路径")
  19. private String cover;
  20. @ApiModelProperty(value = "课程简介")
  21. private String description;
  22. }

2、修改CourseDescription主键生成策略

  1. @ApiModelProperty(value = "课程ID")
  2. @TableId(value = "id", type = IdType.INPUT)
  3. private String id;

3、定义常量

实体类Course.Java中定义

  1. public static final String COURSE_DRAFT = "Draft";
  2. public static final String COURSE_NORMAL = "Normal";

4、定义控制层接口

CourseAdminController.java

  1. package com.guli.edu.controller.admin;
  2. @Api(description="课程管理")
  3. @CrossOrigin //跨域
  4. @RestController
  5. @RequestMapping("/admin/edu/course")
  6. public class CourseAdminController {
  7. @Autowired
  8. private CourseService courseService;
  9. @ApiOperation(value = "新增课程")
  10. @PostMapping("save-course-info")
  11. public R saveCourseInfo(
  12. @ApiParam(name = "CourseInfoForm", value = "课程基本信息", required = true)
  13. @RequestBody CourseInfoForm courseInfoForm){
  14. String courseId = courseService.saveCourseInfo(courseInfoForm);
  15. if(!StringUtils.isEmpty(courseId)){
  16. return R.ok().data("courseId", courseId);
  17. }else{
  18. return R.error().message("保存失败");
  19. }
  20. }
  21. }

5、定义业务层方法

接口:CourseService.java

  1. /**
  2. * 保存课程和课程详情信息
  3. * @param courseInfoForm
  4. * @return 新生成的课程id
  5. */
  6. String saveCourseInfo(CourseInfoForm courseInfoForm);

实现:CourseServiceImpl.java

  1. @Autowired
  2. private CourseDescriptionService courseDescriptionService;
  3. @Override
  4. public String saveCourseInfo(CourseInfoForm courseInfoForm) {
  5. //保存课程基本信息
  6. Course course = new Course();
  7. course.setStatus(Course.COURSE_DRAFT);
  8. BeanUtils.copyProperties(courseInfoForm, course);
  9. boolean resultCourseInfo = this.save(course);
  10. if(!resultCourseInfo){
  11. throw new GuliException(20001, "课程信息保存失败");
  12. }
  13. //保存课程详情信息
  14. CourseDescription courseDescription = new CourseDescription();
  15. courseDescription.setDescription(courseInfoForm.getDescription());
  16. courseDescription.setId(course.getId());
  17. boolean resultDescription = courseDescriptionService.save(courseDescription);
  18. if(!resultDescription){
  19. throw new GuliException(20001, "课程详情信息保存失败");
  20. }
  21. return course.getId();
  22. }

6、Swagger测试

二、前端实现

1、定义api

  1. import request from '@/utils/request'
  2. const api_name = '/admin/edu/course'
  3. export default {
  4. saveCourseInfo(courseInfo) {
  5. return request({
  6. url: `${api_name}/save-course-info`,
  7. method: 'post',
  8. data: courseInfo
  9. })
  10. }
  11. }

2、组件模板

  1. <el-form label-width="120px">
  2. <el-form-item label="课程标题">
  3. <el-input v-model="courseInfo.title" placeholder=" 示例:机器学习项目课:从基础到搭建项目视频课程。专业名称注意大小写"/>
  4. </el-form-item>
  5. <!-- 所属分类 TODO -->
  6. <!-- 课程讲师 TODO -->
  7. <el-form-item label="总课时">
  8. <el-input-number :min="0" v-model="courseInfo.lessonNum" controls-position="right" placeholder="请填写课程的总课时数"/>
  9. </el-form-item>
  10. <!-- 课程简介 TODO -->
  11. <!-- 课程封面 TODO -->
  12. <el-form-item label="课程价格">
  13. <el-input-number :min="0" v-model="courseInfo.price" controls-position="right" placeholder="免费课程请设置为0元"/>
  14. </el-form-item>
  15. <el-form-item>
  16. <el-button :disabled="saveBtnDisabled" type="primary" @click="next">保存并下一步</el-button>
  17. </el-form-item>
  18. </el-form>

3、组件js

  1. <script>
  2. import course from '@/api/edu/course'
  3. const defaultForm = {
  4. title: '',
  5. subjectId: '',
  6. teacherId: '',
  7. lessonNum: 0,
  8. description: '',
  9. cover: '',
  10. price: 0
  11. }
  12. export default {
  13. data() {
  14. return {
  15. courseInfo: defaultForm,
  16. saveBtnDisabled: false // 保存按钮是否禁用
  17. }
  18. },
  19. watch: {
  20. $route(to, from) {
  21. console.log('watch $route')
  22. this.init()
  23. }
  24. },
  25. created() {
  26. console.log('info created')
  27. this.init()
  28. },
  29. methods: {
  30. init() {
  31. if (this.$route.params && this.$route.params.id) {
  32. const id = this.$route.params.id
  33. console.log(id)
  34. } else {
  35. this.courseInfo = { ...defaultForm }
  36. }
  37. },
  38. next() {
  39. console.log('next')
  40. this.saveBtnDisabled = true
  41. if (!this.courseInfo.id) {
  42. this.saveData()
  43. } else {
  44. this.updateData()
  45. }
  46. },
  47. // 保存
  48. saveData() {
  49. course.saveCourseInfo(this.courseInfo).then(response => {
  50. this.$message({
  51. type: 'success',
  52. message: '保存成功!'
  53. })
  54. return response// 将响应结果传递给then
  55. }).then(response => {
  56. this.$router.push({ path: '/edu/course/chapter/' + response.data.courseId })
  57. }).catch((response) => {
  58. this.$message({
  59. type: 'error',
  60. message: response.message
  61. })
  62. })
  63. },
  64. updateData() {
  65. this.$router.push({ path: '/edu/course/chapter/1' })
  66. }
  67. }
  68. }
  69. </script>