Added tests for entity and log middleware.

This commit is contained in:
mikestefanello 2021-12-21 21:02:25 -05:00
parent ac93e0f366
commit fc3fee1306
4 changed files with 57 additions and 7 deletions

View file

@ -11,12 +11,13 @@ import (
"github.com/labstack/echo/v4"
)
// LoadUser loads the user based on the ID provided as a path parameter
func LoadUser(orm *ent.Client) echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
userID, err := strconv.Atoi(c.Param("user"))
if err != nil {
return echo.NewHTTPError(http.StatusNotFound, "Not found")
return echo.NewHTTPError(http.StatusNotFound)
}
u, err := orm.User.
@ -26,16 +27,14 @@ func LoadUser(orm *ent.Client) echo.MiddlewareFunc {
switch err.(type) {
case nil:
c.Set(context.UserKey, u)
return next(c)
case *ent.NotFoundError:
return echo.NewHTTPError(http.StatusNotFound, "Not found")
return echo.NewHTTPError(http.StatusNotFound)
default:
c.Logger().Error(err)
return echo.NewHTTPError(http.StatusInternalServerError, "Internal server error")
return echo.NewHTTPError(http.StatusInternalServerError)
}
c.Set(context.UserKey, u)
return next(c)
}
}
}