2022-07-11 09:12:50 +00:00
|
|
|
package handles
|
2022-06-26 12:00:36 +00:00
|
|
|
|
|
|
|
import (
|
2022-06-28 10:12:53 +00:00
|
|
|
"strconv"
|
|
|
|
|
2022-06-26 12:00:36 +00:00
|
|
|
"github.com/alist-org/alist/v3/internal/db"
|
|
|
|
"github.com/alist-org/alist/v3/internal/model"
|
|
|
|
"github.com/alist-org/alist/v3/internal/operations"
|
|
|
|
"github.com/alist-org/alist/v3/server/common"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2022-07-10 06:45:39 +00:00
|
|
|
func ListStorages(c *gin.Context) {
|
2022-06-26 12:00:36 +00:00
|
|
|
var req common.PageReq
|
|
|
|
if err := c.ShouldBind(&req); err != nil {
|
2022-06-28 10:12:53 +00:00
|
|
|
common.ErrorResp(c, err, 400)
|
2022-06-26 12:00:36 +00:00
|
|
|
return
|
|
|
|
}
|
2022-07-12 10:41:16 +00:00
|
|
|
req.Validate()
|
2022-06-26 12:00:36 +00:00
|
|
|
log.Debugf("%+v", req)
|
2022-07-10 06:45:39 +00:00
|
|
|
storages, total, err := db.GetStorages(req.PageIndex, req.PageSize)
|
2022-06-26 12:00:36 +00:00
|
|
|
if err != nil {
|
|
|
|
common.ErrorResp(c, err, 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
common.SuccessResp(c, common.PageResp{
|
2022-07-10 06:45:39 +00:00
|
|
|
Content: storages,
|
2022-06-26 12:00:36 +00:00
|
|
|
Total: total,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-07-10 06:45:39 +00:00
|
|
|
func CreateStorage(c *gin.Context) {
|
|
|
|
var req model.Storage
|
2022-06-26 12:00:36 +00:00
|
|
|
if err := c.ShouldBind(&req); err != nil {
|
2022-06-28 10:12:53 +00:00
|
|
|
common.ErrorResp(c, err, 400)
|
2022-06-26 12:00:36 +00:00
|
|
|
return
|
|
|
|
}
|
2022-07-10 06:45:39 +00:00
|
|
|
if err := operations.CreateStorage(c, req); err != nil {
|
2022-06-28 10:12:53 +00:00
|
|
|
common.ErrorResp(c, err, 500, true)
|
2022-06-26 12:00:36 +00:00
|
|
|
} else {
|
|
|
|
common.SuccessResp(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-10 06:45:39 +00:00
|
|
|
func UpdateStorage(c *gin.Context) {
|
|
|
|
var req model.Storage
|
2022-06-26 12:00:36 +00:00
|
|
|
if err := c.ShouldBind(&req); err != nil {
|
2022-06-28 10:12:53 +00:00
|
|
|
common.ErrorResp(c, err, 400)
|
2022-06-26 12:00:36 +00:00
|
|
|
return
|
|
|
|
}
|
2022-07-10 06:45:39 +00:00
|
|
|
if err := operations.UpdateStorage(c, req); err != nil {
|
2022-06-28 10:12:53 +00:00
|
|
|
common.ErrorResp(c, err, 500, true)
|
2022-06-26 12:00:36 +00:00
|
|
|
} else {
|
|
|
|
common.SuccessResp(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-10 06:45:39 +00:00
|
|
|
func DeleteStorage(c *gin.Context) {
|
2022-06-26 12:00:36 +00:00
|
|
|
idStr := c.Query("id")
|
|
|
|
id, err := strconv.Atoi(idStr)
|
|
|
|
if err != nil {
|
2022-06-28 10:12:53 +00:00
|
|
|
common.ErrorResp(c, err, 400)
|
2022-06-26 12:00:36 +00:00
|
|
|
return
|
|
|
|
}
|
2022-07-10 06:45:39 +00:00
|
|
|
if err := operations.DeleteStorageById(c, uint(id)); err != nil {
|
2022-06-28 10:12:53 +00:00
|
|
|
common.ErrorResp(c, err, 500, true)
|
2022-06-26 12:00:36 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
common.SuccessResp(c)
|
|
|
|
}
|
2022-07-18 15:02:14 +00:00
|
|
|
|
|
|
|
func GetStorage(c *gin.Context) {
|
|
|
|
idStr := c.Query("id")
|
|
|
|
id, err := strconv.Atoi(idStr)
|
|
|
|
if err != nil {
|
|
|
|
common.ErrorResp(c, err, 400)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
storage, err := db.GetStorageById(uint(id))
|
|
|
|
if err != nil {
|
|
|
|
common.ErrorResp(c, err, 500, true)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
common.SuccessResp(c, storage)
|
|
|
|
}
|