go-cook/internal/handlers/v1/demo.go

59 lines
1.3 KiB
Go

package v1
import (
"fmt"
"net/http"
"git.jamestombleson.com/jtom38/go-cook/internal/domain"
"github.com/labstack/echo/v4"
)
func (h *Handler) DemoHello(c echo.Context) error {
return c.JSON(http.StatusOK, domain.HelloWhoResponse{
BaseResponse: domain.BaseResponse{
Success: true,
Message: "Hello world!",
},
})
}
func (h *Handler) HelloWho(c echo.Context) error {
name := c.Param("who")
return c.JSON(http.StatusOK, domain.HelloWhoResponse{
BaseResponse: domain.BaseResponse{
Success: true,
Message: fmt.Sprintf("Hello, %s", name),
},
})
}
func (h *Handler) HelloBody(c echo.Context) error {
request := domain.HelloBodyRequest{}
err := (&echo.DefaultBinder{}).BindBody(c, &request)
if err != nil {
return h.InternalServerErrorResponse(c, err.Error())
}
return c.JSON(http.StatusOK, domain.HelloWhoResponse{
BaseResponse: domain.BaseResponse{
Success: true,
Message: fmt.Sprintf("Hello, %s", request.Name),
},
})
}
func (h *Handler) ProtectedRoute(c echo.Context) error {
token, err := h.getJwtToken(c)
if err != nil {
h.ReturnUnauthorizedResponse(c, err.Error())
}
err = token.IsValid(domain.ScopeRecipeRead)
if err != nil {
h.ReturnUnauthorizedResponse(c, ErrJwtScopeMissing)
}
return c.JSON(http.StatusOK, token)
}