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

55 lines
1.1 KiB
Go
Raw Normal View History

package v1
import (
"fmt"
2024-03-31 17:48:44 -07:00
"go-cook/api/domain"
"net/http"
"github.com/labstack/echo/v4"
)
func (h *Handler) DemoHello(c echo.Context) error {
2024-03-31 17:48:44 -07:00
return c.JSON(http.StatusOK, domain.HelloWhoResponse{
Success: true,
Message: "Hello world!",
})
}
func (h *Handler) HelloWho(c echo.Context) error {
name := c.Param("who")
2024-03-31 17:48:44 -07:00
return c.JSON(http.StatusOK, domain.HelloWhoResponse{
Success: true,
Message: fmt.Sprintf("Hello, %s", name),
})
}
func (h *Handler) HelloBody(c echo.Context) error {
2024-03-31 17:48:44 -07:00
request := domain.HelloBodyRequest{}
err := (&echo.DefaultBinder{}).BindBody(c, &request)
if err != nil {
2024-03-31 17:48:44 -07:00
return c.JSON(http.StatusBadRequest, domain.HelloWhoResponse{
Success: false,
Error: err.Error(),
})
}
2024-03-31 17:48:44 -07:00
return c.JSON(http.StatusOK, domain.HelloWhoResponse{
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 {
2024-03-31 17:48:44 -07:00
h.ReturnUnauthorizedResponse(c, err.Error())
}
err = token.IsValid(domain.ScopeRecipeRead)
if err != nil {
h.ReturnUnauthorizedResponse(c, ErrJwtScopeMissing)
}
return c.JSON(http.StatusOK, token)
}