一、后端实现

1、业务层

接口:CourseService.java

  1. void updateCourseInfoById(CourseInfoForm courseInfoForm);

实现:CourseServiceImpl.java

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

2、web层

  1. @ApiOperation(value = "更新课程")
  2. @PutMapping("update-course-info/{id}")
  3. public R updateCourseInfoById(
  4. @ApiParam(name = "CourseInfoForm", value = "课程基本信息", required = true)
  5. @RequestBody CourseInfoForm courseInfoForm,
  6. @ApiParam(name = "id", value = "课程ID", required = true)
  7. @PathVariable String id){
  8. courseService.updateCourseInfoById(courseInfoForm);
  9. return R.ok();
  10. }

二、前端实现

1、定义api

course.js

  1. updateCourseInfoById(courseInfo) {
  2. return request({
  3. url: `${api_name}/update-course-info/${courseInfo.id}`,
  4. method: 'put',
  5. data: courseInfo
  6. })
  7. }

2、组件js

info.vue

  1. updateData() {
  2. this.saveBtnDisabled = true
  3. course.updateCourseInfoById(this.courseInfo).then(response => {
  4. this.$message({
  5. type: 'success',
  6. message: '修改成功!'
  7. })
  8. return response// 将响应结果传递给then
  9. }).then(response => {
  10. this.$router.push({ path: '/edu/course/chapter/' + response.data.courseId })
  11. }).catch((response) => {
  12. // console.log(response)
  13. this.$message({
  14. type: 'error',
  15. message: '保存失败'
  16. })
  17. })
  18. },