alist/server/controllers/meta.go

72 lines
1.5 KiB
Go
Raw Normal View History

2022-06-26 11:09:28 +00:00
package controllers
import (
"github.com/alist-org/alist/v3/internal/db"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/pkg/utils"
2022-06-26 11:20:19 +00:00
"github.com/alist-org/alist/v3/server/common"
2022-06-26 11:09:28 +00:00
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"strconv"
)
func ListMetas(c *gin.Context) {
2022-06-26 11:20:19 +00:00
var req common.PageReq
2022-06-26 11:09:28 +00:00
if err := c.ShouldBind(&req); err != nil {
2022-06-26 11:20:19 +00:00
common.ErrorResp(c, err, 400, true)
2022-06-26 11:09:28 +00:00
return
}
log.Debugf("%+v", req)
metas, total, err := db.GetMetas(req.PageIndex, req.PageSize)
if err != nil {
2022-06-26 11:20:19 +00:00
common.ErrorResp(c, err, 500)
2022-06-26 11:09:28 +00:00
return
}
2022-06-26 11:20:19 +00:00
common.SuccessResp(c, common.PageResp{
2022-06-26 11:09:28 +00:00
Content: metas,
Total: total,
})
}
func CreateMeta(c *gin.Context) {
var req model.Meta
if err := c.ShouldBind(&req); err != nil {
2022-06-26 11:20:19 +00:00
common.ErrorResp(c, err, 400, true)
2022-06-26 11:09:28 +00:00
return
}
req.Path = utils.StandardizePath(req.Path)
if err := db.CreateMeta(&req); err != nil {
2022-06-26 11:20:19 +00:00
common.ErrorResp(c, err, 500)
2022-06-26 11:09:28 +00:00
} else {
2022-06-26 11:20:19 +00:00
common.SuccessResp(c)
2022-06-26 11:09:28 +00:00
}
}
func UpdateMeta(c *gin.Context) {
var req model.Meta
if err := c.ShouldBind(&req); err != nil {
2022-06-26 11:20:19 +00:00
common.ErrorResp(c, err, 400, true)
2022-06-26 11:09:28 +00:00
return
}
req.Path = utils.StandardizePath(req.Path)
if err := db.UpdateMeta(&req); err != nil {
2022-06-26 11:20:19 +00:00
common.ErrorResp(c, err, 500)
2022-06-26 11:09:28 +00:00
} else {
2022-06-26 11:20:19 +00:00
common.SuccessResp(c)
2022-06-26 11:09:28 +00:00
}
}
func DeleteMeta(c *gin.Context) {
idStr := c.Query("id")
id, err := strconv.Atoi(idStr)
if err != nil {
2022-06-26 11:20:19 +00:00
common.ErrorResp(c, err, 400, true)
2022-06-26 11:09:28 +00:00
return
}
if err := db.DeleteMetaById(uint(id)); err != nil {
2022-06-26 11:20:19 +00:00
common.ErrorResp(c, err, 500)
2022-06-26 11:09:28 +00:00
return
}
2022-06-26 11:20:19 +00:00
common.SuccessResp(c)
2022-06-26 11:09:28 +00:00
}