feat: register workspace user api

This commit is contained in:
Steven
2022-09-21 23:26:05 +08:00
parent 3364d1bf39
commit c0699f159e
6 changed files with 197 additions and 21 deletions

View File

@ -292,7 +292,7 @@ func findShortcutList(ctx context.Context, tx *sql.Tx, find *api.ShortcutFind) (
where = append(where, fmt.Sprintf("visibility in (%s)", strings.Join(list, ",")))
}
if v := find.MemberID; v != nil {
where, args = append(where, "workspace_id IN (SELECT workspace_id FROM workspace_user WHERE user_id = ? )"), append(args, *v)
where, args = append(where, "workspace_id IN (SELECT workspace_id FROM workspace_user WHERE user_id = ?)"), append(args, *v)
}
rows, err := tx.QueryContext(ctx, `

View File

@ -262,7 +262,7 @@ func findWorkspaceList(ctx context.Context, tx *sql.Tx, find *api.WorkspaceFind)
where, args = append(where, "name = ?"), append(args, *v)
}
if v := find.MemberID; v != nil {
where, args = append(where, "id IN (SELECT workspace_id FROM workspace_user WHERE user_id = ? )"), append(args, *v)
where, args = append(where, "id IN (SELECT workspace_id FROM workspace_user WHERE user_id = ?)"), append(args, *v)
}
query := `

View File

@ -29,6 +29,21 @@ func (raw *workspaceUserRaw) toWorkspaceUser() *api.WorkspaceUser {
}
}
func (s *Store) ComposeWorkspaceUser(ctx context.Context, workspaceUser *api.WorkspaceUser) error {
user, err := s.FindUser(ctx, &api.UserFind{
ID: &workspaceUser.UserID,
})
if err != nil {
return err
}
user.OpenID = ""
user.UserSettingList = nil
workspaceUser.User = user
return nil
}
func (s *Store) UpsertWorkspaceUser(ctx context.Context, upsert *api.WorkspaceUserUpsert) (*api.WorkspaceUser, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
@ -112,14 +127,19 @@ func (s *Store) DeleteWorkspaceUser(ctx context.Context, delete *api.WorkspaceUs
}
func upsertWorkspaceUser(ctx context.Context, tx *sql.Tx, upsert *api.WorkspaceUserUpsert) (*workspaceUserRaw, error) {
set := []string{"workspace_id", "user_id", "role"}
args := []interface{}{upsert.WorkspaceID, upsert.UserID, upsert.Role}
placeholder := []string{"?", "?", "?"}
if v := upsert.UpdatedTs; v != nil {
set, args, placeholder = append(set, "updated_ts"), append(args, *v), append(placeholder, "?")
}
query := `
INSERT INTO workspace_user (
workspace_id,
user_id,
role,
updated_ts
` + strings.Join(set, ", ") + `
)
VALUES (?, ?, ?, ?)
VALUES (` + strings.Join(placeholder, ",") + `)
ON CONFLICT(workspace_id, user_id) DO UPDATE
SET
role = EXCLUDED.role,
@ -127,12 +147,7 @@ func upsertWorkspaceUser(ctx context.Context, tx *sql.Tx, upsert *api.WorkspaceU
RETURNING workspace_id, user_id, role, created_ts, updated_ts
`
var workspaceUserRaw workspaceUserRaw
if err := tx.QueryRowContext(ctx, query,
upsert.WorkspaceID,
upsert.UserID,
upsert.Role,
upsert.UpdatedTs,
).Scan(
if err := tx.QueryRowContext(ctx, query, args...).Scan(
&workspaceUserRaw.WorkspaceID,
&workspaceUserRaw.UserID,
&workspaceUserRaw.Role,