format Server API

This commit is contained in:
mubai
2024-02-26 22:59:04 +08:00
parent 93b9855f52
commit 6709720707
17 changed files with 167 additions and 430 deletions

View File

@@ -4,7 +4,6 @@ import (
"errors"
"fmt"
"github.com/gin-gonic/gin"
"net/http"
"server/logic"
"server/model/system"
"server/plugin/spider"
@@ -17,41 +16,25 @@ import (
func FilmCronTaskList(c *gin.Context) {
tl := logic.CL.GetFilmCrontab()
if len(tl) <= 0 {
c.JSON(http.StatusOK, gin.H{
"status": StatusFailed,
"message": "暂无任务定时任务信息",
})
system.Failed("暂无任务定时任务信息", c)
return
}
c.JSON(http.StatusOK, gin.H{
"status": StatusOk,
"data": tl,
})
system.Success(tl, "定时任务列表获取成功!!!", c)
}
// GetFilmCronTask 通过Id获取对应的定时任务信息
func GetFilmCronTask(c *gin.Context) {
id := c.DefaultQuery("id", "")
if id == "" {
c.JSON(http.StatusOK, gin.H{
"status": StatusFailed,
"message": "定时任务信息获取失败,任务Id不能为空",
})
system.Failed("定时任务信息获取失败,任务Id不能为空", c)
return
}
task, err := logic.CL.GetFilmCrontabById(id)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"status": StatusFailed,
"message": fmt.Sprint("定时任务信息获取失败", err.Error()),
})
system.Failed(fmt.Sprint("定时任务信息获取失败", err.Error()), c)
return
}
c.JSON(http.StatusOK, gin.H{
"status": StatusOk,
"data": task,
})
system.Success(task, "定时任务详情获取成功!!!", c)
}
// FilmCronAdd 添加定时任务
@@ -59,35 +42,22 @@ func FilmCronAdd(c *gin.Context) {
var vo = system.FilmCronVo{}
// 获取请求参数
if err := c.ShouldBindJSON(&vo); err != nil {
c.JSON(http.StatusOK, gin.H{
"status": StatusFailed,
"message": "请求参数异常",
})
system.Failed("请求参数异常!!!", c)
return
}
// 校验请求参数
if err := validTaskAddVo(vo); err != nil {
c.JSON(http.StatusOK, gin.H{
"status": StatusFailed,
"message": err.Error(),
})
system.Failed(err.Error(), c)
return
}
// 去除cron表达式左右空格
vo.Spec = strings.TrimSpace(vo.Spec)
// 执行 定时任务信息保存逻辑
if err := logic.CL.AddFilmCrontab(vo); err != nil {
c.JSON(http.StatusOK, gin.H{
"status": StatusFailed,
"message": fmt.Sprint("定时任务添加失败: ", err.Error()),
})
system.Failed(fmt.Sprint("定时任务添加失败: ", err.Error()), c)
return
}
c.JSON(http.StatusOK, gin.H{
"status": StatusOk,
"message": "定时任务添加成功",
})
system.SuccessOnlyMsg("定时任务添加成功", c)
}
// FilmCronUpdate 更新定时任务信息
@@ -95,40 +65,28 @@ func FilmCronUpdate(c *gin.Context) {
var t = system.FilmCollectTask{}
// 获取请求参数
if err := c.ShouldBindJSON(&t); err != nil {
c.JSON(http.StatusOK, gin.H{
"status": StatusFailed,
"message": "请求参数异常",
})
system.Failed("请求参数异常!!!", c)
return
}
// 校验必要参数
if err := validTaskInfo(t); err != nil {
c.JSON(http.StatusOK, gin.H{
"status": StatusFailed,
"message": err.Error(),
})
system.Failed(err.Error(), c)
return
}
// 获取未更新的task信息
task, err := logic.CL.GetFilmCrontabById(t.Id)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"status": StatusFailed,
"message": fmt.Sprint("更新失败: ", err.Error()),
})
system.Failed(fmt.Sprint("更新失败: ", err.Error()), c)
return
}
// 将task的可变更属性进行
// 将task的可变更属性进行更
task.Ids = t.Ids
task.Time = t.Time
task.State = t.State
task.Remark = t.Remark
// 将变更后的task更新到系统中
logic.CL.UpdateFilmCron(task)
c.JSON(http.StatusOK, gin.H{
"status": StatusOk,
"message": fmt.Sprintf("定时任务[%s]更新成功", task.Id),
})
system.SuccessOnlyMsg(fmt.Sprintf("定时任务[%s]更新成功", task.Id), c)
}
// ChangeTaskState 开启 | 关闭Id 对应的定时任务
@@ -136,54 +94,35 @@ func ChangeTaskState(c *gin.Context) {
var t = system.FilmCollectTask{}
// 获取请求参数
if err := c.ShouldBindJSON(&t); err != nil {
c.JSON(http.StatusOK, gin.H{
"status": StatusFailed,
"message": "请求参数异常",
})
system.Failed("请求参数异常!!!", c)
return
}
// 获取未更新的task信息
task, err := logic.CL.GetFilmCrontabById(t.Id)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"status": StatusFailed,
"message": fmt.Sprint("更新失败: ", err.Error()),
})
system.Failed(fmt.Sprint("更新失败: ", err.Error()), c)
return
}
// 修改task的状态
task.State = t.State
// 将变更后的task更新到系统中
logic.CL.UpdateFilmCron(task)
c.JSON(http.StatusOK, gin.H{
"status": StatusOk,
"message": fmt.Sprintf("定时任务[%s]更新成功", task.Id),
})
system.SuccessOnlyMsg(fmt.Sprintf("定时任务[%s]更新成功", task.Id), c)
}
// DelFilmCron 删除定时任务
func DelFilmCron(c *gin.Context) {
id := c.DefaultQuery("id", "")
if id == "" {
c.JSON(http.StatusOK, gin.H{
"status": StatusFailed,
"message": "删除失败,任务Id不能为空",
})
system.Failed("定时任务清除失败, 任务ID不能为空", c)
return
}
// 如果Id不为空则执行删除逻辑
if err := logic.CL.DelFilmCrontab(id); err != nil {
c.JSON(http.StatusOK, gin.H{
"status": StatusFailed,
"message": err.Error(),
})
system.Failed(err.Error(), c)
return
}
c.JSON(http.StatusOK, gin.H{
"status": StatusOk,
"message": fmt.Sprintf("定时任务[%s]已删除", id),
})
system.SuccessOnlyMsg(fmt.Sprintf("定时任务[%s]已删除", id), c)
}
// -------------------------------------------------- 参数校验 --------------------------------------------------