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,
password,
open_id
)
VALUES (?, ?, ?)
RETURNING id, name, password, open_id, created_ts, updated_ts
2022-02-04 16:24:21 +03:00
`,
create.Name,
create.Password,
create.OpenId,
)
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(
&user.Id,
&user.Name,
&user.Password,
&user.OpenId,
&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-03 10:32:03 +03:00
if v := patch.Password; v != nil {
2022-02-04 16:24:21 +03:00
set, args = append(set, "password = ?"), append(args, v)
2022-02-03 10:32:03 +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-02-03 10:32:03 +03:00
args = append(args, patch.Id)
row, err := db.Db.Query(`
UPDATE user
SET `+strings.Join(set, ", ")+`
WHERE id = ?
RETURNING id, name, password, open_id, created_ts, updated_ts
`, args...)
if err != nil {
return nil, FormatError(err)
}
defer row.Close()
if row.Next() {
var user api.User
if err := row.Scan(
&user.Id,
&user.Name,
&user.Password,
&user.OpenId,
&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-02-03 10:32:03 +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
if v := find.Id; v != nil {
where, args = append(where, "id = ?"), append(args, *v)
}
if v := find.Name; v != nil {
where, args = append(where, "name = ?"), append(args, *v)
}
if v := find.OpenId; v != nil {
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,
password,
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(
&user.Id,
&user.Name,
&user.Password,
&user.OpenId,
&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
}