memos/store/user.go

136 lines
2.3 KiB
Go
Raw Normal View History

2021-12-08 23:43:14 +08:00
package store
import (
2022-08-07 09:23:46 +08:00
"context"
2021-12-08 23:43:14 +08:00
)
// Role is the type of a role.
type Role string
const (
// RoleHost is the HOST role.
RoleHost Role = "HOST"
// RoleAdmin is the ADMIN role.
RoleAdmin Role = "ADMIN"
// RoleUser is the USER role.
RoleUser Role = "USER"
)
func (e Role) String() string {
switch e {
case RoleHost:
return "HOST"
case RoleAdmin:
return "ADMIN"
case RoleUser:
return "USER"
}
return "USER"
}
type User struct {
2023-08-04 21:55:07 +08:00
ID int32
// Standard fields
RowStatus RowStatus
CreatedTs int64
UpdatedTs int64
// Domain specific fields
Username string
Role Role
Email string
Nickname string
PasswordHash string
AvatarURL string
}
type UpdateUser struct {
2023-08-04 21:55:07 +08:00
ID int32
UpdatedTs *int64
RowStatus *RowStatus
2023-09-10 18:56:24 +08:00
Username *string
Role *Role
Email *string
Nickname *string
Password *string
AvatarURL *string
PasswordHash *string
}
type FindUser struct {
2023-08-04 21:55:07 +08:00
ID *int32
RowStatus *RowStatus
2023-07-06 22:53:38 +08:00
Username *string
Role *Role
Email *string
Nickname *string
}
type DeleteUser struct {
2023-08-04 21:55:07 +08:00
ID int32
}
func (s *Store) CreateUser(ctx context.Context, create *User) (*User, error) {
user, err := s.driver.CreateUser(ctx, create)
if err != nil {
return nil, err
}
s.userCache.Store(user.ID, user)
return user, nil
}
func (s *Store) UpdateUser(ctx context.Context, update *UpdateUser) (*User, error) {
user, err := s.driver.UpdateUser(ctx, update)
if err != nil {
return nil, err
}
s.userCache.Store(user.ID, user)
return user, nil
}
func (s *Store) ListUsers(ctx context.Context, find *FindUser) ([]*User, error) {
list, err := s.driver.ListUsers(ctx, find)
2022-02-03 15:32:03 +08:00
if err != nil {
return nil, err
2022-02-03 15:32:03 +08:00
}
for _, user := range list {
s.userCache.Store(user.ID, user)
}
return list, nil
2022-07-26 21:41:20 +08:00
}
func (s *Store) GetUser(ctx context.Context, find *FindUser) (*User, error) {
if find.ID != nil {
if cache, ok := s.userCache.Load(*find.ID); ok {
return cache.(*User), nil
}
}
list, err := s.ListUsers(ctx, find)
if err != nil {
return nil, err
}
if len(list) == 0 {
return nil, nil
}
user := list[0]
s.userCache.Store(user.ID, user)
return user, nil
}
func (s *Store) DeleteUser(ctx context.Context, delete *DeleteUser) error {
err := s.driver.DeleteUser(ctx, delete)
if err != nil {
return err
}
s.userCache.Delete(delete.ID)
return nil
}