一、后台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、定义业务层方法

/**
  * 保存课程和课程详情信息
  * @param courseInfoForm
  * @return 新生成的课程id
  */
String saveCourseInfo(CourseInfoForm courseInfoForm);
@Autowired
private CourseDescriptionService courseDescriptionService;

@Override
public String saveCourseInfo(CourseInfoForm courseInfoForm) {

     //保存课程基本信息
     Course course = new Course();
     course.setStatus(Course.COURSE_DRAFT);
     BeanUtils.copyProperties(courseInfoForm, course);
     boolean resultCourseInfo = this.save(course);
     if(!resultCourseInfo){
         throw new GuliException(20001, "课程信息保存失败");
     }

     //保存课程详情信息
     CourseDescription courseDescription = new CourseDescription();
     courseDescription.setDescription(courseInfoForm.getDescription());
     courseDescription.setId(course.getId());
     boolean resultDescription = courseDescriptionService.save(courseDescription);
     if(!resultDescription){
         throw new GuliException(20001, "课程详情信息保存失败");
     }

     return course.getId();
    }

6、Swagger测试

二、前端实现

1、定义api

import request from '@/utils/request'

const api_name = '/admin/edu/course'

export default {
    saveCourseInfo(courseInfo) {
      return request({
        url: `${api_name}/save-course-info`,
        method: 'post',
        data: courseInfo
      })
    }

}

2、组件模板

<el-form label-width="120px">


  <el-form-item label="课程标题">
    <el-input v-model="courseInfo.title" placeholder=" 示例:机器学习项目课:从基础到搭建项目视频课程。专业名称注意大小写"/>
  </el-form-item>


  <!-- 所属分类 TODO -->


  <!-- 课程讲师 TODO -->


  <el-form-item label="总课时">
    <el-input-number :min="0" v-model="courseInfo.lessonNum" controls-position="right" placeholder="请填写课程的总课时数"/>
  </el-form-item>


  <!-- 课程简介 TODO -->


  <!-- 课程封面 TODO -->


  <el-form-item label="课程价格">
    <el-input-number :min="0" v-model="courseInfo.price" controls-position="right" placeholder="免费课程请设置为0元"/> 元
  </el-form-item>


  <el-form-item>
    <el-button :disabled="saveBtnDisabled" type="primary" @click="next">保存并下一步</el-button>
  </el-form-item>

</el-form>

3、组件js

<script>

import course from '@/api/edu/course'

const defaultForm = {
  title: '',
  subjectId: '',
  teacherId: '',
  lessonNum: 0,
  description: '',
  cover: '',
  price: 0

}

export default {
  data() {
    return {
      courseInfo: defaultForm,
      saveBtnDisabled: false // 保存按钮是否禁用
    }
  },


  watch: {
    $route(to, from) {
      console.log('watch $route')
      this.init()
    }
  },



  created() {
   console.log('info created')
   this.init()
    },

  methods: {


    init() {
      if (this.$route.params && this.$route.params.id) {
        const id = this.$route.params.id
        console.log(id)
      } else {
        this.courseInfo = { ...defaultForm }
      }
    },


    next() {
      console.log('next')
      this.saveBtnDisabled = true
      if (!this.courseInfo.id) {
        this.saveData()
      } else {
        this.updateData()
      }
    },


    // 保存
    saveData() {
      course.saveCourseInfo(this.courseInfo).then(response => {
        this.$message({
          type: 'success',
          message: '保存成功!'
        })
        return response// 将响应结果传递给then
      }).then(response => {
        this.$router.push({ path: '/edu/course/chapter/' + response.data.courseId })
      }).catch((response) => {
        this.$message({
          type: 'error',
          message: response.message
        })
      })
    },


    updateData() {
      this.$router.push({ path: '/edu/course/chapter/1' })
    }
  }

}
78
</script>