From 5783aa99f1931ed8e35579ec1890b97eb9b2ac77 Mon Sep 17 00:00:00 2001 From: Noah Hsu Date: Sun, 26 Jun 2022 20:00:36 +0800 Subject: [PATCH] feat: account manage api --- internal/operations/account.go | 40 +++++++++++++++++--- server/controllers/account.go | 69 ++++++++++++++++++++++++++++++++++ server/controllers/user.go | 2 +- server/router.go | 6 +++ 4 files changed, 110 insertions(+), 7 deletions(-) create mode 100644 server/controllers/account.go diff --git a/internal/operations/account.go b/internal/operations/account.go index 17c28f8bc21..b57a2964020 100644 --- a/internal/operations/account.go +++ b/internal/operations/account.go @@ -33,20 +33,23 @@ func GetAccountByVirtualPath(virtualPath string) (driver.Driver, error) { func CreateAccount(ctx context.Context, account model.Account) error { account.Modified = time.Now() account.VirtualPath = utils.StandardizePath(account.VirtualPath) - err := db.CreateAccount(&account) - if err != nil { - return errors.WithMessage(err, "failed create account in database") - } - // already has an id + var err error + // check driver first driverName := account.Driver driverNew, err := GetDriverNew(driverName) if err != nil { return errors.WithMessage(err, "failed get driver new") } accountDriver := driverNew() + // insert account to database + err = db.CreateAccount(&account) + if err != nil { + return errors.WithMessage(err, "failed create account in database") + } + // already has an id err = accountDriver.Init(ctx, account) if err != nil { - return errors.WithMessage(err, "failed init account") + return errors.WithMessage(err, "failed init account but account is already created") } accountsMap.Store(account.VirtualPath, accountDriver) return nil @@ -60,6 +63,9 @@ func UpdateAccount(ctx context.Context, account model.Account) error { if err != nil { return errors.WithMessage(err, "failed get old account") } + if oldAccount.Driver != account.Driver { + return errors.Errorf("driver cannot be changed") + } account.Modified = time.Now() account.VirtualPath = utils.StandardizePath(account.VirtualPath) err = db.UpdateAccount(&account) @@ -86,6 +92,28 @@ func UpdateAccount(ctx context.Context, account model.Account) error { return nil } +func DeleteAccountById(ctx context.Context, id uint) error { + account, err := db.GetAccountById(id) + if err != nil { + return errors.WithMessage(err, "failed get account") + } + accountDriver, err := GetAccountByVirtualPath(account.VirtualPath) + if err != nil { + return errors.WithMessage(err, "failed get account driver") + } + // drop the account in the driver + if err := accountDriver.Drop(ctx); err != nil { + return errors.WithMessage(err, "failed drop account") + } + // delete the account in the database + if err := db.DeleteAccountById(id); err != nil { + return errors.WithMessage(err, "failed delete account in database") + } + // delete the account in the memory + accountsMap.Delete(account.VirtualPath) + return nil +} + // MustSaveDriverAccount call from specific driver func MustSaveDriverAccount(driver driver.Driver) { err := saveDriverAccount(driver) diff --git a/server/controllers/account.go b/server/controllers/account.go new file mode 100644 index 00000000000..2cc629fa6cc --- /dev/null +++ b/server/controllers/account.go @@ -0,0 +1,69 @@ +package controllers + +import ( + "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" + "strconv" +) + +func ListAccounts(c *gin.Context) { + var req common.PageReq + if err := c.ShouldBind(&req); err != nil { + common.ErrorResp(c, err, 400, true) + return + } + log.Debugf("%+v", req) + accounts, total, err := db.GetAccounts(req.PageIndex, req.PageSize) + if err != nil { + common.ErrorResp(c, err, 500) + return + } + common.SuccessResp(c, common.PageResp{ + Content: accounts, + Total: total, + }) +} + +func CreateAccount(c *gin.Context) { + var req model.Account + if err := c.ShouldBind(&req); err != nil { + common.ErrorResp(c, err, 400, true) + return + } + if err := operations.CreateAccount(c, req); err != nil { + common.ErrorResp(c, err, 500) + } else { + common.SuccessResp(c) + } +} + +func UpdateAccount(c *gin.Context) { + var req model.Account + if err := c.ShouldBind(&req); err != nil { + common.ErrorResp(c, err, 400, true) + return + } + if err := operations.UpdateAccount(c, req); err != nil { + common.ErrorResp(c, err, 500) + } else { + common.SuccessResp(c) + } +} + +func DeleteAccount(c *gin.Context) { + idStr := c.Query("id") + id, err := strconv.Atoi(idStr) + if err != nil { + common.ErrorResp(c, err, 400, true) + return + } + if err := operations.DeleteAccountById(c, uint(id)); err != nil { + common.ErrorResp(c, err, 500) + return + } + common.SuccessResp(c) +} diff --git a/server/controllers/user.go b/server/controllers/user.go index 18150d07e14..8d096d64122 100644 --- a/server/controllers/user.go +++ b/server/controllers/user.go @@ -52,7 +52,7 @@ func UpdateUser(c *gin.Context) { } user, err := db.GetUserById(req.ID) if err != nil { - common.ErrorResp(c, err, 500) + common.ErrorResp(c, err, 500, true) return } if user.Role != req.Role { diff --git a/server/router.go b/server/router.go index db1694633a2..83340fdf327 100644 --- a/server/router.go +++ b/server/router.go @@ -30,6 +30,12 @@ func Init(r *gin.Engine) { user.POST("/create", controllers.CreateUser) user.POST("/update", controllers.UpdateUser) user.POST("/delete", controllers.DeleteUser) + + account := admin.Group("/account") + account.GET("/list", controllers.ListAccounts) + account.POST("/create", controllers.CreateAccount) + account.POST("/update", controllers.UpdateAccount) + account.POST("/delete", controllers.DeleteAccount) } func Cors(r *gin.Engine) {