From 576185088307c4741e635e393d2bab994244788a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Antunes?= Date: Wed, 24 Apr 2024 19:33:49 -0300 Subject: [PATCH] feat: Allow fetching Origin data --- internal/api/controller.go | 24 ++++++++++++++++++++++++ internal/api/request.go | 7 +++++++ internal/service/routing.go | 5 +++++ 3 files changed, 36 insertions(+) diff --git a/internal/api/controller.go b/internal/api/controller.go index 1452a2c..0e2af5c 100644 --- a/internal/api/controller.go +++ b/internal/api/controller.go @@ -243,3 +243,27 @@ func (o *OriginController) UpdateOrigin(ctx *gin.Context) { Message: "Origin updated successfully", }) } + +func (o *OriginController) GetOrigin(ctx *gin.Context) { + id, err := uuid.Parse(ctx.Param("id")) + if err != nil { + ctx.JSON(http.StatusBadRequest, ErrorResponse{ + Error: Error{Message: "BadRequest: invalid UUID format"}, + }) + + return + } + + origin, err := o.originHandler.Get(ctx, id) + if err != nil { + ctx.JSON(http.StatusInternalServerError, ErrorResponse{ + Error: Error{Message: "InternalServerError: failed to get origin"}, + }) + + return + } + + ctx.JSON(http.StatusOK, SuccessResponse{ + Data: FromOrigin(origin), + }) +} diff --git a/internal/api/request.go b/internal/api/request.go index 05b77d2..497a118 100644 --- a/internal/api/request.go +++ b/internal/api/request.go @@ -103,3 +103,10 @@ func (o *OriginData) ToOrigin() service.Origin { Address: o.Address, } } + +func FromOrigin(origin service.Origin) OriginData { + return OriginData{ + ID: origin.ID, + Address: origin.Address, + } +} diff --git a/internal/service/routing.go b/internal/service/routing.go index 8a8ad65..3603083 100644 --- a/internal/service/routing.go +++ b/internal/service/routing.go @@ -18,6 +18,7 @@ type Origin struct { type OriginHandler interface { Update(context.Context, Origin) error + Get(context.Context, uuid.UUID) (Origin, error) } type originHandler struct { @@ -33,3 +34,7 @@ func NewOriginHandler(store OriginStore) OriginHandler { func (h *originHandler) Update(ctx context.Context, origin Origin) error { return h.store.Update(ctx, origin) } + +func (h *originHandler) Get(ctx context.Context, id uuid.UUID) (Origin, error) { + return h.store.Get(ctx, id) +}