memos/store/user.go

185 lines
3.5 KiB
Go
Raw Normal View History

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