Skip to content

Commit

Permalink
chore: migrate GET /my_profile to POST /profiles
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisgsmith committed Feb 11, 2025
1 parent 796d18c commit 5e62c2e
Showing 1 changed file with 33 additions and 20 deletions.
53 changes: 33 additions & 20 deletions api/internal/handler/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,26 +47,6 @@ func (h *ApiHandler) createProfile(ctx context.Context, claims dto.ProfileClaims
func (h *ApiHandler) RegisterProfile(api huma.API) {
registry := api.OpenAPI().Components.Schemas

huma.Register(api, huma.Operation{
Middlewares: h.Cac,
Security: cacSecurity,
OperationID: "profile-create",
Method: http.MethodPost,
Path: "/profiles",
Summary: "creates a user profile",
Tags: profileTags,
DefaultStatus: http.StatusCreated,
}, func(ctx context.Context, input *struct{}) (*Response[db.ProfileCreateRow], error) {
claims := ctx.Value(ctxkey.ProfileClaims).(dto.ProfileClaims)

a, err := h.createProfile(ctx, claims)
if err != nil {
return nil, err
}

return NewResponse(a), nil
})

profileGetOrCreateResponses := map[string]*huma.Response{
"201": {Content: map[string]*huma.MediaType{
"application/json": {Schema: huma.SchemaFromType(registry, reflect.TypeFor[db.ProfileCreateRow]())},
Expand All @@ -77,6 +57,39 @@ func (h *ApiHandler) RegisterProfile(api huma.API) {
}

huma.Register(api, huma.Operation{
Middlewares: h.Cac,
Security: cacSecurity,
OperationID: "profile-create",
Method: http.MethodPost,
Path: "/profiles",
Summary: "creates a user profile",
Responses: profileGetOrCreateResponses,
Tags: profileTags,
}, func(ctx context.Context, input *struct{}) (*ResponseWithStatus[any], error) {
claims := ctx.Value(ctxkey.ProfileClaims).(dto.ProfileClaims)

p, err := h.DBService.ProfileGetWithTokensForClaims(ctx, claims)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
a, err := h.createProfile(ctx, claims)
if err != nil {
return nil, err
}
return NewResponseWithStatus(http.StatusCreated, any(a)), nil
}
return nil, httperr.InternalServerError(err)
}

a, err := h.DBService.ProfileUpdateForClaims(ctx, p, claims)
if err != nil {
return nil, httperr.InternalServerError(err)
}

return NewResponseWithStatus(http.StatusOK, any(a)), nil
})

huma.Register(api, huma.Operation{
Deprecated: true,
Middlewares: h.Cac,
Security: cacSecurity,
OperationID: "profile-get-or-create-for-claims",
Expand Down

0 comments on commit 5e62c2e

Please sign in to comment.