Skip to content

Commit

Permalink
feat: add user model
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Jun 17, 2022
1 parent 1df5472 commit 56c95ea
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 3 deletions.
24 changes: 24 additions & 0 deletions internal/model/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package model

const (
GENERAL = iota
GUEST // only one exists
ADMIN
)

type User struct {
ID uint `json:"id" gorm:"primaryKey"` // unique key
Name string `json:"name" gorm:"unique"` // username
Password string `json:"password"` // password
BasePath string `json:"base_path"` // base path
AllowUpload bool `json:"allow_upload"` // allow upload
Role int `json:"role"` // user's role
}

func (u User) IsGuest() bool {
return u.Role == GUEST
}

func (u User) IsAdmin() bool {
return u.Role == ADMIN
}
10 changes: 7 additions & 3 deletions internal/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ package store
import (
"github.com/alist-org/alist/v3/internal/model"
"gorm.io/gorm"
"log"
)

var db gorm.DB

func Init(dbgorm *gorm.DB) {
db = *dbgorm
db.AutoMigrate(&model.Account{})
func Init(d *gorm.DB) {
db = *d
err := db.AutoMigrate(&model.Account{})
if err != nil {
log.Fatalf("failed migrate database: %s", err.Error())
}
}
78 changes: 78 additions & 0 deletions internal/store/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package store

import (
"github.com/Xhofe/go-cache"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/pkg/singleflight"
"github.com/pkg/errors"
)

var userCache = cache.NewMemCache(cache.WithShards[*model.User](4))
var userG singleflight.Group[*model.User]

func ExistAdmin() bool {
return db.Take(&model.User{Role: model.ADMIN}).Error != nil
}

func ExistGuest() bool {
return db.Take(&model.User{Role: model.GUEST}).Error != nil
}

func GetUserByName(name string) (*model.User, error) {
user, ok := userCache.Get(name)
if ok {
return user, nil
}
user, err, _ := userG.Do(name, func() (*model.User, error) {
user := model.User{Name: name}
if err := db.First(&user).Error; err != nil {
return nil, errors.Wrapf(err, "failed select user")
}
userCache.Set(name, &user)
return &user, nil
})
return user, err
}

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 {
old, err := GetUserById(u.ID)
if err != nil {
return err
}
userCache.Del(old.Name)
return errors.WithStack(db.Save(u).Error)
}

func GetUsers(pageIndex, pageSize int) ([]model.User, int64, error) {
userDb := db.Model(&model.User{})
var count int64
if err := userDb.Count(&count).Error; err != nil {
return nil, 0, errors.Wrapf(err, "failed get users count")
}
var users []model.User
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 {
old, err := GetUserById(id)
if err != nil {
return err
}
userCache.Del(old.Name)
return errors.WithStack(db.Delete(&model.User{}, id).Error)
}

0 comments on commit 56c95ea

Please sign in to comment.