Skip to content

feat: Add update profile endpoint #916

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

Merged
merged 16 commits into from
Apr 12, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ func New(options *Options) (http.Handler, func()) {
r.Route("/{user}", func(r chi.Router) {
r.Use(httpmw.ExtractUserParam(options.Database))
r.Get("/", api.userByName)
r.Patch("/", api.patchUser)
r.Get("/organizations", api.organizationsByUser)
r.Post("/organizations", api.postOrganizationsByUser)
r.Post("/keys", api.postAPIKey)
Expand Down
16 changes: 16 additions & 0 deletions coderd/database/databasefake/databasefake.go
Original file line number Diff line number Diff line change
Expand Up @@ -1027,6 +1027,22 @@ func (q *fakeQuerier) InsertUser(_ context.Context, arg database.InsertUserParam
return user, nil
}

func (q *fakeQuerier) UpdateUser(_ context.Context, arg database.UpdateUserParams) (database.User, error) {
q.mutex.Lock()
defer q.mutex.Unlock()

for index, user := range q.users {
if user.ID != arg.ID {
continue
}
user.Email = arg.Email
user.Username = arg.Username
q.users[index] = user
return user, nil
}
return database.User{}, sql.ErrNoRows
}

func (q *fakeQuerier) InsertWorkspace(_ context.Context, arg database.InsertWorkspaceParams) (database.Workspace, error) {
q.mutex.Lock()
defer q.mutex.Unlock()
Expand Down
1 change: 1 addition & 0 deletions coderd/database/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 42 additions & 0 deletions coderd/database/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions coderd/database/queries/users.sql
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,16 @@ INSERT INTO
)
VALUES
($1, $2, $3, $4, FALSE, $5, $6, $7, $8) RETURNING *;

-- name: UpdateUser :one
UPDATE
users
SET
email = $2,
"name" = $3,
username = $4,
updated_at = CURRENT_TIMESTAMP
WHERE
id = $1
RETURNING *;

26 changes: 26 additions & 0 deletions coderd/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,32 @@ func (*api) userByName(rw http.ResponseWriter, r *http.Request) {
render.JSON(rw, r, convertUser(user))
}

func (api *api) patchUser(rw http.ResponseWriter, r *http.Request) {
user := httpmw.UserParam(r)

var patchUser codersdk.PatchUserRequest
if !httpapi.Read(rw, r, &patchUser) {
return
}

updatedUser, err := api.Database.UpdateUser(r.Context(), database.UpdateUserParams{
ID: user.ID,
Name: patchUser.Name,
Email: patchUser.Email,
Username: patchUser.Username,
})

if err != nil {
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
Message: fmt.Sprintf("patch user: %s", err.Error()),
})
return
}

render.Status(r, http.StatusOK)
render.JSON(rw, r, convertUser(updatedUser))
}

// Returns organizations the parameterized user has access to.
func (api *api) organizationsByUser(rw http.ResponseWriter, r *http.Request) {
user := httpmw.UserParam(r)
Expand Down
13 changes: 13 additions & 0 deletions coderd/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,19 @@ func TestPostUsers(t *testing.T) {
})
}

func TestPatchUser(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
coderdtest.CreateFirstUser(t, client)
user, err := client.PatchUser(context.Background(), codersdk.Me, codersdk.PatchUserRequest{
Username: "newusername",
Email: "newemail@coder.com",
})
require.NoError(t, err)
require.Equal(t, user.Username, "newusername")
require.Equal(t, user.Email, "newemail@coder.com")
}

func TestUserByName(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
Expand Down
20 changes: 20 additions & 0 deletions codersdk/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ type CreateUserRequest struct {
OrganizationID uuid.UUID `json:"organization_id" validate:"required"`
}

type PatchUserRequest struct {
Email string `json:"email" validate:"required,email"`
Username string `json:"username" validate:"required,username"`
Name string `json:"name"`
}

// LoginWithPasswordRequest enables callers to authenticate with email and password.
type LoginWithPasswordRequest struct {
Email string `json:"email" validate:"required,email"`
Expand Down Expand Up @@ -115,6 +121,20 @@ func (c *Client) CreateUser(ctx context.Context, req CreateUserRequest) (User, e
return user, json.NewDecoder(res.Body).Decode(&user)
}

// Patch User
func (c *Client) PatchUser(ctx context.Context, userID uuid.UUID, req PatchUserRequest) (User, error) {
res, err := c.request(ctx, http.MethodPatch, fmt.Sprintf("/api/v2/users/%s", uuidOrMe(userID)), req)
if err != nil {
return User{}, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return User{}, readBodyAsError(res)
}
var user User
return user, json.NewDecoder(res.Body).Decode(&user)
}

// CreateAPIKey generates an API key for the user ID provided.
func (c *Client) CreateAPIKey(ctx context.Context, userID uuid.UUID) (*GenerateAPIKeyResponse, error) {
res, err := c.request(ctx, http.MethodPost, fmt.Sprintf("/api/v2/users/%s/keys", uuidOrMe(userID)), nil)
Expand Down