From ad0cf43aca44d76179ac57fe8aa12bdf5ca3cf90 Mon Sep 17 00:00:00 2001 From: Hayden <64056131+hay-kot@users.noreply.github.com> Date: Fri, 28 Oct 2022 17:47:52 -0800 Subject: [PATCH] more route cleanup --- backend/app/api/handlers/v1/partials.go | 15 +++++++++--- backend/app/api/handlers/v1/v1_ctrl_items.go | 2 +- .../handlers/v1/v1_ctrl_items_attachments.go | 23 ++++++++----------- backend/app/api/handlers/v1/v1_ctrl_labels.go | 2 +- .../app/api/handlers/v1/v1_ctrl_locations.go | 2 +- 5 files changed, 24 insertions(+), 20 deletions(-) diff --git a/backend/app/api/handlers/v1/partials.go b/backend/app/api/handlers/v1/partials.go index e95c7a6..0c025cf 100644 --- a/backend/app/api/handlers/v1/partials.go +++ b/backend/app/api/handlers/v1/partials.go @@ -9,12 +9,21 @@ import ( "github.com/rs/zerolog/log" ) -func (ctrl *V1Controller) routeID(w http.ResponseWriter, r *http.Request) (uuid.UUID, error) { - ID, err := uuid.Parse(chi.URLParam(r, "id")) +// routeID extracts the ID from the request URL. If the ID is not in a valid +// format, an error is returned. If a error is returned, it can be directly returned +// from the handler. the validate.ErrInvalidID error is known by the error middleware +// and will be handled accordingly. +// +// Example: /api/v1/ac614db5-d8b8-4659-9b14-6e913a6eb18a -> uuid.UUID{ac614db5-d8b8-4659-9b14-6e913a6eb18a} +func (ctrl *V1Controller) routeID(r *http.Request) (uuid.UUID, error) { + return ctrl.routeUUID(r, "id") +} + +func (ctrl *V1Controller) routeUUID(r *http.Request, key string) (uuid.UUID, error) { + ID, err := uuid.Parse(chi.URLParam(r, key)) if err != nil { log.Err(err).Msg("failed to parse id") return uuid.Nil, validate.ErrInvalidID } - return ID, nil } diff --git a/backend/app/api/handlers/v1/v1_ctrl_items.go b/backend/app/api/handlers/v1/v1_ctrl_items.go index 367db26..84591ca 100644 --- a/backend/app/api/handlers/v1/v1_ctrl_items.go +++ b/backend/app/api/handlers/v1/v1_ctrl_items.go @@ -137,7 +137,7 @@ func (ctrl *V1Controller) HandleItemUpdate() server.HandlerFunc { func (ctrl *V1Controller) handleItemsGeneral() server.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) error { ctx := services.NewContext(r.Context()) - ID, err := ctrl.routeID(w, r) + ID, err := ctrl.routeID(r) if err != nil { return err } diff --git a/backend/app/api/handlers/v1/v1_ctrl_items_attachments.go b/backend/app/api/handlers/v1/v1_ctrl_items_attachments.go index b0f99c6..327b013 100644 --- a/backend/app/api/handlers/v1/v1_ctrl_items_attachments.go +++ b/backend/app/api/handlers/v1/v1_ctrl_items_attachments.go @@ -5,8 +5,6 @@ import ( "fmt" "net/http" - "github.com/go-chi/chi/v5" - "github.com/google/uuid" "github.com/hay-kot/homebox/backend/ent/attachment" "github.com/hay-kot/homebox/backend/internal/repo" "github.com/hay-kot/homebox/backend/internal/services" @@ -71,7 +69,7 @@ func (ctrl *V1Controller) HandleItemAttachmentCreate() server.HandlerFunc { attachmentType = attachment.TypeAttachment.String() } - id, err := ctrl.routeID(w, r) + id, err := ctrl.routeID(r) if err != nil { return err } @@ -161,36 +159,33 @@ func (ctrl *V1Controller) HandleItemAttachmentUpdate() server.HandlerFunc { } func (ctrl *V1Controller) handleItemAttachmentsHandler(w http.ResponseWriter, r *http.Request) error { - ID, err := ctrl.routeID(w, r) + ID, err := ctrl.routeID(r) if err != nil { return err } - attachmentId, err := uuid.Parse(chi.URLParam(r, "attachment_id")) + attachmentID, err := ctrl.routeUUID(r, "attachment_id") if err != nil { - log.Err(err).Msg("failed to parse attachment_id param") - return validate.NewRequestError(err, http.StatusBadRequest) + return err } ctx := services.NewContext(r.Context()) - switch r.Method { - // Token Handler case http.MethodGet: - token, err := ctrl.svc.Items.AttachmentToken(ctx, ID, attachmentId) + token, err := ctrl.svc.Items.AttachmentToken(ctx, ID, attachmentID) if err != nil { switch err { case services.ErrNotFound: log.Err(err). - Str("id", attachmentId.String()). + Str("id", attachmentID.String()). Msg("failed to find attachment with id") return validate.NewRequestError(err, http.StatusNotFound) case services.ErrFileNotFound: log.Err(err). - Str("id", attachmentId.String()). + Str("id", attachmentID.String()). Msg("failed to find file path for attachment with id") log.Warn().Msg("attachment with no file path removed from database") @@ -206,7 +201,7 @@ func (ctrl *V1Controller) handleItemAttachmentsHandler(w http.ResponseWriter, r // Delete Attachment Handler case http.MethodDelete: - err = ctrl.svc.Items.AttachmentDelete(r.Context(), ctx.GID, ID, attachmentId) + err = ctrl.svc.Items.AttachmentDelete(r.Context(), ctx.GID, ID, attachmentID) if err != nil { log.Err(err).Msg("failed to delete attachment") return validate.NewRequestError(err, http.StatusInternalServerError) @@ -223,7 +218,7 @@ func (ctrl *V1Controller) handleItemAttachmentsHandler(w http.ResponseWriter, r return validate.NewRequestError(err, http.StatusBadRequest) } - attachment.ID = attachmentId + attachment.ID = attachmentID val, err := ctrl.svc.Items.AttachmentUpdate(ctx, ID, &attachment) if err != nil { log.Err(err).Msg("failed to delete attachment") diff --git a/backend/app/api/handlers/v1/v1_ctrl_labels.go b/backend/app/api/handlers/v1/v1_ctrl_labels.go index d9863fb..f02a929 100644 --- a/backend/app/api/handlers/v1/v1_ctrl_labels.go +++ b/backend/app/api/handlers/v1/v1_ctrl_labels.go @@ -96,7 +96,7 @@ func (ctrl *V1Controller) HandleLabelUpdate() server.HandlerFunc { func (ctrl *V1Controller) handleLabelsGeneral() server.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) error { ctx := services.NewContext(r.Context()) - ID, err := ctrl.routeID(w, r) + ID, err := ctrl.routeID(r) if err != nil { return err } diff --git a/backend/app/api/handlers/v1/v1_ctrl_locations.go b/backend/app/api/handlers/v1/v1_ctrl_locations.go index 833467b..775cf76 100644 --- a/backend/app/api/handlers/v1/v1_ctrl_locations.go +++ b/backend/app/api/handlers/v1/v1_ctrl_locations.go @@ -98,7 +98,7 @@ func (ctrl *V1Controller) HandleLocationUpdate() server.HandlerFunc { func (ctrl *V1Controller) handleLocationGeneral() server.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) error { ctx := services.NewContext(r.Context()) - ID, err := ctrl.routeID(w, r) + ID, err := ctrl.routeID(r) if err != nil { return err }