feat: driver and account operate
This commit is contained in:
@ -1,11 +1,40 @@
|
||||
package store
|
||||
|
||||
import "github.com/alist-org/alist/v3/internal/model"
|
||||
import (
|
||||
"github.com/alist-org/alist/v3/internal/model"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type Account interface {
|
||||
Create(account model.Account) error
|
||||
Update(account model.Account) error
|
||||
Delete(id uint) error
|
||||
GetByID(id uint) (*model.Account, error)
|
||||
List() ([]model.Account, error)
|
||||
// CreateAccount just insert account to database
|
||||
func CreateAccount(account *model.Account) error {
|
||||
return errors.WithStack(db.Create(account).Error)
|
||||
}
|
||||
|
||||
// UpdateAccount just update account in database
|
||||
func UpdateAccount(account *model.Account) error {
|
||||
return errors.WithStack(db.Save(account).Error)
|
||||
}
|
||||
|
||||
// DeleteAccountById just delete account from database by id
|
||||
func DeleteAccountById(id uint) error {
|
||||
return errors.WithStack(db.Delete(&model.Account{}, id).Error)
|
||||
}
|
||||
|
||||
// GetAccounts Get all accounts from database
|
||||
func GetAccounts() ([]model.Account, error) {
|
||||
var accounts []model.Account
|
||||
if err := db.Order(columnName("index")).Find(&accounts).Error; err != nil {
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
return accounts, nil
|
||||
}
|
||||
|
||||
// GetAccountById Get Account by id, used to update account usually
|
||||
func GetAccountById(id uint) (*model.Account, error) {
|
||||
var account model.Account
|
||||
account.ID = id
|
||||
if err := db.First(&account).Error; err != nil {
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
return &account, nil
|
||||
}
|
||||
|
7
internal/store/store.go
Normal file
7
internal/store/store.go
Normal file
@ -0,0 +1,7 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var db gorm.DB
|
13
internal/store/util.go
Normal file
13
internal/store/util.go
Normal file
@ -0,0 +1,13 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/alist-org/alist/v3/conf"
|
||||
)
|
||||
|
||||
func columnName(name string) string {
|
||||
if conf.Conf.Database.Type == "postgres" {
|
||||
return fmt.Sprintf(`"%s"`, name)
|
||||
}
|
||||
return fmt.Sprintf("`%s`", name)
|
||||
}
|
Reference in New Issue
Block a user