🐛 fix can't delete and get meta

This commit is contained in:
微凉 2021-11-13 17:10:44 +08:00
parent 5f34b8ab80
commit 48a65784c7
4 changed files with 25 additions and 13 deletions

View File

@ -1,7 +1,6 @@
package model package model
import ( import (
"fmt"
"github.com/Xhofe/alist/conf" "github.com/Xhofe/alist/conf"
"github.com/robfig/cron/v3" "github.com/robfig/cron/v3"
"time" "time"
@ -54,11 +53,13 @@ func CreateAccount(account Account) error {
return nil return nil
} }
func DeleteAccount(name string) error { func DeleteAccount(id uint) error {
account, ok := GetAccount(name) var account Account
if !ok { account.ID = id
return fmt.Errorf("no [%s] account", name) if err := conf.DB.First(&account).Error; err != nil {
return err
} }
name := account.Name
conf.Cron.Remove(cron.EntryID(account.CronId)) conf.Cron.Remove(cron.EntryID(account.CronId))
if err := conf.DB.Delete(&account).Error; err != nil { if err := conf.DB.Delete(&account).Error; err != nil {
return err return err

View File

@ -14,8 +14,7 @@ type Meta struct {
func GetMetaByPath(path string) (*Meta, error) { func GetMetaByPath(path string) (*Meta, error) {
var meta Meta var meta Meta
meta.Path = path err := conf.DB.Where("path = ?", path).First(&meta).Error
err := conf.DB.First(&meta).Error
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -30,8 +29,8 @@ func CreateMeta(meta Meta) error {
return conf.DB.Create(&meta).Error return conf.DB.Create(&meta).Error
} }
func DeleteMeta(path string) error { func DeleteMeta(id uint) error {
meta := Meta{Path: path} meta := Meta{ID: id}
log.Debugf("delete meta: %+v", meta) log.Debugf("delete meta: %+v", meta)
return conf.DB.Delete(&meta).Error return conf.DB.Delete(&meta).Error
} }

View File

@ -5,6 +5,7 @@ import (
"github.com/Xhofe/alist/drivers" "github.com/Xhofe/alist/drivers"
"github.com/Xhofe/alist/model" "github.com/Xhofe/alist/model"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"strconv"
"time" "time"
) )
@ -73,8 +74,13 @@ func SaveAccount(c *gin.Context) {
} }
func DeleteAccount(c *gin.Context) { func DeleteAccount(c *gin.Context) {
name := c.Query("name") idStr := c.Query("id")
if err := model.DeleteAccount(name); err != nil { id, err := strconv.Atoi(idStr)
if err != nil {
ErrorResp(c, err, 400)
return
}
if err := model.DeleteAccount(uint(id)); err != nil {
ErrorResp(c, err, 500) ErrorResp(c, err, 500)
return return
} }

View File

@ -4,6 +4,7 @@ import (
"github.com/Xhofe/alist/model" "github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/utils" "github.com/Xhofe/alist/utils"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"strconv"
) )
func GetMetas(c *gin.Context) { func GetMetas(c *gin.Context) {
@ -44,9 +45,14 @@ func SaveMeta(c *gin.Context) {
} }
func DeleteMeta(c *gin.Context) { func DeleteMeta(c *gin.Context) {
path := c.Query("path") idStr := c.Query("id")
id, err := strconv.Atoi(idStr)
if err != nil {
ErrorResp(c, err, 400)
return
}
//path = utils.ParsePath(path) //path = utils.ParsePath(path)
if err := model.DeleteMeta(path); err != nil { if err := model.DeleteMeta(uint(id)); err != nil {
ErrorResp(c, err, 500) ErrorResp(c, err, 500)
return return
} }