Add caching
This commit is contained in:
parent
f166ffab3c
commit
fe9b4b5f5f
4 changed files with 36 additions and 21 deletions
|
|
@ -8,11 +8,9 @@ import (
|
|||
"strconv"
|
||||
)
|
||||
|
||||
func redirectHandler(c echo.Context, pool *pgxpool.Pool) error {
|
||||
func redirectHandler(c echo.Context, serv *PgService) error {
|
||||
linkId := c.Param("id")
|
||||
|
||||
serv := PgService{rep: PgRepository{pool: pool}}
|
||||
|
||||
link, err := serv.GetById(linkId)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
@ -21,7 +19,7 @@ func redirectHandler(c echo.Context, pool *pgxpool.Pool) error {
|
|||
return c.Redirect(http.StatusSeeOther, link.RedirectURL.String())
|
||||
}
|
||||
|
||||
func creationHandler(c echo.Context, pool *pgxpool.Pool) error {
|
||||
func creationHandler(c echo.Context, serv *PgService) error {
|
||||
var model CreationModel
|
||||
if err := json.NewDecoder(c.Request().Body).Decode(&model); err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Invalid data format.")
|
||||
|
|
@ -32,8 +30,6 @@ func creationHandler(c echo.Context, pool *pgxpool.Pool) error {
|
|||
return echo.NewHTTPError(http.StatusBadRequest, "Invalid URL.")
|
||||
}
|
||||
|
||||
serv := PgService{rep: PgRepository{pool: pool}}
|
||||
|
||||
if err = serv.Create(entity); err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -41,11 +37,9 @@ func creationHandler(c echo.Context, pool *pgxpool.Pool) error {
|
|||
return c.NoContent(http.StatusCreated)
|
||||
}
|
||||
|
||||
func retrievalByIdHandler(c echo.Context, pool *pgxpool.Pool) error {
|
||||
func retrievalByIdHandler(c echo.Context, serv *PgService) error {
|
||||
linkId := c.Param("id")
|
||||
|
||||
serv := PgService{rep: PgRepository{pool: pool}}
|
||||
|
||||
l, err := serv.GetById(linkId)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
@ -54,7 +48,7 @@ func retrievalByIdHandler(c echo.Context, pool *pgxpool.Pool) error {
|
|||
return c.JSON(http.StatusOK, MapEntityToModel(l))
|
||||
}
|
||||
|
||||
func allRetrievalHandler(c echo.Context, pool *pgxpool.Pool) error {
|
||||
func allRetrievalHandler(c echo.Context, serv *PgService) error {
|
||||
limit := 20
|
||||
if v := c.QueryParam("limit"); v != "" {
|
||||
num, err := strconv.Atoi(v)
|
||||
|
|
@ -75,8 +69,6 @@ func allRetrievalHandler(c echo.Context, pool *pgxpool.Pool) error {
|
|||
offset = num
|
||||
}
|
||||
|
||||
serv := PgService{rep: PgRepository{pool: pool}}
|
||||
|
||||
links, err := serv.GetAll(limit, offset)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
@ -90,11 +82,9 @@ func allRetrievalHandler(c echo.Context, pool *pgxpool.Pool) error {
|
|||
return c.JSON(http.StatusOK, models)
|
||||
}
|
||||
|
||||
func removalHandler(c echo.Context, pool *pgxpool.Pool) error {
|
||||
func removalHandler(c echo.Context, serv *PgService) error {
|
||||
linkId := c.Param("id")
|
||||
|
||||
serv := PgService{rep: PgRepository{pool: pool}}
|
||||
|
||||
if err := serv.DeleteById(linkId); err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -103,25 +93,27 @@ func removalHandler(c echo.Context, pool *pgxpool.Pool) error {
|
|||
}
|
||||
|
||||
func AddHandlers(s *echo.Echo, pool *pgxpool.Pool) {
|
||||
serv := NewPgService(PgRepository{pool: pool})
|
||||
|
||||
gr := s.Group("/links")
|
||||
|
||||
gr.POST("", func(ctx echo.Context) error {
|
||||
return creationHandler(ctx, pool)
|
||||
return creationHandler(ctx, &serv)
|
||||
})
|
||||
|
||||
gr.GET("", func(ctx echo.Context) error {
|
||||
return allRetrievalHandler(ctx, pool)
|
||||
return allRetrievalHandler(ctx, &serv)
|
||||
})
|
||||
|
||||
gr.GET("/:id", func(ctx echo.Context) error {
|
||||
return retrievalByIdHandler(ctx, pool)
|
||||
return retrievalByIdHandler(ctx, &serv)
|
||||
})
|
||||
|
||||
gr.DELETE("/:id", func(ctx echo.Context) error {
|
||||
return removalHandler(ctx, pool)
|
||||
return removalHandler(ctx, &serv)
|
||||
})
|
||||
|
||||
s.GET("/:id", func(ctx echo.Context) error {
|
||||
return redirectHandler(ctx, pool)
|
||||
return redirectHandler(ctx, &serv)
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue