2024-03-20 17:54:23 -07:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-03-31 17:48:44 -07:00
|
|
|
"go-cook/api/domain"
|
2024-03-20 17:54:23 -07:00
|
|
|
"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{
|
2024-03-23 08:26:49 -07:00
|
|
|
Success: true,
|
|
|
|
Message: "Hello world!",
|
|
|
|
})
|
2024-03-20 17:54:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) HelloWho(c echo.Context) error {
|
2024-03-23 08:26:49 -07:00
|
|
|
name := c.Param("who")
|
2024-03-31 17:48:44 -07:00
|
|
|
return c.JSON(http.StatusOK, domain.HelloWhoResponse{
|
2024-03-23 08:26:49 -07:00
|
|
|
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{}
|
2024-03-23 08:26:49 -07:00
|
|
|
err := (&echo.DefaultBinder{}).BindBody(c, &request)
|
|
|
|
if err != nil {
|
2024-03-31 17:48:44 -07:00
|
|
|
return c.JSON(http.StatusBadRequest, domain.HelloWhoResponse{
|
2024-03-23 08:26:49 -07:00
|
|
|
Success: false,
|
2024-03-27 21:55:25 -07:00
|
|
|
Error: err.Error(),
|
2024-03-23 08:26:49 -07:00
|
|
|
})
|
|
|
|
}
|
2024-03-27 21:55:25 -07:00
|
|
|
|
2024-03-31 17:48:44 -07:00
|
|
|
return c.JSON(http.StatusOK, domain.HelloWhoResponse{
|
2024-03-23 08:26:49 -07:00
|
|
|
Success: true,
|
|
|
|
Message: fmt.Sprintf("Hello, %s", request.Name),
|
|
|
|
})
|
2024-03-20 17:54:23 -07:00
|
|
|
}
|
2024-03-26 17:54:22 -07:00
|
|
|
|
2024-03-27 21:55:25 -07:00
|
|
|
func (h *Handler) ProtectedRoute(c echo.Context) error {
|
2024-03-29 14:49:57 -07:00
|
|
|
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)
|
2024-03-29 14:49:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(http.StatusOK, token)
|
2024-03-27 21:55:25 -07:00
|
|
|
}
|