-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(PXP-8992): PATCH /user/{username} #140
Changes from all commits
2647e32
f340bc2
81dc98d
13a29ab
8d48398
6d71c78
bb65174
a5b3529
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,11 @@ type User struct { | |
Policies []PolicyBinding `json:"policies"` | ||
} | ||
|
||
type UserWithScalars struct { | ||
Name *string `json:"name,omitempty"` | ||
Email *string `json:"email,omitempty"` | ||
} | ||
|
||
func (user *User) UnmarshalJSON(data []byte) error { | ||
fields := make(map[string]interface{}) | ||
err := json.Unmarshal(data, &fields) | ||
|
@@ -210,6 +215,33 @@ func (user *User) createInDb(db *sqlx.DB) *ErrorResponse { | |
return nil | ||
} | ||
|
||
func (user *User) updateInDb(db *sqlx.DB, name *string, email *string) *ErrorResponse { | ||
stmt := ` | ||
UPDATE usr | ||
SET | ||
name = COALESCE($1, name), | ||
email = COALESCE($2, email) | ||
WHERE | ||
name = $3 | ||
` | ||
result, err := db.Exec(stmt, name, email, user.Name) | ||
if err != nil { | ||
// this should only fail because the target name was not unique | ||
msg := fmt.Sprintf(`failed to update name to "%s": user with this name already exists`, *name) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what if a user with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the case that |
||
return newErrorResponse(msg, 409, &err) | ||
} | ||
|
||
rowsAffected, _ := result.RowsAffected() | ||
if rowsAffected == 0 { | ||
msg := fmt.Sprintf( | ||
"failed to update user: user does not exist: %s", | ||
user.Name, | ||
) | ||
return newErrorResponse(msg, 404, nil) | ||
} | ||
return nil | ||
} | ||
|
||
func (user *User) deleteInDb(db *sqlx.DB) *ErrorResponse { | ||
stmt := "DELETE FROM usr WHERE name = $1" | ||
_, err := db.Exec(stmt, user.Name) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel this /user API is a little odd using username as the id, since it's mutable now. I don't think there's much to do at this point b/c we don't want to rework the whole API... but we'll want to watch out b/c now clients must update their internal representation of username only on a successful PATCH (which is fine and makes sense, but it leaves room for some weird errors where a client might keep trying to patch something that's already been updated and starts getting errors b/c the username they're trying to patch doesn't exist anymore).
Maybe they didn't parse the success from Arborist right or persist it or something. Anything, since this is a new API and we are the client in most cases, this is fine. Just thinking out loud