2022-06-06 13:48:53 +00:00
|
|
|
package store
|
|
|
|
|
2022-06-09 09:11:46 +00:00
|
|
|
import (
|
|
|
|
"github.com/alist-org/alist/v3/internal/model"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2022-06-16 13:59:49 +00:00
|
|
|
// why don't need `cache` for account?
|
|
|
|
// because all account store in `operations.accountsMap`
|
|
|
|
// the most of the read operation is from `operations.accountsMap`
|
|
|
|
// just for persistence in database
|
|
|
|
|
2022-06-09 09:11:46 +00:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2022-06-09 15:05:27 +00:00
|
|
|
// GetAccounts Get all accounts from database order by index
|
2022-06-16 13:59:49 +00:00
|
|
|
func GetAccounts(pageIndex, pageSize int) ([]model.Account, int64, error) {
|
|
|
|
accountDB := db.Model(&model.Account{})
|
|
|
|
var count int64
|
|
|
|
if err := accountDB.Count(&count).Error; err != nil {
|
|
|
|
return nil, 0, errors.Wrapf(err, "failed get accounts count")
|
|
|
|
}
|
2022-06-09 09:11:46 +00:00
|
|
|
var accounts []model.Account
|
2022-06-16 13:59:49 +00:00
|
|
|
if err := accountDB.Order(columnName("index")).Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&accounts).Error; err != nil {
|
|
|
|
return nil, 0, errors.WithStack(err)
|
2022-06-09 09:11:46 +00:00
|
|
|
}
|
2022-06-16 13:59:49 +00:00
|
|
|
return accounts, count, nil
|
2022-06-09 09:11:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2022-06-06 13:48:53 +00:00
|
|
|
}
|