163 lines
3.5 KiB
Go
163 lines
3.5 KiB
Go
package link
|
|
|
|
import (
|
|
"encoding/json"
|
|
"github.com/jackc/pgx/v4/pgxpool"
|
|
"github.com/labstack/echo/v4"
|
|
"net/http"
|
|
"net/url"
|
|
"strconv"
|
|
)
|
|
|
|
func redirectHandler(c echo.Context, serv *PgService) error {
|
|
linkId := c.Param("id")
|
|
|
|
link, err := serv.GetById(linkId)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.Redirect(http.StatusSeeOther, link.RedirectURL.String())
|
|
}
|
|
|
|
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.")
|
|
}
|
|
|
|
entity, err := model.MapModelToEntity()
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Invalid URL.")
|
|
}
|
|
|
|
if err = serv.Create(entity); err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.NoContent(http.StatusCreated)
|
|
}
|
|
|
|
func retrievalByIdHandler(c echo.Context, serv *PgService) error {
|
|
linkId := c.Param("id")
|
|
|
|
l, err := serv.GetById(linkId)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.JSON(http.StatusOK, MapEntityToModel(l))
|
|
}
|
|
|
|
func allRetrievalHandler(c echo.Context, serv *PgService) 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
|
|
}
|
|
|
|
links, err := serv.GetAll(limit, offset)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
models := make([]ResourceModel, len(links))
|
|
for i, v := range links {
|
|
models[i] = MapEntityToModel(v)
|
|
}
|
|
|
|
return c.JSON(http.StatusOK, models)
|
|
}
|
|
|
|
func updateHandler(c echo.Context, serv *PgService) error {
|
|
linkId := c.Param("id")
|
|
|
|
var model UpdateModel
|
|
if err := json.NewDecoder(c.Request().Body).Decode(&model); err != nil {
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Invalid data format.")
|
|
}
|
|
|
|
updatingLink, err := serv.GetById(linkId)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
hasChanges := false
|
|
switch {
|
|
case model.Name != "" && model.Name != updatingLink.Name:
|
|
updatingLink.Name = model.Name
|
|
|
|
hasChanges = true
|
|
case model.RedirectURL != "" && model.RedirectURL != updatingLink.RedirectURL.String():
|
|
if parsedUrl, err := url.Parse(model.RedirectURL); err != nil {
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Invalid URL value.")
|
|
} else {
|
|
updatingLink.RedirectURL = *parsedUrl
|
|
}
|
|
|
|
hasChanges = true
|
|
}
|
|
|
|
if hasChanges {
|
|
if err = serv.Update(updatingLink); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return c.NoContent(http.StatusOK)
|
|
}
|
|
|
|
func removalHandler(c echo.Context, serv *PgService) error {
|
|
linkId := c.Param("id")
|
|
|
|
if err := serv.DeleteById(linkId); err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.NoContent(http.StatusNoContent)
|
|
}
|
|
|
|
func AddHandlers(s *echo.Echo, pool *pgxpool.Pool) {
|
|
serv := NewPgService(PgRepository{pool: pool})
|
|
|
|
linksGroup := s.Group("/links")
|
|
exactLinkGroup := linksGroup.Group("/:id")
|
|
|
|
linksGroup.POST("", func(ctx echo.Context) error {
|
|
return creationHandler(ctx, &serv)
|
|
})
|
|
|
|
linksGroup.GET("", func(ctx echo.Context) error {
|
|
return allRetrievalHandler(ctx, &serv)
|
|
})
|
|
|
|
exactLinkGroup.GET("", func(ctx echo.Context) error {
|
|
return retrievalByIdHandler(ctx, &serv)
|
|
})
|
|
|
|
exactLinkGroup.PATCH("", func(ctx echo.Context) error {
|
|
return updateHandler(ctx, &serv)
|
|
})
|
|
|
|
exactLinkGroup.DELETE("", func(ctx echo.Context) error {
|
|
return removalHandler(ctx, &serv)
|
|
})
|
|
|
|
s.GET("/:id", func(ctx echo.Context) error {
|
|
return redirectHandler(ctx, &serv)
|
|
})
|
|
}
|