优化课程创建

This commit is contained in:
none
2023-03-15 10:40:33 +08:00
parent fb94e93f80
commit cca6d050bc
13 changed files with 213 additions and 136 deletions

View File

@@ -15,8 +15,6 @@ import xyz.playedu.api.service.CourseChapterService;
import xyz.playedu.api.types.JsonResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
/**
* @Author 杭州白书科技有限公司
@@ -32,21 +30,6 @@ public class CourseChapterController {
@Autowired
private ApplicationContext ctx;
@GetMapping("/index")
public JsonResponse index(@PathVariable(name = "courseId") Integer courseId) {
List<CourseChapter> chapters = chapterService.getChaptersByCourseId(courseId);
return JsonResponse.data(chapters);
}
@BackendPermissionMiddleware(slug = BPermissionConstant.COURSE)
@GetMapping("/create")
public JsonResponse create(@PathVariable(name = "courseId") Integer courseId) {
List<CourseChapter> chapters = chapterService.getChaptersByCourseId(courseId);
HashMap<String, Object> data = new HashMap<>();
data.put("chapters", chapters);
return JsonResponse.data(data);
}
@BackendPermissionMiddleware(slug = BPermissionConstant.COURSE)
@PostMapping("/create")
public JsonResponse store(@PathVariable(name = "courseId") Integer courseId, @RequestBody @Validated CourseChapterRequest req) {

View File

@@ -14,12 +14,15 @@ import xyz.playedu.api.event.CourseDestroyEvent;
import xyz.playedu.api.exception.NotFoundException;
import xyz.playedu.api.middleware.BackendPermissionMiddleware;
import xyz.playedu.api.request.backend.CourseRequest;
import xyz.playedu.api.service.CourseChapterService;
import xyz.playedu.api.service.CourseHourService;
import xyz.playedu.api.service.CourseService;
import xyz.playedu.api.service.ResourceCategoryService;
import xyz.playedu.api.types.JsonResponse;
import xyz.playedu.api.types.paginate.CoursePaginateFiler;
import xyz.playedu.api.types.paginate.PaginationResult;
import java.text.ParseException;
import java.util.*;
import java.util.stream.Collectors;
@@ -38,6 +41,12 @@ public class CourseController {
@Autowired
private ResourceCategoryService categoryService;
@Autowired
private CourseChapterService chapterService;
@Autowired
private CourseHourService hourService;
@Autowired
private ApplicationContext ctx;
@@ -74,8 +83,79 @@ public class CourseController {
@BackendPermissionMiddleware(slug = BPermissionConstant.COURSE)
@PostMapping("/create")
@Transactional
public JsonResponse store(@RequestBody @Validated CourseRequest req) {
courseService.createWithCategoryIdsAndDepIds(req.getTitle(), req.getThumb(), req.getIsShow(), req.getCategoryIds(), req.getDepIds());
public JsonResponse store(@RequestBody @Validated CourseRequest req) throws ParseException {
Course course = courseService.createWithCategoryIdsAndDepIds(req.getTitle(), req.getThumb(), req.getIsShow(), req.getCategoryIds(), req.getDepIds());
Map<String, Map<String, Object>[]> chapters = req.getChapters();
Date now = new Date();
if (req.getHours() != null) {//无章节课时配置
List<CourseHour> insertHours = new ArrayList<>();
for (Map<String, Object> hourItem : req.getHours()) {
// 资源类型
String typeVal = MapUtils.getString(hourItem, "type");
// 时长
Integer durationVal = MapUtils.getInteger(hourItem, "duration");
// 资源ID
Integer ridVal = MapUtils.getInteger(hourItem, "rid");
insertHours.add(new CourseHour() {{
setChapterId(0);
setCourseId(course.getId());
setType(typeVal);
setDuration(durationVal);
setRid(ridVal);
setCreatedAt(now);
}});
}
if (insertHours.size() > 0) {
hourService.saveBatch(insertHours);
}
} else {
if (chapters == null) {
return JsonResponse.error("请配置课时");
}
List<CourseHour> insertHours = new ArrayList<>();
final Integer[] chapterSort = {0};
for (Map.Entry<String, Map<String, Object>[]> entry : chapters.entrySet()) {
String chapterName = entry.getKey();
Map<String, Object>[] hoursList = entry.getValue();
CourseChapter tmpChapter = new CourseChapter() {{
setCourseId(course.getId());
setSort(chapterSort[0]++);
setName(chapterName);
setCreatedAt(now);
setUpdatedAt(now);
}};
chapterService.save(tmpChapter);
final Integer[] hourSort = {0};
for (Map<String, Object> hourItem : hoursList) {
// 资源类型
String typeVal = MapUtils.getString(hourItem, "type");
// 时长
Integer durationVal = MapUtils.getInteger(hourItem, "duration");
// 资源ID
Integer ridVal = MapUtils.getInteger(hourItem, "rid");
insertHours.add(new CourseHour() {{
setChapterId(tmpChapter.getId());
setCourseId(course.getId());
setType(typeVal);
setDuration(durationVal);
setRid(ridVal);
setCreatedAt(now);
setSort(hourSort[0]++);
}});
}
}
if (insertHours.size() > 0) {
hourService.saveBatch(insertHours);
}
}
return JsonResponse.success();
}
@@ -85,11 +165,15 @@ public class CourseController {
Course course = courseService.findOrFail(id);
List<Integer> depIds = courseService.getDepIdsByCourseId(course.getId());
List<Integer> categoryIds = courseService.getCategoryIdsByCourseId(course.getId());
List<CourseChapter> chapters = chapterService.getChaptersByCourseId(course.getId());
List<CourseHour> hours = hourService.getHoursByCourseId(course.getId());
HashMap<String, Object> data = new HashMap<>();
data.put("course", course);
data.put("dep_ids", depIds);
data.put("category_ids", categoryIds);
data.put("dep_ids", depIds);//已关联的部门
data.put("category_ids", categoryIds);//已关联的分类
data.put("chapters", chapters);
data.put("hours", hours.stream().collect(Collectors.toMap(CourseHour::getChapterId, e -> e)));
return JsonResponse.data(data);
}

View File

@@ -38,12 +38,6 @@ public class CourseHourController {
@Autowired
private ApplicationContext ctx;
@GetMapping("/index")
public JsonResponse index(@PathVariable(name = "courseId") Integer courseId) {
List<CourseHour> hours = hourService.getHoursByCourseId(courseId);
return JsonResponse.data(hours);
}
@BackendPermissionMiddleware(slug = BPermissionConstant.COURSE)
@GetMapping("/create")
public JsonResponse create(@PathVariable(name = "courseId") Integer courseId) {
@@ -79,7 +73,7 @@ public class CourseHourController {
Integer chapterId = req.getChapterId();
chapterService.findOrFail(chapterId, courseId);
CourseHour courseHour = hourService.create(courseId, chapterId, req.getTitle(), type, req.getDuration(), req.getPublishedAt());
CourseHour courseHour = hourService.create(courseId, chapterId, req.getSort(), req.getTitle(), type, req.getRid(), req.getDuration());
ctx.publishEvent(new CourseHourCreatedEvent(this, PlayEduBContext.getAdminUserID(), courseHour.getCourseId(), courseHour.getChapterId(), courseHour.getId(), new Date()));
return JsonResponse.success();
}
@@ -99,7 +93,7 @@ public class CourseHourController {
Integer chapterId = req.getChapterId();
chapterService.findOrFail(chapterId, courseId);
hourService.update(courseHour, chapterId, req.getTitle(), req.getDuration(), req.getPublishedAt());
hourService.update(courseHour, chapterId, req.getSort(), req.getTitle(), req.getDuration());
return JsonResponse.success();
}