Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc223fcf

Browse files
committed
refactor: User /profile and make it Update/PUT
1 parent1e1fb85 commitc223fcf

File tree

4 files changed

+26
-25
lines changed

4 files changed

+26
-25
lines changed

‎coderd/coderd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func New(options *Options) (http.Handler, func()) {
135135
r.Route("/{user}",func(r chi.Router) {
136136
r.Use(httpmw.ExtractUserParam(options.Database))
137137
r.Get("/",api.userByName)
138-
r.Patch("/",api.patchUserProfile)
138+
r.Put("/profile",api.updateUserProfile)
139139
r.Get("/organizations",api.organizationsByUser)
140140
r.Post("/organizations",api.postOrganizationsByUser)
141141
r.Post("/keys",api.postAPIKey)

‎coderd/users.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -270,33 +270,34 @@ func (*api) userByName(rw http.ResponseWriter, r *http.Request) {
270270
render.JSON(rw,r,convertUser(user))
271271
}
272272

273-
func (api*api)patchUserProfile(rw http.ResponseWriter,r*http.Request) {
273+
func (api*api)updateUserProfile(rw http.ResponseWriter,r*http.Request) {
274+
274275
user:=httpmw.UserParam(r)
275276

276-
varpatchUserProfile codersdk.PatchUserProfileRequest
277-
if!httpapi.Read(rw,r,&patchUserProfile) {
277+
varparams codersdk.UpdateUserProfileRequest
278+
if!httpapi.Read(rw,r,&params) {
278279
return
279280
}
280281

281-
ifpatchUserProfile.Name==nil {
282-
patchUserProfile.Name=&user.Name
282+
ifparams.Name==nil {
283+
params.Name=&user.Name
283284
}
284285

285286
existentUser,err:=api.Database.GetUserByEmailOrUsername(r.Context(), database.GetUserByEmailOrUsernameParams{
286-
Email:patchUserProfile.Email,
287-
Username:patchUserProfile.Username,
287+
Email:params.Email,
288+
Username:params.Username,
288289
})
289290
isDifferentUser:=existentUser.ID!=user.ID
290291

291292
iferr==nil&&isDifferentUser {
292293
responseErrors:= []httpapi.Error{}
293-
ifexistentUser.Email==patchUserProfile.Email {
294+
ifexistentUser.Email==params.Email {
294295
responseErrors=append(responseErrors, httpapi.Error{
295296
Field:"email",
296297
Code:"exists",
297298
})
298299
}
299-
ifexistentUser.Username==patchUserProfile.Username {
300+
ifexistentUser.Username==params.Username {
300301
responseErrors=append(responseErrors, httpapi.Error{
301302
Field:"username",
302303
Code:"exists",
@@ -317,9 +318,9 @@ func (api *api) patchUserProfile(rw http.ResponseWriter, r *http.Request) {
317318

318319
updatedUserProfile,err:=api.Database.UpdateUserProfile(r.Context(), database.UpdateUserProfileParams{
319320
ID:user.ID,
320-
Name:*patchUserProfile.Name,
321-
Email:patchUserProfile.Email,
322-
Username:patchUserProfile.Username,
321+
Name:*params.Name,
322+
Email:params.Email,
323+
Username:params.Username,
323324
UpdatedAt:database.Now(),
324325
})
325326

‎coderd/users_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -200,13 +200,13 @@ func TestPostUsers(t *testing.T) {
200200
})
201201
}
202202

203-
funcTestPatchUserProfile(t*testing.T) {
203+
funcTestUpdateUserProfile(t*testing.T) {
204204
t.Parallel()
205205
t.Run("UserNotFound",func(t*testing.T) {
206206
t.Parallel()
207207
client:=coderdtest.New(t,nil)
208208
coderdtest.CreateFirstUser(t,client)
209-
_,err:=client.PatchUserProfile(context.Background(),uuid.New(), codersdk.PatchUserProfileRequest{
209+
_,err:=client.UpdateUserProfile(context.Background(),uuid.New(), codersdk.UpdateUserProfileRequest{
210210
Username:"newusername",
211211
Email:"newemail@coder.com",
212212
})
@@ -227,7 +227,7 @@ func TestPatchUserProfile(t *testing.T) {
227227
Password:"password",
228228
OrganizationID:user.OrganizationID,
229229
})
230-
_,err=client.PatchUserProfile(context.Background(),codersdk.Me, codersdk.PatchUserProfileRequest{
230+
_,err=client.UpdateUserProfile(context.Background(),codersdk.Me, codersdk.UpdateUserProfileRequest{
231231
Username:"newusername",
232232
Email:existentUser.Email,
233233
})
@@ -246,7 +246,7 @@ func TestPatchUserProfile(t *testing.T) {
246246
Password:"password",
247247
OrganizationID:user.OrganizationID,
248248
})
249-
_,err=client.PatchUserProfile(context.Background(),codersdk.Me, codersdk.PatchUserProfileRequest{
249+
_,err=client.UpdateUserProfile(context.Background(),codersdk.Me, codersdk.UpdateUserProfileRequest{
250250
Username:existentUser.Username,
251251
Email:"newemail@coder.com",
252252
})
@@ -259,7 +259,7 @@ func TestPatchUserProfile(t *testing.T) {
259259
t.Parallel()
260260
client:=coderdtest.New(t,nil)
261261
coderdtest.CreateFirstUser(t,client)
262-
userProfile,err:=client.PatchUserProfile(context.Background(),codersdk.Me, codersdk.PatchUserProfileRequest{
262+
userProfile,err:=client.UpdateUserProfile(context.Background(),codersdk.Me, codersdk.UpdateUserProfileRequest{
263263
Username:"newusername",
264264
Email:"newemail@coder.com",
265265
})
@@ -273,7 +273,7 @@ func TestPatchUserProfile(t *testing.T) {
273273
client:=coderdtest.New(t,nil)
274274
coderdtest.CreateFirstUser(t,client)
275275
me,err:=client.User(context.Background(),codersdk.Me)
276-
userProfile,err:=client.PatchUserProfile(context.Background(),codersdk.Me, codersdk.PatchUserProfileRequest{
276+
userProfile,err:=client.UpdateUserProfile(context.Background(),codersdk.Me, codersdk.UpdateUserProfileRequest{
277277
Username:me.Username,
278278
Email:"newemail@coder.com",
279279
})
@@ -288,13 +288,13 @@ func TestPatchUserProfile(t *testing.T) {
288288
coderdtest.CreateFirstUser(t,client)
289289
me,_:=client.User(context.Background(),codersdk.Me)
290290
newName:="New Name"
291-
firstProfile,_:=client.PatchUserProfile(context.Background(),codersdk.Me, codersdk.PatchUserProfileRequest{
291+
firstProfile,_:=client.UpdateUserProfile(context.Background(),codersdk.Me, codersdk.UpdateUserProfileRequest{
292292
Username:me.Username,
293293
Email:me.Email,
294294
Name:&newName,
295295
})
296296
t.Log(firstProfile)
297-
userProfile,err:=client.PatchUserProfile(context.Background(),codersdk.Me, codersdk.PatchUserProfileRequest{
297+
userProfile,err:=client.UpdateUserProfile(context.Background(),codersdk.Me, codersdk.UpdateUserProfileRequest{
298298
Username:"newusername",
299299
Email:"newemail@coder.com",
300300
})

‎codersdk/users.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ type CreateUserRequest struct {
4242
OrganizationID uuid.UUID`json:"organization_id" validate:"required"`
4343
}
4444

45-
typePatchUserProfileRequeststruct {
45+
typeUpdateUserProfileRequeststruct {
4646
Emailstring`json:"email" validate:"required,email"`
4747
Usernamestring`json:"username" validate:"required,username"`
4848
Name*string`json:"name"`
@@ -122,9 +122,9 @@ func (c *Client) CreateUser(ctx context.Context, req CreateUserRequest) (User, e
122122
returnuser,json.NewDecoder(res.Body).Decode(&user)
123123
}
124124

125-
//PatchUserProfile enables callers to update profile information
126-
func (c*Client)PatchUserProfile(ctx context.Context,userID uuid.UUID,reqPatchUserProfileRequest) (User,error) {
127-
res,err:=c.request(ctx,http.MethodPatch,fmt.Sprintf("/api/v2/users/%s",uuidOrMe(userID)),req)
125+
//UpdateUserProfile enables callers to update profile information
126+
func (c*Client)UpdateUserProfile(ctx context.Context,userID uuid.UUID,reqUpdateUserProfileRequest) (User,error) {
127+
res,err:=c.request(ctx,http.MethodPut,fmt.Sprintf("/api/v2/users/%s/profile",uuidOrMe(userID)),req)
128128
iferr!=nil {
129129
returnUser{},err
130130
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp