memos/store/db/postgres/user.go

176 lines
4.6 KiB
Go
Raw Normal View History

package postgres
import (
"context"
2024-03-30 13:29:48 +08:00
"fmt"
"slices"
2024-01-05 21:27:16 +08:00
"strings"
"github.com/usememos/memos/store"
)
func (d *DB) CreateUser(ctx context.Context, create *store.User) (*store.User, error) {
2024-01-05 21:27:16 +08:00
fields := []string{"username", "role", "email", "nickname", "password_hash", "avatar_url"}
args := []any{create.Username, create.Role, create.Email, create.Nickname, create.PasswordHash, create.AvatarURL}
2024-03-13 21:24:16 +08:00
stmt := "INSERT INTO \"user\" (" + strings.Join(fields, ", ") + ") VALUES (" + placeholders(len(args)) + ") RETURNING id, avatar_url, description, created_ts, updated_ts, row_status"
2024-01-05 21:27:16 +08:00
if err := d.db.QueryRowContext(ctx, stmt, args...).Scan(
&create.ID,
&create.AvatarURL,
2024-03-13 21:24:16 +08:00
&create.Description,
2024-01-05 21:27:16 +08:00
&create.CreatedTs,
&create.UpdatedTs,
&create.RowStatus,
); err != nil {
return nil, err
}
2024-01-05 21:27:16 +08:00
return create, nil
}
func (d *DB) UpdateUser(ctx context.Context, update *store.UpdateUser) (*store.User, error) {
2024-01-05 21:27:16 +08:00
set, args := []string{}, []any{}
if v := update.UpdatedTs; v != nil {
2024-01-05 21:27:16 +08:00
set, args = append(set, "updated_ts = "+placeholder(len(args)+1)), append(args, *v)
}
if v := update.RowStatus; v != nil {
2024-01-05 21:27:16 +08:00
set, args = append(set, "row_status = "+placeholder(len(args)+1)), append(args, *v)
}
if v := update.Username; v != nil {
2024-01-05 21:27:16 +08:00
set, args = append(set, "username = "+placeholder(len(args)+1)), append(args, *v)
}
if v := update.Email; v != nil {
2024-01-05 21:27:16 +08:00
set, args = append(set, "email = "+placeholder(len(args)+1)), append(args, *v)
}
if v := update.Nickname; v != nil {
2024-01-05 21:27:16 +08:00
set, args = append(set, "nickname = "+placeholder(len(args)+1)), append(args, *v)
}
if v := update.AvatarURL; v != nil {
2024-01-05 21:27:16 +08:00
set, args = append(set, "avatar_url = "+placeholder(len(args)+1)), append(args, *v)
}
if v := update.PasswordHash; v != nil {
2024-01-05 21:27:16 +08:00
set, args = append(set, "password_hash = "+placeholder(len(args)+1)), append(args, *v)
}
2024-03-13 21:24:16 +08:00
if v := update.Description; v != nil {
set, args = append(set, "description = "+placeholder(len(args)+1)), append(args, *v)
}
2024-01-05 21:27:16 +08:00
query := `
UPDATE "user"
SET ` + strings.Join(set, ", ") + `
WHERE id = ` + placeholder(len(args)+1) + `
2024-03-13 21:24:16 +08:00
RETURNING id, username, role, email, nickname, password_hash, avatar_url, description, created_ts, updated_ts, row_status
2024-01-05 21:27:16 +08:00
`
args = append(args, update.ID)
user := &store.User{}
if err := d.db.QueryRowContext(ctx, query, args...).Scan(
&user.ID,
&user.Username,
&user.Role,
&user.Email,
&user.Nickname,
&user.PasswordHash,
&user.AvatarURL,
2024-03-13 21:24:16 +08:00
&user.Description,
2024-01-05 21:27:16 +08:00
&user.CreatedTs,
&user.UpdatedTs,
&user.RowStatus,
); err != nil {
return nil, err
}
return user, nil
}
func (d *DB) ListUsers(ctx context.Context, find *store.FindUser) ([]*store.User, error) {
2024-01-05 21:27:16 +08:00
where, args := []string{"1 = 1"}, []any{}
if v := find.ID; v != nil {
2024-01-05 21:27:16 +08:00
where, args = append(where, "id = "+placeholder(len(args)+1)), append(args, *v)
}
if v := find.Username; v != nil {
2024-01-05 21:27:16 +08:00
where, args = append(where, "username = "+placeholder(len(args)+1)), append(args, *v)
}
if v := find.Role; v != nil {
2024-01-05 21:27:16 +08:00
where, args = append(where, "role = "+placeholder(len(args)+1)), append(args, *v)
}
if v := find.Email; v != nil {
2024-01-05 21:27:16 +08:00
where, args = append(where, "email = "+placeholder(len(args)+1)), append(args, *v)
}
if v := find.Nickname; v != nil {
2024-01-05 21:27:16 +08:00
where, args = append(where, "nickname = "+placeholder(len(args)+1)), append(args, *v)
}
2024-03-30 13:29:48 +08:00
orderBy := []string{"created_ts DESC", "row_status DESC"}
if find.Random {
orderBy = slices.Concat([]string{"RANDOM()"}, orderBy)
}
2024-01-05 21:27:16 +08:00
query := `
SELECT
id,
username,
role,
email,
nickname,
password_hash,
avatar_url,
2024-03-13 21:24:16 +08:00
description,
2024-01-05 21:27:16 +08:00
created_ts,
updated_ts,
row_status
FROM "user"
2024-03-30 13:29:48 +08:00
WHERE ` + strings.Join(where, " AND ") + ` ORDER BY ` + strings.Join(orderBy, ", ")
if v := find.Limit; v != nil {
query += fmt.Sprintf(" LIMIT %d", *v)
}
rows, err := d.db.QueryContext(ctx, query, args...)
if err != nil {
return nil, err
}
defer rows.Close()
list := make([]*store.User, 0)
for rows.Next() {
var user store.User
if err := rows.Scan(
&user.ID,
&user.Username,
&user.Role,
&user.Email,
&user.Nickname,
&user.PasswordHash,
&user.AvatarURL,
2024-03-13 21:24:16 +08:00
&user.Description,
&user.CreatedTs,
&user.UpdatedTs,
&user.RowStatus,
); err != nil {
return nil, err
}
list = append(list, &user)
}
if err := rows.Err(); err != nil {
return nil, err
}
return list, nil
}
func (d *DB) DeleteUser(ctx context.Context, delete *store.DeleteUser) error {
2024-03-13 21:24:16 +08:00
result, err := d.db.ExecContext(ctx, `DELETE FROM "user" WHERE id = $1`, delete.ID)
if err != nil {
return err
}
if _, err := result.RowsAffected(); err != nil {
return err
}
if err := d.Vacuum(ctx); err != nil {
// Prevent linter warning.
return err
}
return nil
}