From 5e62c2eac26ef986ed448ff095d62aa06893fec3 Mon Sep 17 00:00:00 2001 From: Dennis Smith Date: Tue, 11 Feb 2025 13:41:53 -0500 Subject: [PATCH] chore: migrate GET /my_profile to POST /profiles --- api/internal/handler/profile.go | 53 ++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 20 deletions(-) diff --git a/api/internal/handler/profile.go b/api/internal/handler/profile.go index 3c343575..499a8e69 100644 --- a/api/internal/handler/profile.go +++ b/api/internal/handler/profile.go @@ -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]())}, @@ -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",