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/jackc/pgx/v4/pgxpool"
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -82,7 +83,7 @@ func allRetrievalHandler(c echo.Context, serv *PgService) error {
|
||||||
return c.JSON(http.StatusOK, models)
|
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")
|
linkId := c.Param("id")
|
||||||
|
|
||||||
var model UpdateModel
|
var model UpdateModel
|
||||||
|
|
@ -90,13 +91,35 @@ func allRetrievalHandler(c echo.Context, serv *PgService) error {
|
||||||
return echo.NewHTTPError(http.StatusBadRequest, "Invalid data format.")
|
return echo.NewHTTPError(http.StatusBadRequest, "Invalid data format.")
|
||||||
}
|
}
|
||||||
|
|
||||||
l, err := serv.GetById(linkId)
|
updatingLink, err := serv.GetById(linkId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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)
|
return c.NoContent(http.StatusOK)
|
||||||
}*/
|
}
|
||||||
|
|
||||||
func removalHandler(c echo.Context, serv *PgService) error {
|
func removalHandler(c echo.Context, serv *PgService) error {
|
||||||
linkId := c.Param("id")
|
linkId := c.Param("id")
|
||||||
|
|
@ -126,9 +149,9 @@ func AddHandlers(s *echo.Echo, pool *pgxpool.Pool) {
|
||||||
return retrievalByIdHandler(ctx, &serv)
|
return retrievalByIdHandler(ctx, &serv)
|
||||||
})
|
})
|
||||||
|
|
||||||
/*exactLinkGroup.PATCH("", func(ctx echo.Context) error {
|
exactLinkGroup.PATCH("", func(ctx echo.Context) error {
|
||||||
return updateHandler(ctx, &serv)
|
return updateHandler(ctx, &serv)
|
||||||
})*/
|
})
|
||||||
|
|
||||||
exactLinkGroup.DELETE("", func(ctx echo.Context) error {
|
exactLinkGroup.DELETE("", func(ctx echo.Context) error {
|
||||||
return removalHandler(ctx, &serv)
|
return removalHandler(ctx, &serv)
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,7 @@ type Repository interface {
|
||||||
Save(link *Link) error
|
Save(link *Link) error
|
||||||
FindById(id string) (*Link, error)
|
FindById(id string) (*Link, error)
|
||||||
FindAll(limit int, offset int) (Links, error)
|
FindAll(limit int, offset int) (Links, error)
|
||||||
|
Update(link *Link) error
|
||||||
DeleteById(id string) error
|
DeleteById(id string) error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -135,6 +136,38 @@ func (r *PgRepository) FindAll(limit int, offset int) (Links, error) {
|
||||||
return links, nil
|
return links, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *PgRepository) Update(link *Link) error {
|
||||||
|
if link.Id == "" {
|
||||||
|
return errors.New("link ID must not be empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), defaultContextTimeout)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
tx, err := r.pool.Begin(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
sql := `
|
||||||
|
UPDATE links
|
||||||
|
SET name = $1, redirect_url = $2
|
||||||
|
WHERE id = $3
|
||||||
|
`
|
||||||
|
|
||||||
|
_, err = tx.Exec(ctx, sql, link.Name, link.RedirectURL.String(), link.Id)
|
||||||
|
if err != nil {
|
||||||
|
_ = tx.Rollback(ctx)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = tx.Commit(ctx); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (r *PgRepository) DeleteById(id string) error {
|
func (r *PgRepository) DeleteById(id string) error {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), defaultContextTimeout)
|
ctx, cancel := context.WithTimeout(context.Background(), defaultContextTimeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ type Service interface {
|
||||||
Create(link *Link) error
|
Create(link *Link) error
|
||||||
GetById(id string) (*Link, error)
|
GetById(id string) (*Link, error)
|
||||||
GetAll(limit int, offset int) (Links, error)
|
GetAll(limit int, offset int) (Links, error)
|
||||||
|
Update(data *Link) error
|
||||||
DeleteById(id string) error
|
DeleteById(id string) error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -72,6 +73,14 @@ func (s *PgService) GetAll(limit int, offset int) (Links, error) {
|
||||||
return links, nil
|
return links, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *PgService) Update(data *Link) error {
|
||||||
|
if err := s.rep.Update(data); err != nil {
|
||||||
|
return apperrors.UnknownError{Err: err}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (s *PgService) DeleteById(id string) error {
|
func (s *PgService) DeleteById(id string) error {
|
||||||
s.cache.Delete(id)
|
s.cache.Delete(id)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue