diff --git a/model/account.go b/model/account.go index 6ea03f74..fc7b68e2 100644 --- a/model/account.go +++ b/model/account.go @@ -8,7 +8,8 @@ import ( ) type Account struct { - Name string `json:"name" gorm:"primaryKey" binding:"required"` + ID uint `json:"id" gorm:"primaryKey"` + Name string `json:"name" gorm:"unique" binding:"required"` Index int `json:"index"` Type string `json:"type"` Username string `json:"username"` @@ -38,7 +39,15 @@ var accountsMap = map[string]Account{} // SaveAccount save account to database func SaveAccount(account Account) error { - if err := conf.DB.Save(account).Error; err != nil { + if err := conf.DB.Save(&account).Error; err != nil { + return err + } + RegisterAccount(account) + return nil +} + +func CreateAccount(account Account) error { + if err := conf.DB.Create(&account).Error; err != nil { return err } RegisterAccount(account) diff --git a/model/meta.go b/model/meta.go index 04e3621c..0c0e8ddb 100644 --- a/model/meta.go +++ b/model/meta.go @@ -1,9 +1,13 @@ package model -import "github.com/Xhofe/alist/conf" +import ( + "github.com/Xhofe/alist/conf" + log "github.com/sirupsen/logrus" +) type Meta struct { - Path string `json:"path" gorm:"primaryKey" binding:"required"` + ID uint `json:"id" gorm:"primaryKey"` + Path string `json:"path" gorm:"unique" binding:"required"` Password string `json:"password"` Hide string `json:"hide"` } @@ -19,11 +23,16 @@ func GetMetaByPath(path string) (*Meta, error) { } func SaveMeta(meta Meta) error { - return conf.DB.Save(meta).Error + return conf.DB.Save(&meta).Error +} + +func CreateMeta(meta Meta) error { + return conf.DB.Create(&meta).Error } func DeleteMeta(path string) error { meta := Meta{Path: path} + log.Debugf("delete meta: %+v", meta) return conf.DB.Delete(&meta).Error } diff --git a/server/account.go b/server/account.go index a3a19884..43d589cf 100644 --- a/server/account.go +++ b/server/account.go @@ -17,6 +17,31 @@ func GetAccounts(c *gin.Context) { SuccessResp(c, accounts) } +func CreateAccount(c *gin.Context) { + var req model.Account + if err := c.ShouldBind(&req); err != nil { + ErrorResp(c, err, 400) + return + } + driver, ok := drivers.GetDriver(req.Type) + if !ok { + ErrorResp(c, fmt.Errorf("no [%s] driver", req.Type), 400) + return + } + now := time.Now() + req.UpdatedAt = &now + if err := model.CreateAccount(req); err != nil { + ErrorResp(c, err, 500) + } else { + err = driver.Save(&req, nil) + if err != nil { + ErrorResp(c, err, 500) + return + } + SuccessResp(c) + } +} + func SaveAccount(c *gin.Context) { var req model.Account if err := c.ShouldBind(&req); err != nil { diff --git a/server/meta.go b/server/meta.go index 780a6a6d..6aba4789 100644 --- a/server/meta.go +++ b/server/meta.go @@ -15,6 +15,20 @@ func GetMetas(c *gin.Context) { SuccessResp(c, metas) } +func CreateMeta(c *gin.Context) { + var req model.Meta + if err := c.ShouldBind(&req); err != nil { + ErrorResp(c, err, 400) + return + } + req.Path = utils.ParsePath(req.Path) + if err := model.CreateMeta(req); err != nil { + ErrorResp(c, err, 500) + } else { + SuccessResp(c) + } +} + func SaveMeta(c *gin.Context) { var req model.Meta if err := c.ShouldBind(&req); err != nil { @@ -34,6 +48,7 @@ func DeleteMeta(c *gin.Context) { //path = utils.ParsePath(path) if err := model.DeleteMeta(path); err != nil { ErrorResp(c, err, 500) + return } SuccessResp(c) } diff --git a/server/router.go b/server/router.go index 7a7ddd68..c1af47ad 100644 --- a/server/router.go +++ b/server/router.go @@ -27,14 +27,16 @@ func InitApiRouter(r *gin.Engine) { admin.GET("/login", Login) admin.GET("/settings", GetSettings) admin.POST("/settings", SaveSettings) - admin.POST("/account", SaveAccount) + admin.POST("/account/create", CreateAccount) + admin.POST("/account/save", SaveAccount) admin.GET("/accounts", GetAccounts) admin.DELETE("/account", DeleteAccount) admin.GET("/drivers", GetDrivers) admin.GET("/clear_cache", ClearCache) admin.GET("/metas", GetMetas) - admin.POST("/meta", SaveMeta) + admin.POST("/meta/create", CreateMeta) + admin.POST("/meta/save", SaveMeta) admin.DELETE("/meta", DeleteMeta) } Static(r)