Study/server/api/v1/learning/chapter.go

201 lines
6.4 KiB
Go

package learning
import (
"strconv"
"github.com/flipped-aurora/gin-vue-admin/server/global"
"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
"github.com/flipped-aurora/gin-vue-admin/server/model/learning"
learningReq "github.com/flipped-aurora/gin-vue-admin/server/model/learning/request"
"github.com/flipped-aurora/gin-vue-admin/server/service"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
)
type ChapterApi struct{}
// CreateChapter 创建章节
// @Tags Chapter
// @Summary 创建章节
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body learningReq.ChapterCreate true "创建章节"
// @Success 200 {object} response.Response{msg=string} "创建成功"
// @Router /chapter/createChapter [post]
func (chapterApi *ChapterApi) CreateChapter(c *gin.Context) {
var req learningReq.ChapterCreate
err := c.ShouldBindJSON(&req)
if err != nil {
response.FailWithMessage(err.Error(), c)
return
}
chapter := learning.Chapter{
CourseId: req.CourseId,
Title: req.Title,
Description: req.Description,
Sort: req.Sort,
Status: req.Status,
Duration: req.Duration,
}
err = service.ServiceGroupApp.LearningServiceGroup.ChapterService.CreateChapter(c.Request.Context(), &chapter)
if err != nil {
global.GVA_LOG.Error("创建失败!", zap.Error(err))
response.FailWithMessage("创建失败", c)
return
}
response.OkWithMessage("创建成功", c)
}
// DeleteChapter 删除章节
// @Tags Chapter
// @Summary 删除章节
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除章节"
// @Success 200 {object} response.Response{msg=string} "删除成功"
// @Router /chapter/deleteChapter [delete]
func (chapterApi *ChapterApi) DeleteChapter(c *gin.Context) {
ID := c.Query("ID")
err := service.ServiceGroupApp.LearningServiceGroup.ChapterService.DeleteChapter(c.Request.Context(), ID)
if err != nil {
global.GVA_LOG.Error("删除失败!", zap.Error(err))
response.FailWithMessage("删除失败", c)
return
}
response.OkWithMessage("删除成功", c)
}
// DeleteChapterByIds 批量删除章节
// @Tags Chapter
// @Summary 批量删除章节
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{msg=string} "批量删除成功"
// @Router /chapter/deleteChapterByIds [delete]
func (chapterApi *ChapterApi) DeleteChapterByIds(c *gin.Context) {
IDs := c.QueryArray("IDs[]")
err := service.ServiceGroupApp.LearningServiceGroup.ChapterService.DeleteChapterByIds(c.Request.Context(), IDs)
if err != nil {
global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
response.FailWithMessage("批量删除失败", c)
return
}
response.OkWithMessage("批量删除成功", c)
}
// UpdateChapter 更新章节
// @Tags Chapter
// @Summary 更新章节
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body learningReq.ChapterUpdate true "更新章节"
// @Success 200 {object} response.Response{msg=string} "更新成功"
// @Router /chapter/updateChapter [put]
func (chapterApi *ChapterApi) UpdateChapter(c *gin.Context) {
var req learningReq.ChapterUpdate
err := c.ShouldBindJSON(&req)
if err != nil {
response.FailWithMessage(err.Error(), c)
return
}
chapter := learning.Chapter{
GVA_MODEL: global.GVA_MODEL{ID: req.ID},
CourseId: req.CourseId,
Title: req.Title,
Description: req.Description,
Sort: req.Sort,
Status: req.Status,
Duration: req.Duration,
}
err = service.ServiceGroupApp.LearningServiceGroup.ChapterService.UpdateChapter(c.Request.Context(), chapter)
if err != nil {
global.GVA_LOG.Error("更新失败!", zap.Error(err))
response.FailWithMessage("更新失败", c)
return
}
response.OkWithMessage("更新成功", c)
}
// FindChapter 用id查询章节
// @Tags Chapter
// @Summary 用id查询章节
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query learning.Chapter true "用id查询章节"
// @Success 200 {object} response.Response{data=learning.Chapter,msg=string} "查询成功"
// @Router /chapter/findChapter [get]
func (chapterApi *ChapterApi) FindChapter(c *gin.Context) {
ID := c.Query("ID")
rechapter, err := service.ServiceGroupApp.LearningServiceGroup.ChapterService.GetChapter(c.Request.Context(), ID)
if err != nil {
global.GVA_LOG.Error("查询失败!", zap.Error(err))
response.FailWithMessage("查询失败", c)
return
}
response.OkWithData(rechapter, c)
}
// GetChapterList 分页获取章节列表
// @Tags Chapter
// @Summary 分页获取章节列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query learningReq.ChapterSearch true "分页获取章节列表"
// @Success 200 {object} response.Response{data=response.PageResult,msg=string} "获取成功"
// @Router /chapter/getChapterList [get]
func (chapterApi *ChapterApi) GetChapterList(c *gin.Context) {
var pageInfo learningReq.ChapterSearch
err := c.ShouldBindQuery(&pageInfo)
if err != nil {
response.FailWithMessage(err.Error(), c)
return
}
list, total, err := service.ServiceGroupApp.LearningServiceGroup.ChapterService.GetChapterInfoList(c.Request.Context(), pageInfo)
if err != nil {
global.GVA_LOG.Error("获取失败!", zap.Error(err))
response.FailWithMessage("获取失败", c)
return
}
response.OkWithDetailed(response.PageResult{
List: list,
Total: total,
Page: pageInfo.Page,
PageSize: pageInfo.PageSize,
}, "获取成功", c)
}
// GetChaptersByCourse 根据课程获取章节列表
// @Tags Chapter
// @Summary 根据课程获取章节列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param courseId query uint true "课程ID"
// @Success 200 {object} response.Response{data=[]learning.Chapter,msg=string} "获取成功"
// @Router /chapter/getChaptersByCourse [get]
func (chapterApi *ChapterApi) GetChaptersByCourse(c *gin.Context) {
courseIdStr := c.Query("courseId")
courseId, err := strconv.ParseUint(courseIdStr, 10, 32)
if err != nil {
response.FailWithMessage("课程ID格式错误", c)
return
}
list, err := service.ServiceGroupApp.LearningServiceGroup.ChapterService.GetChaptersByCourse(c.Request.Context(), uint(courseId))
if err != nil {
global.GVA_LOG.Error("获取失败!", zap.Error(err))
response.FailWithMessage("获取失败", c)
return
}
response.OkWithData(list, c)
}