♻️ solve circular dependency

This commit is contained in:
微凉 2022-01-15 19:59:24 +08:00
parent ed670e528f
commit 0648fdebc2
2 changed files with 12 additions and 13 deletions

View File

@ -2,8 +2,6 @@ package model
import ( import (
"github.com/Xhofe/alist/conf" "github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/drivers/base"
log "github.com/sirupsen/logrus"
"time" "time"
) )
@ -65,24 +63,18 @@ func CreateAccount(account *Account) error {
return nil return nil
} }
func DeleteAccount(id uint) error { func DeleteAccount(id uint) (*Account, error) {
var account Account var account Account
account.ID = id account.ID = id
if err := conf.DB.First(&account).Error; err != nil { if err := conf.DB.First(&account).Error; err != nil {
return err return nil, err
} }
name := account.Name name := account.Name
driver, ok := base.GetDriver(account.Type)
if ok {
_ = driver.Save(nil, &account)
} else {
log.Errorf("no driver: %s", account.Type)
}
if err := conf.DB.Delete(&account).Error; err != nil { if err := conf.DB.Delete(&account).Error; err != nil {
return err return nil, err
} }
delete(accountsMap, name) delete(accountsMap, name)
return nil return &account, nil
} }
func DeleteAccountFromMap(name string) { func DeleteAccountFromMap(name string) {

View File

@ -87,9 +87,16 @@ func DeleteAccount(c *gin.Context) {
common.ErrorResp(c, err, 400) common.ErrorResp(c, err, 400)
return return
} }
if err := model.DeleteAccount(uint(id)); err != nil { if account, err := model.DeleteAccount(uint(id)); err != nil {
common.ErrorResp(c, err, 500) common.ErrorResp(c, err, 500)
return return
} else {
driver, ok := base.GetDriver(account.Type)
if ok {
_ = driver.Save(nil, account)
} else {
log.Errorf("no driver: %s", account.Type)
}
} }
common.SuccessResp(c) common.SuccessResp(c)
} }