* refactor:separate the setting method from the db package to the op package and add the cache * refactor:separate the meta method from the db package to the op package * fix:setting not load database data * refactor:separate the user method from the db package to the op package * refactor:remove user JoinPath error * fix:op package user cache * refactor:fs package list method * fix:tile virtual paths (close #2743) * Revert "refactor:remove user JoinPath error" This reverts commit 4e20daaf9e700da047000d4fd4900abbe05c3848. * clean path directly may lead to unknown behavior * fix: The path of the meta passed in must be prefix of reqPath * chore: rename all virtualPath to mountPath * fix: `getStoragesByPath` and `GetStorageVirtualFilesByPath` is_sub_path: /a/b isn't subpath of /a/bc * fix: don't save setting if hook error Co-authored-by: Noah Hsu <i@nn.ci>
54 lines
1.4 KiB
Go
54 lines
1.4 KiB
Go
package db
|
|
|
|
import (
|
|
"github.com/alist-org/alist/v3/internal/model"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
func GetUserByRole(role int) (*model.User, error) {
|
|
user := model.User{Role: role}
|
|
if err := db.Where(user).Take(&user).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return &user, nil
|
|
}
|
|
|
|
func GetUserByName(username string) (*model.User, error) {
|
|
user := model.User{Username: username}
|
|
if err := db.Where(user).First(&user).Error; err != nil {
|
|
return nil, errors.Wrapf(err, "failed find user")
|
|
}
|
|
return &user, nil
|
|
}
|
|
|
|
func GetUserById(id uint) (*model.User, error) {
|
|
var u model.User
|
|
if err := db.First(&u, id).Error; err != nil {
|
|
return nil, errors.Wrapf(err, "failed get old user")
|
|
}
|
|
return &u, nil
|
|
}
|
|
|
|
func CreateUser(u *model.User) error {
|
|
return errors.WithStack(db.Create(u).Error)
|
|
}
|
|
|
|
func UpdateUser(u *model.User) error {
|
|
return errors.WithStack(db.Save(u).Error)
|
|
}
|
|
|
|
func GetUsers(pageIndex, pageSize int) (users []model.User, count int64, err error) {
|
|
userDB := db.Model(&model.User{})
|
|
if err := userDB.Count(&count).Error; err != nil {
|
|
return nil, 0, errors.Wrapf(err, "failed get users count")
|
|
}
|
|
if err := userDB.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&users).Error; err != nil {
|
|
return nil, 0, errors.Wrapf(err, "failed get find users")
|
|
}
|
|
return users, count, nil
|
|
}
|
|
|
|
func DeleteUserById(id uint) error {
|
|
return errors.WithStack(db.Delete(&model.User{}, id).Error)
|
|
}
|