198 lines
6.3 KiB
Go
198 lines
6.3 KiB
Go
package learning
|
|
|
|
import (
|
|
"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 CourseApi struct{}
|
|
|
|
// CreateCourse 创建课程
|
|
// @Tags Course
|
|
// @Summary 创建课程
|
|
// @Security ApiKeyAuth
|
|
// @accept application/json
|
|
// @Produce application/json
|
|
// @Param data body learningReq.CourseCreate true "创建课程"
|
|
// @Success 200 {object} response.Response{msg=string} "创建成功"
|
|
// @Router /course/createCourse [post]
|
|
func (courseApi *CourseApi) CreateCourse(c *gin.Context) {
|
|
var req learningReq.CourseCreate
|
|
err := c.ShouldBindJSON(&req)
|
|
if err != nil {
|
|
response.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
course := learning.Course{
|
|
Title: req.Title,
|
|
Description: req.Description,
|
|
Cover: req.Cover,
|
|
Difficulty: req.Difficulty,
|
|
Duration: req.Duration,
|
|
Status: req.Status,
|
|
Price: req.Price,
|
|
Tags: req.Tags,
|
|
Sort: req.Sort,
|
|
}
|
|
|
|
err = service.ServiceGroupApp.LearningServiceGroup.CourseService.CreateCourse(c.Request.Context(), &course)
|
|
if err != nil {
|
|
global.GVA_LOG.Error("创建失败!", zap.Error(err))
|
|
response.FailWithMessage("创建失败", c)
|
|
return
|
|
}
|
|
response.OkWithMessage("创建成功", c)
|
|
}
|
|
|
|
// DeleteCourse 删除课程
|
|
// @Tags Course
|
|
// @Summary 删除课程
|
|
// @Security ApiKeyAuth
|
|
// @accept application/json
|
|
// @Produce application/json
|
|
// @Param data body request.IdsReq true "批量删除课程"
|
|
// @Success 200 {object} response.Response{msg=string} "删除成功"
|
|
// @Router /course/deleteCourse [delete]
|
|
func (courseApi *CourseApi) DeleteCourse(c *gin.Context) {
|
|
ID := c.Query("ID")
|
|
err := service.ServiceGroupApp.LearningServiceGroup.CourseService.DeleteCourse(c.Request.Context(), ID)
|
|
if err != nil {
|
|
global.GVA_LOG.Error("删除失败!", zap.Error(err))
|
|
response.FailWithMessage("删除失败", c)
|
|
return
|
|
}
|
|
response.OkWithMessage("删除成功", c)
|
|
}
|
|
|
|
// DeleteCourseByIds 批量删除课程
|
|
// @Tags Course
|
|
// @Summary 批量删除课程
|
|
// @Security ApiKeyAuth
|
|
// @accept application/json
|
|
// @Produce application/json
|
|
// @Success 200 {object} response.Response{msg=string} "批量删除成功"
|
|
// @Router /course/deleteCourseByIds [delete]
|
|
func (courseApi *CourseApi) DeleteCourseByIds(c *gin.Context) {
|
|
IDs := c.QueryArray("IDs[]")
|
|
err := service.ServiceGroupApp.LearningServiceGroup.CourseService.DeleteCourseByIds(c.Request.Context(), IDs)
|
|
if err != nil {
|
|
global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
|
|
response.FailWithMessage("批量删除失败", c)
|
|
return
|
|
}
|
|
response.OkWithMessage("批量删除成功", c)
|
|
}
|
|
|
|
// UpdateCourse 更新课程
|
|
// @Tags Course
|
|
// @Summary 更新课程
|
|
// @Security ApiKeyAuth
|
|
// @accept application/json
|
|
// @Produce application/json
|
|
// @Param data body learningReq.CourseUpdate true "更新课程"
|
|
// @Success 200 {object} response.Response{msg=string} "更新成功"
|
|
// @Router /course/updateCourse [put]
|
|
func (courseApi *CourseApi) UpdateCourse(c *gin.Context) {
|
|
var req learningReq.CourseUpdate
|
|
err := c.ShouldBindJSON(&req)
|
|
if err != nil {
|
|
response.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
course := learning.Course{
|
|
GVA_MODEL: global.GVA_MODEL{ID: req.ID},
|
|
Title: req.Title,
|
|
Description: req.Description,
|
|
Cover: req.Cover,
|
|
Difficulty: req.Difficulty,
|
|
Duration: req.Duration,
|
|
Status: req.Status,
|
|
Price: req.Price,
|
|
Tags: req.Tags,
|
|
Sort: req.Sort,
|
|
}
|
|
|
|
err = service.ServiceGroupApp.LearningServiceGroup.CourseService.UpdateCourse(c.Request.Context(), course)
|
|
if err != nil {
|
|
global.GVA_LOG.Error("更新失败!", zap.Error(err))
|
|
response.FailWithMessage("更新失败", c)
|
|
return
|
|
}
|
|
response.OkWithMessage("更新成功", c)
|
|
}
|
|
|
|
// FindCourse 用id查询课程
|
|
// @Tags Course
|
|
// @Summary 用id查询课程
|
|
// @Security ApiKeyAuth
|
|
// @accept application/json
|
|
// @Produce application/json
|
|
// @Param data query learning.Course true "用id查询课程"
|
|
// @Success 200 {object} response.Response{data=learning.Course,msg=string} "查询成功"
|
|
// @Router /course/findCourse [get]
|
|
func (courseApi *CourseApi) FindCourse(c *gin.Context) {
|
|
ID := c.Query("ID")
|
|
recourse, err := service.ServiceGroupApp.LearningServiceGroup.CourseService.GetCourse(c.Request.Context(), ID)
|
|
if err != nil {
|
|
global.GVA_LOG.Error("查询失败!", zap.Error(err))
|
|
response.FailWithMessage("查询失败", c)
|
|
return
|
|
}
|
|
response.OkWithData(recourse, c)
|
|
}
|
|
|
|
// GetCourseList 分页获取课程列表
|
|
// @Tags Course
|
|
// @Summary 分页获取课程列表
|
|
// @Security ApiKeyAuth
|
|
// @accept application/json
|
|
// @Produce application/json
|
|
// @Param data query learningReq.CourseSearch true "分页获取课程列表"
|
|
// @Success 200 {object} response.Response{data=response.PageResult,msg=string} "获取成功"
|
|
// @Router /course/getCourseList [get]
|
|
func (courseApi *CourseApi) GetCourseList(c *gin.Context) {
|
|
var pageInfo learningReq.CourseSearch
|
|
err := c.ShouldBindQuery(&pageInfo)
|
|
if err != nil {
|
|
response.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
list, total, err := service.ServiceGroupApp.LearningServiceGroup.CourseService.GetCourseInfoList(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)
|
|
}
|
|
|
|
// GetPublishedCourses 获取已发布的课程列表
|
|
// @Tags Course
|
|
// @Summary 获取已发布的课程列表
|
|
// @Security ApiKeyAuth
|
|
// @accept application/json
|
|
// @Produce application/json
|
|
// @Success 200 {object} response.Response{data=[]learning.Course,msg=string} "获取成功"
|
|
// @Router /course/getPublishedCourses [get]
|
|
func (courseApi *CourseApi) GetPublishedCourses(c *gin.Context) {
|
|
list, err := service.ServiceGroupApp.LearningServiceGroup.CourseService.GetPublishedCourses(c.Request.Context())
|
|
if err != nil {
|
|
global.GVA_LOG.Error("获取失败!", zap.Error(err))
|
|
response.FailWithMessage("获取失败", c)
|
|
return
|
|
}
|
|
response.OkWithData(list, c)
|
|
}
|