Add endpoint for updating link
This commit is contained in:
parent
9ae733e618
commit
8a23425826
3 changed files with 70 additions and 5 deletions
|
|
@ -5,6 +5,7 @@ import (
|
|||
"github.com/jackc/pgx/v4/pgxpool"
|
||||
"github.com/labstack/echo/v4"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
|
|
@ -82,7 +83,7 @@ func allRetrievalHandler(c echo.Context, serv *PgService) error {
|
|||
return c.JSON(http.StatusOK, models)
|
||||
}
|
||||
|
||||
/*func updateHandler(c echo.Context, serv *PgService) error {
|
||||
func updateHandler(c echo.Context, serv *PgService) error {
|
||||
linkId := c.Param("id")
|
||||
|
||||
var model UpdateModel
|
||||
|
|
@ -90,13 +91,35 @@ func allRetrievalHandler(c echo.Context, serv *PgService) error {
|
|||
return echo.NewHTTPError(http.StatusBadRequest, "Invalid data format.")
|
||||
}
|
||||
|
||||
l, err := serv.GetById(linkId)
|
||||
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")
|
||||
|
|
@ -126,9 +149,9 @@ func AddHandlers(s *echo.Echo, pool *pgxpool.Pool) {
|
|||
return retrievalByIdHandler(ctx, &serv)
|
||||
})
|
||||
|
||||
/*exactLinkGroup.PATCH("", func(ctx echo.Context) error {
|
||||
exactLinkGroup.PATCH("", func(ctx echo.Context) error {
|
||||
return updateHandler(ctx, &serv)
|
||||
})*/
|
||||
})
|
||||
|
||||
exactLinkGroup.DELETE("", func(ctx echo.Context) error {
|
||||
return removalHandler(ctx, &serv)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue