From ec24600269dcdf9e43caa2576f728cf6c55f6ade Mon Sep 17 00:00:00 2001 From: James Tombleson Date: Sun, 31 Mar 2024 17:48:44 -0700 Subject: [PATCH] mostly pushing things into domain --- api/handlers/v1/demo.go | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/api/handlers/v1/demo.go b/api/handlers/v1/demo.go index 597cb6f..d8bfcdc 100644 --- a/api/handlers/v1/demo.go +++ b/api/handlers/v1/demo.go @@ -2,20 +2,14 @@ package v1 import ( "fmt" - "go-cook/api/models" + "go-cook/api/domain" "net/http" "github.com/labstack/echo/v4" ) -type HelloWhoResponse struct { - Success bool `json:"success"` - Error string `error:"error"` - Message string `json:"message"` -} - func (h *Handler) DemoHello(c echo.Context) error { - return c.JSON(http.StatusOK, HelloWhoResponse{ + return c.JSON(http.StatusOK, domain.HelloWhoResponse{ Success: true, Message: "Hello world!", }) @@ -23,27 +17,23 @@ func (h *Handler) DemoHello(c echo.Context) error { func (h *Handler) HelloWho(c echo.Context) error { name := c.Param("who") - return c.JSON(http.StatusOK, HelloWhoResponse{ + return c.JSON(http.StatusOK, domain.HelloWhoResponse{ Success: true, Message: fmt.Sprintf("Hello, %s", name), }) } -type HelloBodyRequest struct { - Name string `json:"name" validate:"required"` -} - func (h *Handler) HelloBody(c echo.Context) error { - request := HelloBodyRequest{} + request := domain.HelloBodyRequest{} err := (&echo.DefaultBinder{}).BindBody(c, &request) if err != nil { - return c.JSON(http.StatusBadRequest, HelloWhoResponse{ + return c.JSON(http.StatusBadRequest, domain.HelloWhoResponse{ Success: false, Error: err.Error(), }) } - return c.JSON(http.StatusOK, HelloWhoResponse{ + return c.JSON(http.StatusOK, domain.HelloWhoResponse{ Success: true, Message: fmt.Sprintf("Hello, %s", request.Name), }) @@ -52,10 +42,12 @@ func (h *Handler) HelloBody(c echo.Context) error { func (h *Handler) ProtectedRoute(c echo.Context) error { token, err := h.getJwtToken(c) if err != nil { - return c.JSON(http.StatusForbidden, models.ErrorResponse{ - HttpCode: http.StatusForbidden, - Message: err.Error(), - }) + h.ReturnUnauthorizedResponse(c, err.Error()) + } + + err = token.IsValid(domain.ScopeRecipeRead) + if err != nil { + h.ReturnUnauthorizedResponse(c, ErrJwtScopeMissing) } return c.JSON(http.StatusOK, token)