mirror of https://github.com/Xhofe/alist
100 lines
2.1 KiB
Go
100 lines
2.1 KiB
Go
package handles
|
|
|
|
import (
|
|
"errors"
|
|
"github.com/alist-org/alist/v3/internal/db"
|
|
"github.com/alist-org/alist/v3/internal/model"
|
|
"github.com/alist-org/alist/v3/internal/op"
|
|
"github.com/alist-org/alist/v3/server/common"
|
|
"github.com/gin-gonic/gin"
|
|
log "github.com/sirupsen/logrus"
|
|
"strconv"
|
|
)
|
|
|
|
func ListLabel(c *gin.Context) {
|
|
var req model.PageReq
|
|
if err := c.ShouldBind(&req); err != nil {
|
|
common.ErrorResp(c, err, 400)
|
|
return
|
|
}
|
|
req.Validate()
|
|
log.Debugf("%+v", req)
|
|
labels, total, err := db.GetLabels(req.Page, req.PerPage)
|
|
if err != nil {
|
|
common.ErrorResp(c, err, 500)
|
|
return
|
|
}
|
|
common.SuccessResp(c, common.PageResp{
|
|
Content: labels,
|
|
Total: total,
|
|
})
|
|
}
|
|
|
|
func GetLabel(c *gin.Context) {
|
|
idStr := c.Query("id")
|
|
id, err := strconv.Atoi(idStr)
|
|
if err != nil {
|
|
common.ErrorResp(c, err, 400)
|
|
return
|
|
}
|
|
label, err := db.GetLabelById(uint(id))
|
|
if err != nil {
|
|
common.ErrorResp(c, err, 500, true)
|
|
return
|
|
}
|
|
common.SuccessResp(c, label)
|
|
}
|
|
|
|
func CreateLabel(c *gin.Context) {
|
|
var req model.Label
|
|
if err := c.ShouldBind(&req); err != nil {
|
|
common.ErrorResp(c, err, 400)
|
|
return
|
|
}
|
|
if db.GetLabelByName(req.Name) {
|
|
common.ErrorResp(c, errors.New("label name is exists"), 401)
|
|
return
|
|
}
|
|
if id, err := db.CreateLabel(req); err != nil {
|
|
common.ErrorWithDataResp(c, err, 500, gin.H{
|
|
"id": id,
|
|
}, true)
|
|
} else {
|
|
common.SuccessResp(c, gin.H{
|
|
"id": id,
|
|
})
|
|
}
|
|
}
|
|
|
|
func UpdateLabel(c *gin.Context) {
|
|
var req model.Label
|
|
if err := c.ShouldBind(&req); err != nil {
|
|
common.ErrorResp(c, err, 400)
|
|
return
|
|
}
|
|
if label, err := db.UpdateLabel(&req); err != nil {
|
|
common.ErrorResp(c, err, 500, true)
|
|
} else {
|
|
common.SuccessResp(c, label)
|
|
}
|
|
}
|
|
|
|
func DeleteLabel(c *gin.Context) {
|
|
idStr := c.Query("id")
|
|
id, err := strconv.Atoi(idStr)
|
|
if err != nil {
|
|
common.ErrorResp(c, err, 400)
|
|
return
|
|
}
|
|
userObj, ok := c.Value("user").(*model.User)
|
|
if !ok {
|
|
common.ErrorStrResp(c, "user invalid", 401)
|
|
return
|
|
}
|
|
if err = op.DeleteLabelById(c, uint(id), userObj.ID); err != nil {
|
|
common.ErrorResp(c, err, 500, true)
|
|
return
|
|
}
|
|
common.SuccessResp(c)
|
|
}
|