mirror of
https://github.com/PlayEdu/PlayEdu
synced 2025-07-24 02:09:35 +08:00
课程管理
This commit is contained in:
parent
6b4d40d758
commit
3974f068e0
@ -0,0 +1,73 @@
|
||||
package xyz.playedu.api.controller.backend;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import xyz.playedu.api.PlayEduBackendThreadLocal;
|
||||
import xyz.playedu.api.domain.CourseChapter;
|
||||
import xyz.playedu.api.event.CourseChapterDestroyEvent;
|
||||
import xyz.playedu.api.exception.NotFoundException;
|
||||
import xyz.playedu.api.request.backend.CourseChapterRequest;
|
||||
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 杭州白书科技有限公司
|
||||
* @create 2023/2/26 17:28
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/backend/v1/course/{courseId}/chapter")
|
||||
public class CourseChapterController {
|
||||
|
||||
@Autowired
|
||||
private CourseChapterService chapterService;
|
||||
|
||||
@Autowired
|
||||
private ApplicationContext ctx;
|
||||
|
||||
@GetMapping("/index")
|
||||
public JsonResponse index(@PathVariable(name = "courseId") Integer courseId) {
|
||||
List<CourseChapter> chapters = chapterService.getChaptersByCourseId(courseId);
|
||||
return JsonResponse.data(chapters);
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
@PostMapping("/create")
|
||||
public JsonResponse store(@PathVariable(name = "courseId") Integer courseId, @RequestBody @Validated CourseChapterRequest req) {
|
||||
chapterService.create(courseId, req.getName(), req.getSort());
|
||||
return JsonResponse.success();
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public JsonResponse edit(@PathVariable(name = "courseId") Integer courseId, @PathVariable(name = "id") Integer id) throws NotFoundException {
|
||||
CourseChapter chapter = chapterService.findOrFail(id, courseId);
|
||||
return JsonResponse.data(chapter);
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public JsonResponse update(@PathVariable(name = "courseId") Integer courseId, @PathVariable(name = "id") Integer id, @RequestBody @Validated CourseChapterRequest req) throws NotFoundException {
|
||||
CourseChapter chapter = chapterService.findOrFail(id, courseId);
|
||||
chapterService.update(chapter, req.getName(), req.getSort());
|
||||
return JsonResponse.data(chapter);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public JsonResponse destroy(@PathVariable(name = "courseId") Integer courseId, @PathVariable(name = "id") Integer id) throws NotFoundException {
|
||||
CourseChapter chapter = chapterService.findOrFail(id, courseId);
|
||||
chapterService.removeById(chapter.getId());
|
||||
ctx.publishEvent(new CourseChapterDestroyEvent(this, PlayEduBackendThreadLocal.getAdminUserID(), chapter.getCourseId(), new Date()));
|
||||
return JsonResponse.success();
|
||||
}
|
||||
}
|
101
src/main/java/xyz/playedu/api/domain/CourseChapter.java
Normal file
101
src/main/java/xyz/playedu/api/domain/CourseChapter.java
Normal file
@ -0,0 +1,101 @@
|
||||
package xyz.playedu.api.domain;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
*
|
||||
* @TableName course_chapters
|
||||
*/
|
||||
@TableName(value ="course_chapters")
|
||||
@Data
|
||||
public class CourseChapter implements Serializable {
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@TableId(type = IdType.AUTO)
|
||||
private Integer id;
|
||||
|
||||
/**
|
||||
* 课程ID
|
||||
*/
|
||||
private Integer courseId;
|
||||
|
||||
/**
|
||||
* 章节名
|
||||
*/
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* 升序
|
||||
*/
|
||||
private Integer sort;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private Date createdAt;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private Date updatedAt;
|
||||
|
||||
@TableField(exist = false)
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
public boolean equals(Object that) {
|
||||
if (this == that) {
|
||||
return true;
|
||||
}
|
||||
if (that == null) {
|
||||
return false;
|
||||
}
|
||||
if (getClass() != that.getClass()) {
|
||||
return false;
|
||||
}
|
||||
CourseChapter other = (CourseChapter) that;
|
||||
return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId()))
|
||||
&& (this.getCourseId() == null ? other.getCourseId() == null : this.getCourseId().equals(other.getCourseId()))
|
||||
&& (this.getName() == null ? other.getName() == null : this.getName().equals(other.getName()))
|
||||
&& (this.getSort() == null ? other.getSort() == null : this.getSort().equals(other.getSort()))
|
||||
&& (this.getCreatedAt() == null ? other.getCreatedAt() == null : this.getCreatedAt().equals(other.getCreatedAt()))
|
||||
&& (this.getUpdatedAt() == null ? other.getUpdatedAt() == null : this.getUpdatedAt().equals(other.getUpdatedAt()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((getId() == null) ? 0 : getId().hashCode());
|
||||
result = prime * result + ((getCourseId() == null) ? 0 : getCourseId().hashCode());
|
||||
result = prime * result + ((getName() == null) ? 0 : getName().hashCode());
|
||||
result = prime * result + ((getSort() == null) ? 0 : getSort().hashCode());
|
||||
result = prime * result + ((getCreatedAt() == null) ? 0 : getCreatedAt().hashCode());
|
||||
result = prime * result + ((getUpdatedAt() == null) ? 0 : getUpdatedAt().hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(getClass().getSimpleName());
|
||||
sb.append(" [");
|
||||
sb.append("Hash = ").append(hashCode());
|
||||
sb.append(", id=").append(id);
|
||||
sb.append(", courseId=").append(courseId);
|
||||
sb.append(", name=").append(name);
|
||||
sb.append(", sort=").append(sort);
|
||||
sb.append(", createdAt=").append(createdAt);
|
||||
sb.append(", updatedAt=").append(updatedAt);
|
||||
sb.append(", serialVersionUID=").append(serialVersionUID);
|
||||
sb.append("]");
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package xyz.playedu.api.event;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @Author 杭州白书科技有限公司
|
||||
* @create 2023/2/26 17:42
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
public class CourseChapterDestroyEvent extends ApplicationEvent {
|
||||
private Integer adminId;
|
||||
private Integer chapterId;
|
||||
private Date date;
|
||||
|
||||
public CourseChapterDestroyEvent(Object source, Integer adminId, Integer chapterId, Date date) {
|
||||
super(source);
|
||||
this.adminId = adminId;
|
||||
this.chapterId = chapterId;
|
||||
this.date = date;
|
||||
}
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package xyz.playedu.api.mapper;
|
||||
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import xyz.playedu.api.domain.CourseChapter;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
|
||||
/**
|
||||
* @author tengteng
|
||||
* @description 针对表【course_chapters】的数据库操作Mapper
|
||||
* @createDate 2023-02-26 17:34:01
|
||||
* @Entity xyz.playedu.api.domain.CourseChapter
|
||||
*/
|
||||
@Mapper
|
||||
public interface CourseChapterMapper extends BaseMapper<CourseChapter> {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@ -0,0 +1,23 @@
|
||||
package xyz.playedu.api.request.backend;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.Data;
|
||||
import org.hibernate.validator.constraints.Length;
|
||||
|
||||
/**
|
||||
* @Author 杭州白书科技有限公司
|
||||
* @create 2023/2/26 17:36
|
||||
*/
|
||||
@Data
|
||||
public class CourseChapterRequest {
|
||||
|
||||
@NotBlank(message = "请输入章节名")
|
||||
@NotNull(message = "name参数不存在")
|
||||
@Length(min = 1, max = 64, message = "章节名长度在1-64个字符之间")
|
||||
private String name;
|
||||
|
||||
@NotNull(message = "sort参数不存在")
|
||||
private Integer sort;
|
||||
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package xyz.playedu.api.service;
|
||||
|
||||
import xyz.playedu.api.domain.CourseChapter;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import xyz.playedu.api.exception.NotFoundException;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author tengteng
|
||||
* @description 针对表【course_chapters】的数据库操作Service
|
||||
* @createDate 2023-02-26 17:30:19
|
||||
*/
|
||||
public interface CourseChapterService extends IService<CourseChapter> {
|
||||
|
||||
List<CourseChapter> getChaptersByCourseId(Integer courseId);
|
||||
|
||||
void create(Integer courseId, String name, Integer sort);
|
||||
|
||||
void update(CourseChapter chapter, String name, Integer sort);
|
||||
|
||||
CourseChapter findOrFail(Integer id) throws NotFoundException;
|
||||
|
||||
CourseChapter findOrFail(Integer id, Integer courseId) throws NotFoundException;
|
||||
|
||||
}
|
@ -0,0 +1,68 @@
|
||||
package xyz.playedu.api.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import xyz.playedu.api.domain.CourseChapter;
|
||||
import xyz.playedu.api.exception.NotFoundException;
|
||||
import xyz.playedu.api.service.CourseChapterService;
|
||||
import xyz.playedu.api.mapper.CourseChapterMapper;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author tengteng
|
||||
* @description 针对表【course_chapters】的数据库操作Service实现
|
||||
* @createDate 2023-02-26 17:30:18
|
||||
*/
|
||||
@Service
|
||||
public class CourseChapterServiceImpl extends ServiceImpl<CourseChapterMapper, CourseChapter>
|
||||
implements CourseChapterService {
|
||||
|
||||
@Override
|
||||
public void create(Integer courseId, String name, Integer sort) {
|
||||
CourseChapter chapter = new CourseChapter();
|
||||
chapter.setCourseId(courseId);
|
||||
chapter.setName(name);
|
||||
chapter.setSort(sort);
|
||||
chapter.setCreatedAt(new Date());
|
||||
chapter.setUpdatedAt(new Date());
|
||||
save(chapter);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(CourseChapter chapter, String name, Integer sort) {
|
||||
CourseChapter newChapter = new CourseChapter();
|
||||
newChapter.setId(chapter.getId());
|
||||
newChapter.setName(name);
|
||||
newChapter.setSort(sort);
|
||||
updateById(newChapter);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CourseChapter findOrFail(Integer id) throws NotFoundException {
|
||||
CourseChapter chapter = getOne(query().getWrapper().eq("id", id));
|
||||
if (chapter == null) {
|
||||
throw new NotFoundException("章节不存在");
|
||||
}
|
||||
return chapter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<CourseChapter> getChaptersByCourseId(Integer courseId) {
|
||||
return list(query().getWrapper().eq("course_id", courseId).orderByAsc("sort"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public CourseChapter findOrFail(Integer id, Integer courseId) throws NotFoundException {
|
||||
CourseChapter chapter = getOne(query().getWrapper().eq("id", id).eq("course_id", courseId));
|
||||
if (chapter == null) {
|
||||
throw new NotFoundException("章节不存在");
|
||||
}
|
||||
return chapter;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
20
src/main/resources/mapper/CourseChapterMapper.xml
Normal file
20
src/main/resources/mapper/CourseChapterMapper.xml
Normal file
@ -0,0 +1,20 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="xyz.playedu.api.mapper.CourseChapterMapper">
|
||||
|
||||
<resultMap id="BaseResultMap" type="xyz.playedu.api.domain.CourseChapter">
|
||||
<id property="id" column="id" jdbcType="INTEGER"/>
|
||||
<result property="courseId" column="course_id" jdbcType="INTEGER"/>
|
||||
<result property="name" column="name" jdbcType="VARCHAR"/>
|
||||
<result property="sort" column="sort" jdbcType="INTEGER"/>
|
||||
<result property="createdAt" column="created_at" jdbcType="TIMESTAMP"/>
|
||||
<result property="updatedAt" column="updated_at" jdbcType="TIMESTAMP"/>
|
||||
</resultMap>
|
||||
|
||||
<sql id="Base_Column_List">
|
||||
id,course_id,name,
|
||||
sort,created_at,updated_at
|
||||
</sql>
|
||||
</mapper>
|
Loading…
x
Reference in New Issue
Block a user