2021-12-08 23:43:14 +08:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
2022-08-07 09:23:46 +08:00
|
|
|
"context"
|
2022-05-22 00:59:22 +08:00
|
|
|
"database/sql"
|
2021-12-08 23:43:14 +08:00
|
|
|
"fmt"
|
2021-12-09 22:02:57 +08:00
|
|
|
"strings"
|
2022-06-27 22:09:06 +08:00
|
|
|
|
|
|
|
"github.com/usememos/memos/api"
|
|
|
|
"github.com/usememos/memos/common"
|
2021-12-08 23:43:14 +08:00
|
|
|
)
|
|
|
|
|
2023-06-17 22:35:17 +08:00
|
|
|
// Role is the type of a role.
|
|
|
|
type Role string
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Host is the HOST role.
|
|
|
|
Host Role = "HOST"
|
|
|
|
// Admin is the ADMIN role.
|
|
|
|
Admin Role = "ADMIN"
|
|
|
|
// NormalUser is the USER role.
|
|
|
|
NormalUser Role = "USER"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (e Role) String() string {
|
|
|
|
switch e {
|
|
|
|
case Host:
|
|
|
|
return "HOST"
|
|
|
|
case Admin:
|
|
|
|
return "ADMIN"
|
|
|
|
case NormalUser:
|
|
|
|
return "USER"
|
|
|
|
}
|
|
|
|
return "USER"
|
|
|
|
}
|
|
|
|
|
|
|
|
type UserMessage 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 FindUserMessage struct {
|
|
|
|
ID *int
|
|
|
|
|
|
|
|
// Standard fields
|
|
|
|
RowStatus *RowStatus
|
|
|
|
|
|
|
|
// Domain specific fields
|
|
|
|
Username *string
|
|
|
|
Role *Role
|
|
|
|
Email *string
|
|
|
|
Nickname *string
|
|
|
|
OpenID *string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) CreateUserV1(ctx context.Context, create *UserMessage) (*UserMessage, error) {
|
|
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, FormatError(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, FormatError(err)
|
|
|
|
}
|
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
userMessage := create
|
|
|
|
return userMessage, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) ListUsers(ctx context.Context, find *FindUserMessage) ([]*UserMessage, error) {
|
|
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
list, err := listUsers(ctx, tx, find)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return list, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) GetUser(ctx context.Context, find *FindUserMessage) (*UserMessage, error) {
|
|
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
list, err := listUsers(ctx, tx, find)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(list) == 0 {
|
|
|
|
return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("user not found")}
|
|
|
|
}
|
|
|
|
|
|
|
|
memoMessage := list[0]
|
|
|
|
return memoMessage, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func listUsers(ctx context.Context, tx *sql.Tx, find *FindUserMessage) ([]*UserMessage, error) {
|
|
|
|
where, args := []string{"1 = 1"}, []any{}
|
|
|
|
|
|
|
|
if v := find.ID; v != nil {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
if v := find.OpenID; v != nil {
|
|
|
|
where, args = append(where, "open_id = ?"), append(args, *v)
|
|
|
|
}
|
|
|
|
|
|
|
|
query := `
|
|
|
|
SELECT
|
|
|
|
id,
|
|
|
|
username,
|
|
|
|
role,
|
|
|
|
email,
|
|
|
|
nickname,
|
|
|
|
password_hash,
|
|
|
|
open_id,
|
|
|
|
avatar_url,
|
|
|
|
created_ts,
|
|
|
|
updated_ts,
|
|
|
|
row_status
|
|
|
|
FROM user
|
|
|
|
WHERE ` + strings.Join(where, " AND ") + `
|
|
|
|
ORDER BY created_ts DESC, row_status DESC
|
|
|
|
`
|
|
|
|
rows, err := tx.QueryContext(ctx, query, args...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
userMessageList := make([]*UserMessage, 0)
|
|
|
|
for rows.Next() {
|
|
|
|
var userMessage UserMessage
|
|
|
|
if err := rows.Scan(
|
|
|
|
&userMessage.ID,
|
|
|
|
&userMessage.Username,
|
|
|
|
&userMessage.Role,
|
|
|
|
&userMessage.Email,
|
|
|
|
&userMessage.Nickname,
|
|
|
|
&userMessage.PasswordHash,
|
|
|
|
&userMessage.OpenID,
|
|
|
|
&userMessage.AvatarURL,
|
|
|
|
&userMessage.CreatedTs,
|
|
|
|
&userMessage.UpdatedTs,
|
|
|
|
&userMessage.RowStatus,
|
|
|
|
); err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
userMessageList = append(userMessageList, &userMessage)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return userMessageList, nil
|
|
|
|
}
|
|
|
|
|
2022-05-19 18:32:04 +08:00
|
|
|
// userRaw is the store model for an User.
|
|
|
|
// Fields have exactly the same meanings as User.
|
|
|
|
type userRaw struct {
|
|
|
|
ID int
|
|
|
|
|
|
|
|
// Standard fields
|
|
|
|
RowStatus api.RowStatus
|
|
|
|
CreatedTs int64
|
|
|
|
UpdatedTs int64
|
|
|
|
|
|
|
|
// Domain specific fields
|
2022-11-23 22:27:21 +08:00
|
|
|
Username string
|
2022-05-19 18:32:04 +08:00
|
|
|
Role api.Role
|
2022-11-23 22:27:21 +08:00
|
|
|
Email string
|
|
|
|
Nickname string
|
2022-05-19 18:32:04 +08:00
|
|
|
PasswordHash string
|
|
|
|
OpenID string
|
2023-02-17 23:55:56 +08:00
|
|
|
AvatarURL string
|
2022-05-19 18:32:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (raw *userRaw) toUser() *api.User {
|
|
|
|
return &api.User{
|
|
|
|
ID: raw.ID,
|
|
|
|
|
|
|
|
RowStatus: raw.RowStatus,
|
|
|
|
CreatedTs: raw.CreatedTs,
|
|
|
|
UpdatedTs: raw.UpdatedTs,
|
|
|
|
|
2022-11-23 22:27:21 +08:00
|
|
|
Username: raw.Username,
|
2022-05-19 18:32:04 +08:00
|
|
|
Role: raw.Role,
|
2022-11-23 22:27:21 +08:00
|
|
|
Email: raw.Email,
|
|
|
|
Nickname: raw.Nickname,
|
2022-05-19 18:32:04 +08:00
|
|
|
PasswordHash: raw.PasswordHash,
|
|
|
|
OpenID: raw.OpenID,
|
2023-02-17 23:55:56 +08:00
|
|
|
AvatarURL: raw.AvatarURL,
|
2022-05-19 18:32:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-07 09:23:46 +08:00
|
|
|
func (s *Store) CreateUser(ctx context.Context, create *api.UserCreate) (*api.User, error) {
|
|
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
userRaw, err := createUser(ctx, tx, create)
|
2021-12-15 10:55:17 +08:00
|
|
|
if err != nil {
|
2022-02-03 15:32:03 +08:00
|
|
|
return nil, err
|
2021-12-15 10:55:17 +08:00
|
|
|
}
|
|
|
|
|
2022-08-07 09:23:46 +08:00
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
|
2023-02-11 14:19:26 +08:00
|
|
|
s.userCache.Store(userRaw.ID, userRaw)
|
2022-10-01 10:37:02 +08:00
|
|
|
user := userRaw.toUser()
|
2023-05-13 22:25:15 +08:00
|
|
|
|
2022-02-03 15:32:03 +08:00
|
|
|
return user, nil
|
|
|
|
}
|
2021-12-08 23:43:14 +08:00
|
|
|
|
2022-08-07 09:23:46 +08:00
|
|
|
func (s *Store) PatchUser(ctx context.Context, patch *api.UserPatch) (*api.User, error) {
|
|
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
userRaw, err := patchUser(ctx, tx, patch)
|
2022-02-03 15:32:03 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-12-08 23:43:14 +08:00
|
|
|
}
|
|
|
|
|
2022-08-07 09:23:46 +08:00
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
|
2023-02-11 14:19:26 +08:00
|
|
|
s.userCache.Store(userRaw.ID, userRaw)
|
2022-10-01 10:37:02 +08:00
|
|
|
user := userRaw.toUser()
|
2022-02-03 15:32:03 +08:00
|
|
|
return user, nil
|
2021-12-08 23:43:14 +08:00
|
|
|
}
|
|
|
|
|
2022-08-07 09:23:46 +08:00
|
|
|
func (s *Store) FindUserList(ctx context.Context, find *api.UserFind) ([]*api.User, error) {
|
|
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
userRawList, err := findUserList(ctx, tx, find)
|
2022-05-16 22:19:39 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-05-19 18:32:04 +08:00
|
|
|
list := []*api.User{}
|
|
|
|
for _, raw := range userRawList {
|
|
|
|
list = append(list, raw.toUser())
|
|
|
|
}
|
|
|
|
|
2022-05-16 22:19:39 +08:00
|
|
|
return list, nil
|
|
|
|
}
|
|
|
|
|
2022-08-07 09:23:46 +08:00
|
|
|
func (s *Store) FindUser(ctx context.Context, find *api.UserFind) (*api.User, error) {
|
2022-08-07 08:09:43 +08:00
|
|
|
if find.ID != nil {
|
2023-02-11 14:19:26 +08:00
|
|
|
if user, ok := s.userCache.Load(*find.ID); ok {
|
|
|
|
return user.(*userRaw).toUser(), nil
|
2022-08-07 08:09:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-07 09:23:46 +08:00
|
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
list, err := findUserList(ctx, tx, find)
|
2022-02-03 15:32:03 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-12-14 20:40:24 +08:00
|
|
|
|
2022-02-03 15:32:03 +08:00
|
|
|
if len(list) == 0 {
|
2022-09-05 21:14:17 +08:00
|
|
|
return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("not found user with filter %+v", find)}
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
2021-12-08 23:43:14 +08:00
|
|
|
|
2022-10-01 10:37:02 +08:00
|
|
|
userRaw := list[0]
|
2023-02-11 14:19:26 +08:00
|
|
|
s.userCache.Store(userRaw.ID, userRaw)
|
2022-10-01 10:37:02 +08:00
|
|
|
user := userRaw.toUser()
|
2022-05-19 18:32:04 +08:00
|
|
|
return user, nil
|
2021-12-08 23:43:14 +08:00
|
|
|
}
|
|
|
|
|
2022-08-07 09:23:46 +08:00
|
|
|
func (s *Store) DeleteUser(ctx context.Context, delete *api.UserDelete) error {
|
|
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
|
|
if err != nil {
|
|
|
|
return FormatError(err)
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
2022-11-06 12:21:58 +08:00
|
|
|
if err := deleteUser(ctx, tx, delete); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-05-09 09:02:59 +08:00
|
|
|
if err := s.vacuumImpl(ctx, tx); err != nil {
|
2022-11-06 12:21:58 +08:00
|
|
|
return err
|
2022-07-26 21:41:20 +08:00
|
|
|
}
|
|
|
|
|
2022-08-07 09:23:46 +08:00
|
|
|
if err := tx.Commit(); err != nil {
|
2022-11-06 12:21:58 +08:00
|
|
|
return err
|
2022-08-07 09:23:46 +08:00
|
|
|
}
|
|
|
|
|
2023-02-11 14:19:26 +08:00
|
|
|
s.userCache.Delete(delete.ID)
|
2022-07-26 21:41:20 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-07 09:23:46 +08:00
|
|
|
func createUser(ctx context.Context, tx *sql.Tx, create *api.UserCreate) (*userRaw, error) {
|
|
|
|
query := `
|
2022-02-03 15:32:03 +08:00
|
|
|
INSERT INTO user (
|
2022-11-23 22:27:21 +08:00
|
|
|
username,
|
2022-05-15 10:57:54 +08:00
|
|
|
role,
|
2022-11-23 22:27:21 +08:00
|
|
|
email,
|
|
|
|
nickname,
|
2022-02-06 16:19:20 +08:00
|
|
|
password_hash,
|
2023-02-18 10:48:31 +08:00
|
|
|
open_id
|
2022-02-03 15:32:03 +08:00
|
|
|
)
|
2023-02-18 10:48:31 +08:00
|
|
|
VALUES (?, ?, ?, ?, ?, ?)
|
2023-02-17 23:55:56 +08:00
|
|
|
RETURNING id, username, role, email, nickname, password_hash, open_id, avatar_url, created_ts, updated_ts, row_status
|
2022-08-07 09:23:46 +08:00
|
|
|
`
|
|
|
|
var userRaw userRaw
|
|
|
|
if err := tx.QueryRowContext(ctx, query,
|
2022-11-23 22:27:21 +08:00
|
|
|
create.Username,
|
2022-05-15 10:57:54 +08:00
|
|
|
create.Role,
|
2022-11-23 22:27:21 +08:00
|
|
|
create.Email,
|
|
|
|
create.Nickname,
|
2022-02-06 16:19:20 +08:00
|
|
|
create.PasswordHash,
|
2022-05-03 02:05:43 +08:00
|
|
|
create.OpenID,
|
2022-08-07 09:23:46 +08:00
|
|
|
).Scan(
|
2022-05-19 18:32:04 +08:00
|
|
|
&userRaw.ID,
|
2022-11-23 22:27:21 +08:00
|
|
|
&userRaw.Username,
|
2022-05-19 18:32:04 +08:00
|
|
|
&userRaw.Role,
|
2022-11-23 22:27:21 +08:00
|
|
|
&userRaw.Email,
|
|
|
|
&userRaw.Nickname,
|
2022-05-19 18:32:04 +08:00
|
|
|
&userRaw.PasswordHash,
|
|
|
|
&userRaw.OpenID,
|
2023-02-17 23:55:56 +08:00
|
|
|
&userRaw.AvatarURL,
|
2022-05-19 18:32:04 +08:00
|
|
|
&userRaw.CreatedTs,
|
|
|
|
&userRaw.UpdatedTs,
|
2022-07-08 22:16:18 +08:00
|
|
|
&userRaw.RowStatus,
|
2022-02-03 15:32:03 +08:00
|
|
|
); err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
|
2022-05-19 18:32:04 +08:00
|
|
|
return &userRaw, nil
|
2021-12-08 23:43:14 +08:00
|
|
|
}
|
|
|
|
|
2022-08-07 09:23:46 +08:00
|
|
|
func patchUser(ctx context.Context, tx *sql.Tx, patch *api.UserPatch) (*userRaw, error) {
|
2023-03-18 22:34:22 +08:00
|
|
|
set, args := []string{}, []any{}
|
2021-12-08 23:43:14 +08:00
|
|
|
|
2022-10-26 23:00:09 +08:00
|
|
|
if v := patch.UpdatedTs; v != nil {
|
|
|
|
set, args = append(set, "updated_ts = ?"), append(args, *v)
|
|
|
|
}
|
2022-05-19 18:32:04 +08:00
|
|
|
if v := patch.RowStatus; v != nil {
|
|
|
|
set, args = append(set, "row_status = ?"), append(args, *v)
|
|
|
|
}
|
2022-11-23 22:27:21 +08:00
|
|
|
if v := patch.Username; v != nil {
|
|
|
|
set, args = append(set, "username = ?"), append(args, *v)
|
|
|
|
}
|
2022-05-15 10:57:54 +08:00
|
|
|
if v := patch.Email; v != nil {
|
2022-05-19 18:32:04 +08:00
|
|
|
set, args = append(set, "email = ?"), append(args, *v)
|
2022-05-15 10:57:54 +08:00
|
|
|
}
|
2022-11-23 22:27:21 +08:00
|
|
|
if v := patch.Nickname; v != nil {
|
|
|
|
set, args = append(set, "nickname = ?"), append(args, *v)
|
2021-12-08 23:43:14 +08:00
|
|
|
}
|
2023-02-17 23:55:56 +08:00
|
|
|
if v := patch.AvatarURL; v != nil {
|
|
|
|
set, args = append(set, "avatar_url = ?"), append(args, *v)
|
|
|
|
}
|
2022-02-06 16:19:20 +08:00
|
|
|
if v := patch.PasswordHash; v != nil {
|
2022-05-19 18:32:04 +08:00
|
|
|
set, args = append(set, "password_hash = ?"), append(args, *v)
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
2022-05-03 02:05:43 +08:00
|
|
|
if v := patch.OpenID; v != nil {
|
2022-05-19 18:32:04 +08:00
|
|
|
set, args = append(set, "open_id = ?"), append(args, *v)
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
2022-02-04 21:24:21 +08:00
|
|
|
|
2022-05-03 02:05:43 +08:00
|
|
|
args = append(args, patch.ID)
|
2022-02-03 15:32:03 +08:00
|
|
|
|
2022-08-07 09:23:46 +08:00
|
|
|
query := `
|
2022-02-03 15:32:03 +08:00
|
|
|
UPDATE user
|
2022-08-07 09:23:46 +08:00
|
|
|
SET ` + strings.Join(set, ", ") + `
|
2022-02-03 15:32:03 +08:00
|
|
|
WHERE id = ?
|
2023-02-17 23:55:56 +08:00
|
|
|
RETURNING id, username, role, email, nickname, password_hash, open_id, avatar_url, created_ts, updated_ts, row_status
|
2022-08-07 09:23:46 +08:00
|
|
|
`
|
2022-11-23 22:27:21 +08:00
|
|
|
var userRaw userRaw
|
|
|
|
if err := tx.QueryRowContext(ctx, query, args...).Scan(
|
|
|
|
&userRaw.ID,
|
|
|
|
&userRaw.Username,
|
|
|
|
&userRaw.Role,
|
|
|
|
&userRaw.Email,
|
|
|
|
&userRaw.Nickname,
|
|
|
|
&userRaw.PasswordHash,
|
|
|
|
&userRaw.OpenID,
|
2023-02-17 23:55:56 +08:00
|
|
|
&userRaw.AvatarURL,
|
2022-11-23 22:27:21 +08:00
|
|
|
&userRaw.CreatedTs,
|
|
|
|
&userRaw.UpdatedTs,
|
|
|
|
&userRaw.RowStatus,
|
|
|
|
); err != nil {
|
2022-02-03 15:32:03 +08:00
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
2021-12-15 10:55:17 +08:00
|
|
|
|
2022-11-23 22:27:21 +08:00
|
|
|
return &userRaw, nil
|
2021-12-08 23:43:14 +08:00
|
|
|
}
|
|
|
|
|
2022-08-07 09:23:46 +08:00
|
|
|
func findUserList(ctx context.Context, tx *sql.Tx, find *api.UserFind) ([]*userRaw, error) {
|
2023-03-18 22:34:22 +08:00
|
|
|
where, args := []string{"1 = 1"}, []any{}
|
2022-02-03 15:32:03 +08:00
|
|
|
|
2022-05-03 02:05:43 +08:00
|
|
|
if v := find.ID; v != nil {
|
2022-02-03 15:32:03 +08:00
|
|
|
where, args = append(where, "id = ?"), append(args, *v)
|
|
|
|
}
|
2022-11-23 22:27:21 +08:00
|
|
|
if v := find.Username; v != nil {
|
|
|
|
where, args = append(where, "username = ?"), append(args, *v)
|
|
|
|
}
|
2022-05-15 10:57:54 +08: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 22:27:21 +08:00
|
|
|
if v := find.Nickname; v != nil {
|
|
|
|
where, args = append(where, "nickname = ?"), append(args, *v)
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
2022-05-03 02:05:43 +08:00
|
|
|
if v := find.OpenID; v != nil {
|
2022-02-03 15:32:03 +08:00
|
|
|
where, args = append(where, "open_id = ?"), append(args, *v)
|
|
|
|
}
|
2021-12-08 23:43:14 +08:00
|
|
|
|
2022-08-07 09:23:46 +08:00
|
|
|
query := `
|
2022-02-03 15:32:03 +08:00
|
|
|
SELECT
|
|
|
|
id,
|
2022-11-23 22:27:21 +08:00
|
|
|
username,
|
2022-05-15 10:57:54 +08:00
|
|
|
role,
|
2022-11-23 22:27:21 +08:00
|
|
|
email,
|
|
|
|
nickname,
|
2022-02-06 16:19:20 +08:00
|
|
|
password_hash,
|
2022-02-03 15:32:03 +08:00
|
|
|
open_id,
|
2023-02-17 23:55:56 +08:00
|
|
|
avatar_url,
|
2022-02-04 00:56:44 +08:00
|
|
|
created_ts,
|
2022-07-08 22:16:18 +08:00
|
|
|
updated_ts,
|
|
|
|
row_status
|
2022-02-03 15:32:03 +08:00
|
|
|
FROM user
|
2022-08-07 09:23:46 +08:00
|
|
|
WHERE ` + strings.Join(where, " AND ") + `
|
|
|
|
ORDER BY created_ts DESC, row_status DESC
|
|
|
|
`
|
|
|
|
rows, err := tx.QueryContext(ctx, query, args...)
|
2022-02-03 15:32:03 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
2022-05-19 18:32:04 +08:00
|
|
|
userRawList := make([]*userRaw, 0)
|
2022-02-03 15:32:03 +08:00
|
|
|
for rows.Next() {
|
2022-05-19 18:32:04 +08:00
|
|
|
var userRaw userRaw
|
2022-02-03 15:32:03 +08:00
|
|
|
if err := rows.Scan(
|
2022-05-19 18:32:04 +08:00
|
|
|
&userRaw.ID,
|
2022-11-23 22:27:21 +08:00
|
|
|
&userRaw.Username,
|
2022-05-19 18:32:04 +08:00
|
|
|
&userRaw.Role,
|
2022-11-23 22:27:21 +08:00
|
|
|
&userRaw.Email,
|
|
|
|
&userRaw.Nickname,
|
2022-05-19 18:32:04 +08:00
|
|
|
&userRaw.PasswordHash,
|
|
|
|
&userRaw.OpenID,
|
2023-02-17 23:55:56 +08:00
|
|
|
&userRaw.AvatarURL,
|
2022-05-19 18:32:04 +08:00
|
|
|
&userRaw.CreatedTs,
|
|
|
|
&userRaw.UpdatedTs,
|
2022-07-08 22:16:18 +08:00
|
|
|
&userRaw.RowStatus,
|
2022-02-03 15:32:03 +08:00
|
|
|
); err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
2022-05-19 18:32:04 +08:00
|
|
|
userRawList = append(userRawList, &userRaw)
|
2021-12-08 23:43:14 +08:00
|
|
|
}
|
|
|
|
|
2022-02-03 15:32:03 +08:00
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
return nil, FormatError(err)
|
2021-12-08 23:43:14 +08:00
|
|
|
}
|
2022-02-03 15:32:03 +08:00
|
|
|
|
2022-05-19 18:32:04 +08:00
|
|
|
return userRawList, nil
|
2021-12-08 23:43:14 +08:00
|
|
|
}
|
2022-07-26 21:41:20 +08:00
|
|
|
|
2022-08-07 09:23:46 +08:00
|
|
|
func deleteUser(ctx context.Context, tx *sql.Tx, delete *api.UserDelete) error {
|
2022-09-03 18:54:22 +08:00
|
|
|
result, err := tx.ExecContext(ctx, `
|
2022-07-26 22:32:26 +08:00
|
|
|
DELETE FROM user WHERE id = ?
|
2022-09-03 18:54:22 +08:00
|
|
|
`, delete.ID)
|
|
|
|
if err != nil {
|
2022-07-26 21:41:20 +08:00
|
|
|
return FormatError(err)
|
|
|
|
}
|
|
|
|
|
2023-02-17 23:55:56 +08:00
|
|
|
rows, err := result.RowsAffected()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-03 18:54:22 +08:00
|
|
|
if rows == 0 {
|
2022-11-06 12:21:58 +08:00
|
|
|
return &common.Error{Code: common.NotFound, Err: fmt.Errorf("user not found")}
|
2022-09-03 18:54:22 +08:00
|
|
|
}
|
|
|
|
|
2022-07-26 21:41:20 +08:00
|
|
|
return nil
|
|
|
|
}
|