memos/store/user.go

206 lines
4 KiB
Go
Raw Normal View History

2021-12-08 23:43:14 +08:00
package store
import (
"fmt"
2022-02-03 15:32:03 +08:00
"memos/api"
"memos/common"
2021-12-09 22:02:57 +08:00
"strings"
2021-12-08 23:43:14 +08:00
)
2022-02-03 15:32:03 +08:00
type UserService struct {
db *DB
2021-12-08 23:43:14 +08:00
}
2022-02-03 15:32:03 +08:00
func NewUserService(db *DB) *UserService {
return &UserService{db: db}
2021-12-09 22:02:57 +08:00
}
2022-02-03 15:32:03 +08:00
func (s *UserService) CreateUser(create *api.UserCreate) (*api.User, error) {
user, err := createUser(s.db, 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-02-03 15:32:03 +08:00
return user, nil
}
2021-12-08 23:43:14 +08:00
2022-02-03 15:32:03 +08:00
func (s *UserService) PatchUser(patch *api.UserPatch) (*api.User, error) {
user, err := patchUser(s.db, patch)
if err != nil {
return nil, err
2021-12-08 23:43:14 +08:00
}
2022-02-03 15:32:03 +08:00
return user, nil
2021-12-08 23:43:14 +08:00
}
2022-02-03 15:32:03 +08:00
func (s *UserService) FindUser(find *api.UserFind) (*api.User, error) {
list, err := findUserList(s.db, find)
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 {
return nil, nil
} else if len(list) > 1 {
return nil, &common.Error{Code: common.Conflict, Err: fmt.Errorf("found %d users with filter %+v, expect 1. ", len(list), find)}
}
2021-12-08 23:43:14 +08:00
2022-02-03 15:32:03 +08:00
return list[0], nil
2021-12-08 23:43:14 +08:00
}
2022-02-03 15:32:03 +08:00
func createUser(db *DB, create *api.UserCreate) (*api.User, error) {
row, err := db.Db.Query(`
INSERT INTO user (
email,
role,
2022-02-03 15:32:03 +08:00
name,
2022-02-06 16:19:20 +08:00
password_hash,
2022-02-03 15:32:03 +08:00
open_id
)
VALUES (?, ?, ?, ?, ?)
RETURNING id, email, role, name, password_hash, open_id, created_ts, updated_ts
2022-02-04 21:24:21 +08:00
`,
create.Email,
create.Role,
2022-02-04 21:24:21 +08:00
create.Name,
2022-02-06 16:19:20 +08:00
create.PasswordHash,
2022-05-03 02:05:43 +08:00
create.OpenID,
2022-02-04 21:24:21 +08:00
)
2022-02-03 15:32:03 +08:00
if err != nil {
return nil, FormatError(err)
}
defer row.Close()
row.Next()
var user api.User
if err := row.Scan(
2022-05-03 02:05:43 +08:00
&user.ID,
&user.Email,
&user.Role,
2022-02-03 15:32:03 +08:00
&user.Name,
2022-02-06 16:19:20 +08:00
&user.PasswordHash,
2022-05-03 02:05:43 +08:00
&user.OpenID,
2022-02-03 15:32:03 +08:00
&user.CreatedTs,
&user.UpdatedTs,
); err != nil {
return nil, FormatError(err)
}
return &user, nil
2021-12-08 23:43:14 +08:00
}
2022-02-03 15:32:03 +08:00
func patchUser(db *DB, patch *api.UserPatch) (*api.User, error) {
set, args := []string{}, []interface{}{}
2021-12-08 23:43:14 +08:00
if v := patch.Email; v != nil {
set, args = append(set, "email = ?"), append(args, v)
}
2022-02-03 15:32:03 +08:00
if v := patch.Name; v != nil {
2022-02-04 21:24:21 +08:00
set, args = append(set, "name = ?"), append(args, v)
2021-12-08 23:43:14 +08:00
}
2022-02-06 16:19:20 +08:00
if v := patch.PasswordHash; v != nil {
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-02-04 21:24:21 +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
row, err := db.Db.Query(`
UPDATE user
SET `+strings.Join(set, ", ")+`
WHERE id = ?
RETURNING id, email, role, name, password_hash, open_id, created_ts, updated_ts
2022-02-03 15:32:03 +08:00
`, args...)
if err != nil {
return nil, FormatError(err)
}
defer row.Close()
if row.Next() {
var user api.User
if err := row.Scan(
2022-05-03 02:05:43 +08:00
&user.ID,
&user.Email,
&user.Role,
2022-02-03 15:32:03 +08:00
&user.Name,
2022-02-06 16:19:20 +08:00
&user.PasswordHash,
2022-05-03 02:05:43 +08:00
&user.OpenID,
2022-02-03 15:32:03 +08:00
&user.CreatedTs,
&user.UpdatedTs,
); err != nil {
return nil, FormatError(err)
}
return &user, nil
2021-12-08 23:43:14 +08:00
}
2021-12-15 10:55:17 +08:00
2022-05-03 02:05:43 +08:00
return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("user ID not found: %d", patch.ID)}
2021-12-08 23:43:14 +08:00
}
2022-02-03 15:32:03 +08:00
func findUserList(db *DB, find *api.UserFind) ([]*api.User, error) {
2022-02-04 00:56:44 +08:00
where, args := []string{"1 = 1"}, []interface{}{}
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)
}
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-02-03 15:32:03 +08:00
if v := find.Name; v != nil {
where, args = append(where, "name = ?"), append(args, *v)
}
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-02-03 15:32:03 +08:00
rows, err := db.Db.Query(`
SELECT
id,
email,
role,
2022-02-03 15:32:03 +08:00
name,
2022-02-06 16:19:20 +08:00
password_hash,
2022-02-03 15:32:03 +08:00
open_id,
2022-02-04 00:56:44 +08:00
created_ts,
2022-02-03 15:32:03 +08:00
updated_ts
FROM user
WHERE `+strings.Join(where, " AND "),
args...,
)
if err != nil {
return nil, FormatError(err)
}
defer rows.Close()
list := make([]*api.User, 0)
for rows.Next() {
var user api.User
if err := rows.Scan(
2022-05-03 02:05:43 +08:00
&user.ID,
&user.Email,
&user.Role,
2022-02-03 15:32:03 +08:00
&user.Name,
2022-02-06 16:19:20 +08:00
&user.PasswordHash,
2022-05-03 02:05:43 +08:00
&user.OpenID,
2022-02-03 15:32:03 +08:00
&user.CreatedTs,
&user.UpdatedTs,
); err != nil {
2022-02-04 00:56:44 +08:00
fmt.Println(err)
2022-02-03 15:32:03 +08:00
return nil, FormatError(err)
}
list = append(list, &user)
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
return list, nil
2021-12-08 23:43:14 +08:00
}