memos/store/user.go

284 lines
5.6 KiB
Go
Raw Normal View History

2021-12-08 18:43:14 +03:00
package store
import (
2022-08-07 04:23:46 +03:00
"context"
2021-12-09 17:02:57 +03:00
"strings"
2021-12-08 18:43:14 +03: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 16:55:07 +03:00
ID int32
// Standard fields
RowStatus RowStatus
CreatedTs int64
UpdatedTs int64
// Domain specific fields
Username string
Role Role
Email string
Nickname string
PasswordHash string
OpenID string
AvatarURL string
}
type UpdateUser struct {
2023-08-04 16:55:07 +03:00
ID int32
UpdatedTs *int64
RowStatus *RowStatus
2023-09-10 13:56:24 +03:00
Username *string
Role *Role
Email *string
Nickname *string
Password *string
AvatarURL *string
PasswordHash *string
OpenID *string
}
type FindUser struct {
2023-08-04 16:55:07 +03:00
ID *int32
RowStatus *RowStatus
2023-07-06 17:53:38 +03:00
Username *string
Role *Role
Email *string
Nickname *string
OpenID *string
}
type DeleteUser struct {
2023-08-04 16:55:07 +03:00
ID int32
}
func (s *Store) CreateUser(ctx context.Context, create *User) (*User, error) {
stmt := `
INSERT INTO user (
username,
role,
email,
nickname,
password_hash,
open_id
)
VALUES (?, ?, ?, ?, ?, ?)
RETURNING id, avatar_url, created_ts, updated_ts, row_status
`
if err := s.db.QueryRowContext(
ctx,
stmt,
create.Username,
create.Role,
create.Email,
create.Nickname,
create.PasswordHash,
create.OpenID,
).Scan(
&create.ID,
&create.AvatarURL,
&create.CreatedTs,
&create.UpdatedTs,
&create.RowStatus,
); err != nil {
return nil, err
}
user := create
s.userCache.Store(user.ID, user)
return user, nil
}
func (s *Store) UpdateUser(ctx context.Context, update *UpdateUser) (*User, error) {
set, args := []string{}, []any{}
if v := update.UpdatedTs; v != nil {
set, args = append(set, "updated_ts = ?"), append(args, *v)
}
if v := update.RowStatus; v != nil {
set, args = append(set, "row_status = ?"), append(args, *v)
}
if v := update.Username; v != nil {
set, args = append(set, "username = ?"), append(args, *v)
}
if v := update.Email; v != nil {
set, args = append(set, "email = ?"), append(args, *v)
}
if v := update.Nickname; v != nil {
set, args = append(set, "nickname = ?"), append(args, *v)
}
if v := update.AvatarURL; v != nil {
set, args = append(set, "avatar_url = ?"), append(args, *v)
}
if v := update.PasswordHash; v != nil {
set, args = append(set, "password_hash = ?"), append(args, *v)
}
if v := update.OpenID; v != nil {
set, args = append(set, "open_id = ?"), append(args, *v)
}
args = append(args, update.ID)
query := `
UPDATE user
SET ` + strings.Join(set, ", ") + `
WHERE id = ?
RETURNING id, username, role, email, nickname, password_hash, open_id, avatar_url, created_ts, updated_ts, row_status
`
user := &User{}
if err := s.db.QueryRowContext(ctx, query, args...).Scan(
&user.ID,
&user.Username,
&user.Role,
&user.Email,
&user.Nickname,
&user.PasswordHash,
&user.OpenID,
&user.AvatarURL,
&user.CreatedTs,
&user.UpdatedTs,
&user.RowStatus,
); 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) {
where, args := []string{"1 = 1"}, []any{}
2022-02-03 10:32:03 +03:00
2022-05-02 21:05:43 +03:00
if v := find.ID; v != nil {
2022-02-03 10:32:03 +03:00
where, args = append(where, "id = ?"), append(args, *v)
}
if v := find.Username; v != nil {
where, args = append(where, "username = ?"), append(args, *v)
}
if v := find.Role; v != nil {
where, args = append(where, "role = ?"), append(args, *v)
}
if v := find.Email; v != nil {
where, args = append(where, "email = ?"), append(args, *v)
}
if v := find.Nickname; v != nil {
where, args = append(where, "nickname = ?"), append(args, *v)
2022-02-03 10:32:03 +03:00
}
2022-05-02 21:05:43 +03:00
if v := find.OpenID; v != nil {
2022-02-03 10:32:03 +03:00
where, args = append(where, "open_id = ?"), append(args, *v)
}
2021-12-08 18:43:14 +03:00
2022-08-07 04:23:46 +03:00
query := `
2022-02-03 10:32:03 +03:00
SELECT
id,
username,
role,
email,
nickname,
2022-02-06 11:19:20 +03:00
password_hash,
2022-02-03 10:32:03 +03:00
open_id,
avatar_url,
2022-02-03 19:56:44 +03:00
created_ts,
updated_ts,
row_status
2022-02-03 10:32:03 +03:00
FROM user
2022-08-07 04:23:46 +03:00
WHERE ` + strings.Join(where, " AND ") + `
ORDER BY created_ts DESC, row_status DESC
`
rows, err := s.db.QueryContext(ctx, query, args...)
2022-02-03 10:32:03 +03:00
if err != nil {
return nil, err
2022-02-03 10:32:03 +03:00
}
defer rows.Close()
list := make([]*User, 0)
2022-02-03 10:32:03 +03:00
for rows.Next() {
var user User
2022-02-03 10:32:03 +03:00
if err := rows.Scan(
&user.ID,
&user.Username,
&user.Role,
&user.Email,
&user.Nickname,
&user.PasswordHash,
&user.OpenID,
&user.AvatarURL,
&user.CreatedTs,
&user.UpdatedTs,
&user.RowStatus,
2022-02-03 10:32:03 +03:00
); err != nil {
return nil, err
2022-02-03 10:32:03 +03:00
}
list = append(list, &user)
2021-12-08 18:43:14 +03:00
}
2022-02-03 10:32:03 +03:00
if err := rows.Err(); err != nil {
return nil, err
}
for _, user := range list {
s.userCache.Store(user.ID, user)
}
return list, nil
2022-07-26 16:41:20 +03: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 {
result, err := s.db.ExecContext(ctx, `
DELETE FROM user WHERE id = ?
`, delete.ID)
if err != nil {
return err
}
if _, err := result.RowsAffected(); err != nil {
return err
}
if err := s.Vacuum(ctx); err != nil {
// Prevent linter warning.
return err
}
s.userCache.Delete(delete.ID)
return nil
}