mirror of
https://github.com/aykhans/slash-e.git
synced 2025-06-14 20:07:50 +00:00
feat: implement api v1 for users
This commit is contained in:
@ -343,16 +343,8 @@ func (raw *shortcutRaw) toShortcut() *api.Shortcut {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Store) ComposeShortcut(ctx context.Context, shortcut *api.Shortcut) error {
|
||||
user, err := s.FindUser(ctx, &api.UserFind{
|
||||
ID: &shortcut.CreatorID,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
user.OpenID = ""
|
||||
shortcut.Creator = user
|
||||
|
||||
func (*Store) ComposeShortcut(_ context.Context, _ *api.Shortcut) error {
|
||||
// TODO: implement this.
|
||||
return nil
|
||||
}
|
||||
|
||||
|
346
store/user.go
346
store/user.go
@ -5,9 +5,6 @@ import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/boojack/shortify/api"
|
||||
"github.com/boojack/shortify/internal/errorutil"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
@ -50,7 +47,7 @@ type DeleteUser struct {
|
||||
ID int
|
||||
}
|
||||
|
||||
func (s *Store) CreateUserV1(ctx context.Context, create *User) (*User, error) {
|
||||
func (s *Store) CreateUser(ctx context.Context, create *User) (*User, error) {
|
||||
tx, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -88,10 +85,11 @@ func (s *Store) CreateUserV1(ctx context.Context, create *User) (*User, error) {
|
||||
}
|
||||
|
||||
user := create
|
||||
s.userCache.Store(user.ID, user)
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (s *Store) UpdateUserV1(ctx context.Context, update *UpdateUser) (*User, error) {
|
||||
func (s *Store) UpdateUser(ctx context.Context, update *UpdateUser) (*User, error) {
|
||||
tx, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -148,6 +146,7 @@ func (s *Store) UpdateUserV1(ctx context.Context, update *UpdateUser) (*User, er
|
||||
return nil, err
|
||||
}
|
||||
|
||||
s.userCache.Store(user.ID, user)
|
||||
return user, nil
|
||||
}
|
||||
|
||||
@ -163,10 +162,20 @@ func (s *Store) ListUsers(ctx context.Context, find *FindUser) ([]*User, error)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, user := range list {
|
||||
s.userCache.Store(user.ID, user)
|
||||
}
|
||||
|
||||
return list, nil
|
||||
}
|
||||
|
||||
func (s *Store) GetUserV1(ctx context.Context, find *FindUser) (*User, error) {
|
||||
func (s *Store) GetUser(ctx context.Context, find *FindUser) (*User, error) {
|
||||
if find.ID != nil {
|
||||
if cache, ok := s.workspaceCache.Load(*find.ID); ok {
|
||||
return cache.(*User), nil
|
||||
}
|
||||
}
|
||||
|
||||
tx, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -185,7 +194,7 @@ func (s *Store) GetUserV1(ctx context.Context, find *FindUser) (*User, error) {
|
||||
return list[0], nil
|
||||
}
|
||||
|
||||
func (s *Store) DeleteUserV1(ctx context.Context, delete *DeleteUser) error {
|
||||
func (s *Store) DeleteUser(ctx context.Context, delete *DeleteUser) error {
|
||||
tx, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -203,6 +212,8 @@ func (s *Store) DeleteUserV1(ctx context.Context, delete *DeleteUser) error {
|
||||
return err
|
||||
}
|
||||
|
||||
s.userCache.Delete(delete.ID)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -274,324 +285,3 @@ func listUsers(ctx context.Context, tx *sql.Tx, find *FindUser) ([]*User, error)
|
||||
|
||||
return list, nil
|
||||
}
|
||||
|
||||
// userRaw is the store model for an User.
|
||||
// Fields have exactly the same meanings as User.
|
||||
type userRaw struct {
|
||||
ID int
|
||||
|
||||
// Standard fields
|
||||
CreatedTs int64
|
||||
UpdatedTs int64
|
||||
RowStatus api.RowStatus
|
||||
|
||||
// Domain specific fields
|
||||
Email string
|
||||
DisplayName string
|
||||
PasswordHash string
|
||||
OpenID string
|
||||
Role api.Role
|
||||
}
|
||||
|
||||
func (raw *userRaw) toUser() *api.User {
|
||||
return &api.User{
|
||||
ID: raw.ID,
|
||||
|
||||
CreatedTs: raw.CreatedTs,
|
||||
UpdatedTs: raw.UpdatedTs,
|
||||
RowStatus: raw.RowStatus,
|
||||
|
||||
Email: raw.Email,
|
||||
DisplayName: raw.DisplayName,
|
||||
PasswordHash: raw.PasswordHash,
|
||||
OpenID: raw.OpenID,
|
||||
Role: raw.Role,
|
||||
}
|
||||
}
|
||||
|
||||
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, err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
userRaw, err := createUser(ctx, tx, create)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := tx.Commit(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
s.userCache.Store(userRaw.ID, userRaw)
|
||||
user := userRaw.toUser()
|
||||
return user, nil
|
||||
}
|
||||
|
||||
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, err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
userRaw, err := patchUser(ctx, tx, patch)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := tx.Commit(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
s.userCache.Store(userRaw.ID, userRaw)
|
||||
user := userRaw.toUser()
|
||||
return user, nil
|
||||
}
|
||||
|
||||
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, err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
userRawList, err := findUserList(ctx, tx, find)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
list := []*api.User{}
|
||||
for _, userRaw := range userRawList {
|
||||
s.userCache.Store(userRaw.ID, userRaw)
|
||||
list = append(list, userRaw.toUser())
|
||||
}
|
||||
|
||||
return list, nil
|
||||
}
|
||||
|
||||
func (s *Store) FindUser(ctx context.Context, find *api.UserFind) (*api.User, error) {
|
||||
if find.ID != nil {
|
||||
if cache, ok := s.userCache.Load(*find.ID); ok {
|
||||
return cache.(*userRaw).toUser(), nil
|
||||
}
|
||||
}
|
||||
|
||||
tx, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
list, err := findUserList(ctx, tx, find)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(list) == 0 {
|
||||
return nil, &errorutil.Error{Code: errorutil.NotFound, Err: fmt.Errorf("not found user with filter %+v", find)}
|
||||
} else if len(list) > 1 {
|
||||
return nil, &errorutil.Error{Code: errorutil.Conflict, Err: fmt.Errorf("found %d users with filter %+v, expect 1", len(list), find)}
|
||||
}
|
||||
|
||||
userRaw := list[0]
|
||||
s.userCache.Store(userRaw.ID, userRaw)
|
||||
user := userRaw.toUser()
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (s *Store) DeleteUser(ctx context.Context, delete *api.UserDelete) error {
|
||||
tx, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
err = deleteUser(ctx, tx, delete)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := tx.Commit(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
s.userCache.Delete(delete.ID)
|
||||
return nil
|
||||
}
|
||||
|
||||
func createUser(ctx context.Context, tx *sql.Tx, create *api.UserCreate) (*userRaw, error) {
|
||||
query := `
|
||||
INSERT INTO user (
|
||||
email,
|
||||
display_name,
|
||||
password_hash,
|
||||
open_id,
|
||||
role
|
||||
)
|
||||
VALUES (?, ?, ?, ?)
|
||||
RETURNING id, created_ts, updated_ts, row_status, email, display_name, password_hash, open_id, role
|
||||
`
|
||||
var userRaw userRaw
|
||||
if err := tx.QueryRowContext(ctx, query,
|
||||
create.Email,
|
||||
create.DisplayName,
|
||||
create.PasswordHash,
|
||||
create.OpenID,
|
||||
create.Role,
|
||||
).Scan(
|
||||
&userRaw.ID,
|
||||
&userRaw.CreatedTs,
|
||||
&userRaw.UpdatedTs,
|
||||
&userRaw.RowStatus,
|
||||
&userRaw.Email,
|
||||
&userRaw.DisplayName,
|
||||
&userRaw.PasswordHash,
|
||||
&userRaw.OpenID,
|
||||
&userRaw.Role,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &userRaw, nil
|
||||
}
|
||||
|
||||
func patchUser(ctx context.Context, tx *sql.Tx, patch *api.UserPatch) (*userRaw, error) {
|
||||
set, args := []string{}, []any{}
|
||||
|
||||
if v := patch.RowStatus; v != nil {
|
||||
set, args = append(set, "row_status = ?"), append(args, *v)
|
||||
}
|
||||
if v := patch.Email; v != nil {
|
||||
set, args = append(set, "email = ?"), append(args, *v)
|
||||
}
|
||||
if v := patch.DisplayName; v != nil {
|
||||
set, args = append(set, "display_name = ?"), append(args, *v)
|
||||
}
|
||||
if v := patch.PasswordHash; v != nil {
|
||||
set, args = append(set, "password_hash = ?"), append(args, *v)
|
||||
}
|
||||
if v := patch.OpenID; v != nil {
|
||||
set, args = append(set, "open_id = ?"), append(args, *v)
|
||||
}
|
||||
|
||||
args = append(args, patch.ID)
|
||||
|
||||
query := `
|
||||
UPDATE user
|
||||
SET ` + strings.Join(set, ", ") + `
|
||||
WHERE id = ?
|
||||
RETURNING id, created_ts, updated_ts, row_status, email, display_name, password_hash, open_id, role
|
||||
`
|
||||
row, err := tx.QueryContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer row.Close()
|
||||
|
||||
if row.Next() {
|
||||
var userRaw userRaw
|
||||
if err := row.Scan(
|
||||
&userRaw.ID,
|
||||
&userRaw.CreatedTs,
|
||||
&userRaw.UpdatedTs,
|
||||
&userRaw.RowStatus,
|
||||
&userRaw.Email,
|
||||
&userRaw.DisplayName,
|
||||
&userRaw.PasswordHash,
|
||||
&userRaw.OpenID,
|
||||
&userRaw.Role,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := row.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &userRaw, nil
|
||||
}
|
||||
|
||||
return nil, &errorutil.Error{Code: errorutil.NotFound, Err: fmt.Errorf("user ID not found: %d", patch.ID)}
|
||||
}
|
||||
|
||||
func findUserList(ctx context.Context, tx *sql.Tx, find *api.UserFind) ([]*userRaw, error) {
|
||||
where, args := []string{"1 = 1"}, []any{}
|
||||
|
||||
if v := find.ID; v != nil {
|
||||
where, args = append(where, "id = ?"), append(args, *v)
|
||||
}
|
||||
if v := find.Email; v != nil {
|
||||
where, args = append(where, "email = ?"), append(args, *v)
|
||||
}
|
||||
if v := find.OpenID; v != nil {
|
||||
where, args = append(where, "open_id = ?"), append(args, *v)
|
||||
}
|
||||
if v := find.Role; v != nil {
|
||||
where, args = append(where, "role = ?"), append(args, *v)
|
||||
}
|
||||
|
||||
query := `
|
||||
SELECT
|
||||
id,
|
||||
created_ts,
|
||||
updated_ts,
|
||||
row_status,
|
||||
email,
|
||||
display_name,
|
||||
password_hash,
|
||||
open_id,
|
||||
role
|
||||
FROM user
|
||||
WHERE ` + strings.Join(where, " AND ") + `
|
||||
ORDER BY updated_ts DESC, created_ts DESC
|
||||
`
|
||||
rows, err := tx.QueryContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
userRawList := make([]*userRaw, 0)
|
||||
for rows.Next() {
|
||||
var userRaw userRaw
|
||||
if err := rows.Scan(
|
||||
&userRaw.ID,
|
||||
&userRaw.CreatedTs,
|
||||
&userRaw.UpdatedTs,
|
||||
&userRaw.RowStatus,
|
||||
&userRaw.Email,
|
||||
&userRaw.DisplayName,
|
||||
&userRaw.PasswordHash,
|
||||
&userRaw.OpenID,
|
||||
&userRaw.Role,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
userRawList = append(userRawList, &userRaw)
|
||||
}
|
||||
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return userRawList, nil
|
||||
}
|
||||
|
||||
func deleteUser(ctx context.Context, tx *sql.Tx, delete *api.UserDelete) error {
|
||||
result, err := tx.ExecContext(ctx, `
|
||||
DELETE FROM user WHERE id = ?
|
||||
`, delete.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
rows, _ := result.RowsAffected()
|
||||
if rows == 0 {
|
||||
return &errorutil.Error{Code: errorutil.NotFound, Err: fmt.Errorf("user ID not found: %d", delete.ID)}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@ -191,7 +191,7 @@ func (raw *workspaceUserRaw) toWorkspaceUser() *api.WorkspaceUser {
|
||||
}
|
||||
|
||||
func (s *Store) ComposeWorkspaceUser(ctx context.Context, workspaceUser *api.WorkspaceUser) error {
|
||||
user, err := s.FindUser(ctx, &api.UserFind{
|
||||
user, err := s.GetUser(ctx, &FindUser{
|
||||
ID: &workspaceUser.UserID,
|
||||
})
|
||||
if err != nil {
|
||||
@ -199,7 +199,7 @@ func (s *Store) ComposeWorkspaceUser(ctx context.Context, workspaceUser *api.Wor
|
||||
}
|
||||
|
||||
workspaceUser.Email = user.Email
|
||||
workspaceUser.DisplayName = user.DisplayName
|
||||
workspaceUser.DisplayName = user.Nickname
|
||||
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user