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

59 lines
1.3 KiB
Go
Raw Permalink Normal View History

package v1
import (
"fmt"
"net/http"
"git.jamestombleson.com/jtom38/go-cook/internal/domain"
2024-04-05 17:50:29 -07:00
"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{
BaseResponse: domain.BaseResponse{
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{
BaseResponse: domain.BaseResponse{
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 {
return h.InternalServerErrorResponse(c, err.Error())
}
2024-03-31 17:48:44 -07:00
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 {
2024-03-31 17:48:44 -07:00
h.ReturnUnauthorizedResponse(c, err.Error())
}
2024-04-05 17:50:29 -07:00
err = token.IsValid(domain.ScopeRecipeRead)
2024-03-31 17:48:44 -07:00
if err != nil {
h.ReturnUnauthorizedResponse(c, ErrJwtScopeMissing)
}
return c.JSON(http.StatusOK, token)
}