Add endpoints for retrieving link/links data
This commit is contained in:
parent
4ed0bba1a0
commit
5c0125a4b1
1 changed files with 62 additions and 0 deletions
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"github.com/jackc/pgx/v4/pgxpool"
|
"github.com/jackc/pgx/v4/pgxpool"
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
func redirectHandler(c echo.Context, pool *pgxpool.Pool) error {
|
func redirectHandler(c echo.Context, pool *pgxpool.Pool) error {
|
||||||
|
|
@ -53,6 +54,59 @@ func creationHandler(c echo.Context, pool *pgxpool.Pool) error {
|
||||||
return c.NoContent(http.StatusCreated)
|
return c.NoContent(http.StatusCreated)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func retrievalByIdHandler(c echo.Context, pool *pgxpool.Pool) error {
|
||||||
|
linkId := c.Param("id")
|
||||||
|
|
||||||
|
rep := PgRepository{pool: pool}
|
||||||
|
|
||||||
|
link, err := rep.FindById(linkId)
|
||||||
|
if err != nil {
|
||||||
|
return echo.NewHTTPError(http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
|
||||||
|
if link == nil {
|
||||||
|
return echo.NewHTTPError(http.StatusNotFound, "Link with given ID was not found.")
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.JSON(http.StatusOK, MapEntityToModel(link))
|
||||||
|
}
|
||||||
|
|
||||||
|
func allRetrievalHandler(c echo.Context, pool *pgxpool.Pool) error {
|
||||||
|
limit := 20
|
||||||
|
if v := c.QueryParam("limit"); v != "" {
|
||||||
|
num, err := strconv.Atoi(v)
|
||||||
|
if err != nil {
|
||||||
|
return echo.NewHTTPError(http.StatusBadRequest, "Invalid limit value.")
|
||||||
|
}
|
||||||
|
|
||||||
|
limit = num
|
||||||
|
}
|
||||||
|
|
||||||
|
offset := 0
|
||||||
|
if v := c.QueryParam("offset"); v != "" {
|
||||||
|
num, err := strconv.Atoi(v)
|
||||||
|
if err != nil {
|
||||||
|
return echo.NewHTTPError(http.StatusBadRequest, "Invalid offset value.")
|
||||||
|
}
|
||||||
|
|
||||||
|
offset = num
|
||||||
|
}
|
||||||
|
|
||||||
|
rep := PgRepository{pool: pool}
|
||||||
|
|
||||||
|
links, err := rep.FindAll(limit, offset)
|
||||||
|
if err != nil {
|
||||||
|
return echo.NewHTTPError(http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
|
||||||
|
models := make([]ResourceModel, len(links))
|
||||||
|
for i, link := range links {
|
||||||
|
models[i] = MapEntityToModel(&link)
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.JSON(http.StatusOK, models)
|
||||||
|
}
|
||||||
|
|
||||||
func removalHandler(c echo.Context, pool *pgxpool.Pool) error {
|
func removalHandler(c echo.Context, pool *pgxpool.Pool) error {
|
||||||
linkId := c.Param("id")
|
linkId := c.Param("id")
|
||||||
|
|
||||||
|
|
@ -72,6 +126,14 @@ func AddHandlers(s *echo.Echo, pool *pgxpool.Pool) {
|
||||||
return creationHandler(ctx, pool)
|
return creationHandler(ctx, pool)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
gr.GET("", func(ctx echo.Context) error {
|
||||||
|
return allRetrievalHandler(ctx, pool)
|
||||||
|
})
|
||||||
|
|
||||||
|
gr.GET("/:id", func(ctx echo.Context) error {
|
||||||
|
return retrievalByIdHandler(ctx, pool)
|
||||||
|
})
|
||||||
|
|
||||||
gr.DELETE("/:id", func(ctx echo.Context) error {
|
gr.DELETE("/:id", func(ctx echo.Context) error {
|
||||||
return removalHandler(ctx, pool)
|
return removalHandler(ctx, pool)
|
||||||
})
|
})
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue