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
|
|
|
)
|
|
|
|
|
2023-06-17 17:35:17 +03:00
|
|
|
// Role is the type of a role.
|
|
|
|
type Role string
|
|
|
|
|
|
|
|
const (
|
2023-07-02 13:56:25 +03:00
|
|
|
// RoleHost is the HOST role.
|
|
|
|
RoleHost Role = "HOST"
|
|
|
|
// RoleAdmin is the ADMIN role.
|
|
|
|
RoleAdmin Role = "ADMIN"
|
|
|
|
// RoleUser is the USER role.
|
|
|
|
RoleUser Role = "USER"
|
2023-06-17 17:35:17 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func (e Role) String() string {
|
|
|
|
switch e {
|
2023-07-02 13:56:25 +03:00
|
|
|
case RoleHost:
|
2023-06-17 17:35:17 +03:00
|
|
|
return "HOST"
|
2023-07-02 13:56:25 +03:00
|
|
|
case RoleAdmin:
|
2023-06-17 17:35:17 +03:00
|
|
|
return "ADMIN"
|
2023-07-02 13:56:25 +03:00
|
|
|
case RoleUser:
|
2023-06-17 17:35:17 +03:00
|
|
|
return "USER"
|
|
|
|
}
|
|
|
|
return "USER"
|
|
|
|
}
|
|
|
|
|
2023-07-02 09:27:23 +03:00
|
|
|
type User struct {
|
2023-08-04 16:55:07 +03:00
|
|
|
ID int32
|
2023-06-17 17:35:17 +03:00
|
|
|
|
|
|
|
// Standard fields
|
|
|
|
RowStatus RowStatus
|
|
|
|
CreatedTs int64
|
|
|
|
UpdatedTs int64
|
|
|
|
|
|
|
|
// Domain specific fields
|
|
|
|
Username string
|
|
|
|
Role Role
|
|
|
|
Email string
|
|
|
|
Nickname string
|
|
|
|
PasswordHash string
|
|
|
|
AvatarURL string
|
|
|
|
}
|
|
|
|
|
2023-07-02 09:27:23 +03:00
|
|
|
type UpdateUser struct {
|
2023-08-04 16:55:07 +03:00
|
|
|
ID int32
|
2023-07-02 09:27:23 +03:00
|
|
|
|
|
|
|
UpdatedTs *int64
|
|
|
|
RowStatus *RowStatus
|
2023-09-10 13:56:24 +03:00
|
|
|
Username *string
|
|
|
|
Role *Role
|
|
|
|
Email *string
|
|
|
|
Nickname *string
|
|
|
|
Password *string
|
|
|
|
AvatarURL *string
|
2023-07-02 09:27:23 +03:00
|
|
|
PasswordHash *string
|
|
|
|
}
|
|
|
|
|
|
|
|
type FindUser struct {
|
2023-08-04 16:55:07 +03:00
|
|
|
ID *int32
|
2023-06-17 17:35:17 +03:00
|
|
|
RowStatus *RowStatus
|
2023-07-06 17:53:38 +03:00
|
|
|
Username *string
|
|
|
|
Role *Role
|
|
|
|
Email *string
|
|
|
|
Nickname *string
|
2023-06-17 17:35:17 +03:00
|
|
|
}
|
|
|
|
|
2023-07-02 13:56:25 +03:00
|
|
|
type DeleteUser struct {
|
2023-08-04 16:55:07 +03:00
|
|
|
ID int32
|
2023-07-02 13:56:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) CreateUser(ctx context.Context, create *User) (*User, error) {
|
2023-07-20 18:15:56 +03:00
|
|
|
stmt := `
|
2023-06-17 17:35:17 +03:00
|
|
|
INSERT INTO user (
|
|
|
|
username,
|
|
|
|
role,
|
|
|
|
email,
|
|
|
|
nickname,
|
2023-09-14 17:57:27 +03:00
|
|
|
password_hash
|
2023-06-17 17:35:17 +03:00
|
|
|
)
|
2023-09-14 17:57:27 +03:00
|
|
|
VALUES (?, ?, ?, ?, ?)
|
2023-06-17 17:35:17 +03:00
|
|
|
RETURNING id, avatar_url, created_ts, updated_ts, row_status
|
|
|
|
`
|
2023-07-20 18:15:56 +03:00
|
|
|
if err := s.db.QueryRowContext(
|
|
|
|
ctx,
|
|
|
|
stmt,
|
2023-06-17 17:35:17 +03:00
|
|
|
create.Username,
|
|
|
|
create.Role,
|
|
|
|
create.Email,
|
|
|
|
create.Nickname,
|
|
|
|
create.PasswordHash,
|
|
|
|
).Scan(
|
|
|
|
&create.ID,
|
|
|
|
&create.AvatarURL,
|
|
|
|
&create.CreatedTs,
|
|
|
|
&create.UpdatedTs,
|
|
|
|
&create.RowStatus,
|
|
|
|
); err != nil {
|
2023-07-02 09:27:23 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
2023-07-06 16:56:42 +03:00
|
|
|
|
2023-07-02 09:27:23 +03:00
|
|
|
user := create
|
2023-07-02 13:56:25 +03:00
|
|
|
s.userCache.Store(user.ID, user)
|
2023-07-02 09:27:23 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
args = append(args, update.ID)
|
|
|
|
|
|
|
|
query := `
|
|
|
|
UPDATE user
|
|
|
|
SET ` + strings.Join(set, ", ") + `
|
|
|
|
WHERE id = ?
|
2023-09-14 17:57:27 +03:00
|
|
|
RETURNING id, username, role, email, nickname, password_hash, avatar_url, created_ts, updated_ts, row_status
|
2023-07-02 09:27:23 +03:00
|
|
|
`
|
|
|
|
user := &User{}
|
2023-07-20 18:15:56 +03:00
|
|
|
if err := s.db.QueryRowContext(ctx, query, args...).Scan(
|
2023-07-02 09:27:23 +03:00
|
|
|
&user.ID,
|
|
|
|
&user.Username,
|
|
|
|
&user.Role,
|
|
|
|
&user.Email,
|
|
|
|
&user.Nickname,
|
|
|
|
&user.PasswordHash,
|
|
|
|
&user.AvatarURL,
|
|
|
|
&user.CreatedTs,
|
|
|
|
&user.UpdatedTs,
|
|
|
|
&user.RowStatus,
|
|
|
|
); err != nil {
|
|
|
|
return nil, err
|
2023-06-17 17:35:17 +03:00
|
|
|
}
|
2023-07-02 09:27:23 +03:00
|
|
|
|
2023-07-02 13:56:25 +03:00
|
|
|
s.userCache.Store(user.ID, user)
|
2023-07-02 09:27:23 +03:00
|
|
|
return user, nil
|
2023-06-17 17:35:17 +03:00
|
|
|
}
|
|
|
|
|
2023-07-02 09:27:23 +03:00
|
|
|
func (s *Store) ListUsers(ctx context.Context, find *FindUser) ([]*User, error) {
|
2023-03-18 17:34:22 +03:00
|
|
|
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)
|
|
|
|
}
|
2022-11-23 17:27:21 +03:00
|
|
|
if v := find.Username; v != nil {
|
|
|
|
where, args = append(where, "username = ?"), append(args, *v)
|
|
|
|
}
|
2022-05-15 05:57:54 +03:00
|
|
|
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)
|
|
|
|
}
|
2022-11-23 17:27:21 +03:00
|
|
|
if v := find.Nickname; v != nil {
|
|
|
|
where, args = append(where, "nickname = ?"), append(args, *v)
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|
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,
|
2022-11-23 17:27:21 +03:00
|
|
|
username,
|
2022-05-15 05:57:54 +03:00
|
|
|
role,
|
2022-11-23 17:27:21 +03:00
|
|
|
email,
|
|
|
|
nickname,
|
2022-02-06 11:19:20 +03:00
|
|
|
password_hash,
|
2023-02-17 18:55:56 +03:00
|
|
|
avatar_url,
|
2022-02-03 19:56:44 +03:00
|
|
|
created_ts,
|
2022-07-08 17:16:18 +03:00
|
|
|
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
|
|
|
|
`
|
2023-07-20 18:15:56 +03:00
|
|
|
rows, err := s.db.QueryContext(ctx, query, args...)
|
2022-02-03 10:32:03 +03:00
|
|
|
if err != nil {
|
2023-07-02 13:56:25 +03:00
|
|
|
return nil, err
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
2023-07-02 13:56:25 +03:00
|
|
|
list := make([]*User, 0)
|
2022-02-03 10:32:03 +03:00
|
|
|
for rows.Next() {
|
2023-07-02 13:56:25 +03:00
|
|
|
var user User
|
2022-02-03 10:32:03 +03:00
|
|
|
if err := rows.Scan(
|
2023-07-02 13:56:25 +03:00
|
|
|
&user.ID,
|
|
|
|
&user.Username,
|
|
|
|
&user.Role,
|
|
|
|
&user.Email,
|
|
|
|
&user.Nickname,
|
|
|
|
&user.PasswordHash,
|
|
|
|
&user.AvatarURL,
|
|
|
|
&user.CreatedTs,
|
|
|
|
&user.UpdatedTs,
|
|
|
|
&user.RowStatus,
|
2022-02-03 10:32:03 +03:00
|
|
|
); err != nil {
|
2023-07-02 13:56:25 +03:00
|
|
|
return nil, err
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|
2023-07-02 13:56:25 +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 {
|
2023-07-02 13:56:25 +03:00
|
|
|
return nil, err
|
2022-09-03 13:54:22 +03:00
|
|
|
}
|
|
|
|
|
2023-07-20 18:15:56 +03:00
|
|
|
for _, user := range list {
|
|
|
|
s.userCache.Store(user.ID, user)
|
|
|
|
}
|
2023-07-02 13:56:25 +03:00
|
|
|
return list, nil
|
2022-07-26 16:41:20 +03:00
|
|
|
}
|
2023-07-20 18:15:56 +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
|
|
|
|
}
|