memos/store/user.go

347 lines
6.7 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"
2022-05-21 19:59:22 +03:00
"database/sql"
"errors"
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 {
ID int
// 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 {
ID int
UpdatedTs *int64
RowStatus *RowStatus
Username *string `json:"username"`
Email *string `json:"email"`
Nickname *string `json:"nickname"`
Password *string `json:"password"`
ResetOpenID *bool `json:"resetOpenId"`
AvatarURL *string `json:"avatarUrl"`
PasswordHash *string
OpenID *string
}
type FindUser struct {
2023-07-06 17:53:38 +03:00
ID *int
RowStatus *RowStatus
2023-07-06 17:53:38 +03:00
Username *string
Role *Role
Email *string
Nickname *string
OpenID *string
}
type DeleteUser struct {
ID int
}
func (s *Store) CreateUser(ctx context.Context, create *User) (*User, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, err
}
defer tx.Rollback()
query := `
INSERT INTO user (
username,
role,
email,
nickname,
password_hash,
open_id
)
VALUES (?, ?, ?, ?, ?, ?)
RETURNING id, avatar_url, created_ts, updated_ts, row_status
`
if err := tx.QueryRowContext(ctx, query,
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
}
if err := tx.Commit(); 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) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, err
}
defer tx.Rollback()
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 := tx.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
}
if err := tx.Commit(); 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) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, err
}
defer tx.Rollback()
list, err := listUsers(ctx, tx, find)
if err != nil {
return nil, err
}
if err := tx.Commit(); err != nil {
return nil, err
}
for _, user := range list {
s.userCache.Store(user.ID, user)
}
return list, nil
}
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
}
}
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, err
}
defer tx.Rollback()
list, err := listUsers(ctx, tx, find)
if err != nil {
return nil, err
}
if len(list) == 0 {
return nil, nil
}
if err := tx.Commit(); err != nil {
return nil, err
}
user := list[0]
s.userCache.Store(user.ID, user)
return user, nil
}
func (s *Store) DeleteUser(ctx context.Context, delete *DeleteUser) error {
2022-08-07 04:23:46 +03:00
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return err
2022-08-07 04:23:46 +03:00
}
defer tx.Rollback()
result, err := tx.ExecContext(ctx, `
DELETE FROM user WHERE id = ?
`, delete.ID)
2022-02-03 10:32:03 +03:00
if err != nil {
return err
2022-02-03 10:32:03 +03:00
}
2021-12-08 18:43:14 +03:00
rows, err := result.RowsAffected()
2022-08-07 04:23:46 +03:00
if err != nil {
2022-11-06 07:21:58 +03:00
return err
}
if rows == 0 {
return errors.New("user not found")
}
if err := s.vacuumImpl(ctx, tx); err != nil {
2022-11-06 07:21:58 +03:00
return err
2022-07-26 16:41:20 +03:00
}
2022-08-07 04:23:46 +03:00
if err := tx.Commit(); err != nil {
2022-11-06 07:21:58 +03:00
return err
2022-08-07 04:23:46 +03:00
}
s.userCache.Delete(delete.ID)
2022-07-26 16:41:20 +03:00
return nil
}
func listUsers(ctx context.Context, tx *sql.Tx, 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 := tx.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
}
return list, nil
2022-07-26 16:41:20 +03:00
}