Refactor
This commit is contained in:
parent
e426559cec
commit
ddaf0dcbfc
6 changed files with 42 additions and 40 deletions
|
|
@ -9,20 +9,20 @@ import (
|
|||
"strconv"
|
||||
)
|
||||
|
||||
func redirectHandler(c echo.Context, serv *PgService) error {
|
||||
linkId := c.Param("id")
|
||||
func redirectHandler(ctx echo.Context, serv Service) error {
|
||||
linkId := ctx.Param("id")
|
||||
|
||||
link, err := serv.GetById(linkId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Redirect(http.StatusSeeOther, link.RedirectURL.String())
|
||||
return ctx.Redirect(http.StatusSeeOther, link.RedirectURL.String())
|
||||
}
|
||||
|
||||
func creationHandler(c echo.Context, serv *PgService) error {
|
||||
func creationHandler(ctx echo.Context, serv Service) error {
|
||||
var model CreationModel
|
||||
if err := json.NewDecoder(c.Request().Body).Decode(&model); err != nil {
|
||||
if err := json.NewDecoder(ctx.Request().Body).Decode(&model); err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Invalid data format.")
|
||||
}
|
||||
|
||||
|
|
@ -35,23 +35,23 @@ func creationHandler(c echo.Context, serv *PgService) error {
|
|||
return err
|
||||
}
|
||||
|
||||
return c.NoContent(http.StatusCreated)
|
||||
return ctx.NoContent(http.StatusCreated)
|
||||
}
|
||||
|
||||
func retrievalByIdHandler(c echo.Context, serv *PgService) error {
|
||||
linkId := c.Param("id")
|
||||
func retrievalByIdHandler(ctx echo.Context, serv Service) error {
|
||||
linkId := ctx.Param("id")
|
||||
|
||||
l, err := serv.GetById(linkId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, MapEntityToModel(l))
|
||||
return ctx.JSON(http.StatusOK, MapEntityToModel(l))
|
||||
}
|
||||
|
||||
func allRetrievalHandler(c echo.Context, serv *PgService) error {
|
||||
func allRetrievalHandler(ctx echo.Context, serv Service) error {
|
||||
limit := 20
|
||||
if v := c.QueryParam("limit"); v != "" {
|
||||
if v := ctx.QueryParam("limit"); v != "" {
|
||||
num, err := strconv.Atoi(v)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Invalid limit value.")
|
||||
|
|
@ -61,7 +61,7 @@ func allRetrievalHandler(c echo.Context, serv *PgService) error {
|
|||
}
|
||||
|
||||
offset := 0
|
||||
if v := c.QueryParam("offset"); v != "" {
|
||||
if v := ctx.QueryParam("offset"); v != "" {
|
||||
num, err := strconv.Atoi(v)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Invalid offset value.")
|
||||
|
|
@ -80,14 +80,14 @@ func allRetrievalHandler(c echo.Context, serv *PgService) error {
|
|||
models[i] = MapEntityToModel(v)
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, models)
|
||||
return ctx.JSON(http.StatusOK, models)
|
||||
}
|
||||
|
||||
func updateHandler(c echo.Context, serv *PgService) error {
|
||||
linkId := c.Param("id")
|
||||
func updateHandler(ctx echo.Context, serv Service) error {
|
||||
linkId := ctx.Param("id")
|
||||
|
||||
var model UpdateModel
|
||||
if err := json.NewDecoder(c.Request().Body).Decode(&model); err != nil {
|
||||
if err := json.NewDecoder(ctx.Request().Body).Decode(&model); err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Invalid data format.")
|
||||
}
|
||||
|
||||
|
|
@ -118,46 +118,46 @@ func updateHandler(c echo.Context, serv *PgService) error {
|
|||
}
|
||||
}
|
||||
|
||||
return c.NoContent(http.StatusOK)
|
||||
return ctx.NoContent(http.StatusOK)
|
||||
}
|
||||
|
||||
func removalHandler(c echo.Context, serv *PgService) error {
|
||||
linkId := c.Param("id")
|
||||
func removalHandler(ctx echo.Context, serv Service) error {
|
||||
linkId := ctx.Param("id")
|
||||
|
||||
if err := serv.DeleteById(linkId); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.NoContent(http.StatusNoContent)
|
||||
return ctx.NoContent(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func AddHandlers(s *echo.Echo, pool *pgxpool.Pool) {
|
||||
serv := NewPgService(PgRepository{pool: pool})
|
||||
func AddHandlers(server *echo.Echo, pool *pgxpool.Pool) {
|
||||
serv := NewService(NewRepository(pool))
|
||||
|
||||
linksGroup := s.Group("/links")
|
||||
linksGroup := server.Group("/links")
|
||||
exactLinkGroup := linksGroup.Group("/:id")
|
||||
|
||||
linksGroup.POST("", func(ctx echo.Context) error {
|
||||
return creationHandler(ctx, &serv)
|
||||
return creationHandler(ctx, serv)
|
||||
})
|
||||
|
||||
linksGroup.GET("", func(ctx echo.Context) error {
|
||||
return allRetrievalHandler(ctx, &serv)
|
||||
return allRetrievalHandler(ctx, serv)
|
||||
})
|
||||
|
||||
exactLinkGroup.GET("", func(ctx echo.Context) error {
|
||||
return retrievalByIdHandler(ctx, &serv)
|
||||
return retrievalByIdHandler(ctx, serv)
|
||||
})
|
||||
|
||||
exactLinkGroup.PATCH("", func(ctx echo.Context) error {
|
||||
return updateHandler(ctx, &serv)
|
||||
return updateHandler(ctx, serv)
|
||||
})
|
||||
|
||||
exactLinkGroup.DELETE("", func(ctx echo.Context) error {
|
||||
return removalHandler(ctx, &serv)
|
||||
return removalHandler(ctx, serv)
|
||||
})
|
||||
|
||||
s.GET("/:id", func(ctx echo.Context) error {
|
||||
return redirectHandler(ctx, &serv)
|
||||
server.GET("/:id", func(ctx echo.Context) error {
|
||||
return redirectHandler(ctx, serv)
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue