Skip to content

Commit

Permalink
feat: account manage api
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Jun 26, 2022
1 parent cab498e commit 5783aa9
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 7 deletions.
40 changes: 34 additions & 6 deletions internal/operations/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand Down
69 changes: 69 additions & 0 deletions server/controllers/account.go
Original file line number Diff line number Diff line change
@@ -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)
}
2 changes: 1 addition & 1 deletion server/controllers/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 5783aa9

Please sign in to comment.