Update link handlers to use service instead of repository
This commit is contained in:
parent
221eb3f346
commit
a4cfa52e3e
1 changed files with 19 additions and 35 deletions
|
|
@ -1,6 +1,7 @@
|
|||
package link
|
||||
|
||||
import (
|
||||
. "cgnolink/err"
|
||||
"encoding/json"
|
||||
"github.com/jackc/pgx/v4/pgxpool"
|
||||
"github.com/labstack/echo/v4"
|
||||
|
|
@ -11,15 +12,11 @@ import (
|
|||
func redirectHandler(c echo.Context, pool *pgxpool.Pool) error {
|
||||
linkId := c.Param("id")
|
||||
|
||||
rep := PgRepository{pool: pool}
|
||||
serv := PgService{rep: PgRepository{pool: pool}}
|
||||
|
||||
link, err := rep.FindById(linkId)
|
||||
link, err := serv.GetById(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 MapErrToHTTPErr(err)
|
||||
}
|
||||
|
||||
return c.Redirect(http.StatusSeeOther, link.RedirectURL.String())
|
||||
|
|
@ -36,19 +33,10 @@ func creationHandler(c echo.Context, pool *pgxpool.Pool) error {
|
|||
return echo.NewHTTPError(http.StatusBadRequest, "Invalid URL.")
|
||||
}
|
||||
|
||||
rep := PgRepository{pool: pool}
|
||||
serv := PgService{rep: PgRepository{pool: pool}}
|
||||
|
||||
link, err := rep.FindById(entity.Id)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
if link != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Link with given ID already exists.")
|
||||
}
|
||||
|
||||
if err = rep.Save(entity); err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError)
|
||||
if err = serv.Create(entity); err != nil {
|
||||
return MapErrToHTTPErr(err)
|
||||
}
|
||||
|
||||
return c.NoContent(http.StatusCreated)
|
||||
|
|
@ -57,18 +45,14 @@ func creationHandler(c echo.Context, pool *pgxpool.Pool) error {
|
|||
func retrievalByIdHandler(c echo.Context, pool *pgxpool.Pool) error {
|
||||
linkId := c.Param("id")
|
||||
|
||||
rep := PgRepository{pool: pool}
|
||||
serv := PgService{rep: PgRepository{pool: pool}}
|
||||
|
||||
link, err := rep.FindById(linkId)
|
||||
l, err := serv.GetById(linkId)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError)
|
||||
return MapErrToHTTPErr(err)
|
||||
}
|
||||
|
||||
if link == nil {
|
||||
return echo.NewHTTPError(http.StatusNotFound, "Link with given ID was not found.")
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, MapEntityToModel(link))
|
||||
return c.JSON(http.StatusOK, MapEntityToModel(l))
|
||||
}
|
||||
|
||||
func allRetrievalHandler(c echo.Context, pool *pgxpool.Pool) error {
|
||||
|
|
@ -92,16 +76,16 @@ func allRetrievalHandler(c echo.Context, pool *pgxpool.Pool) error {
|
|||
offset = num
|
||||
}
|
||||
|
||||
rep := PgRepository{pool: pool}
|
||||
serv := PgService{rep: PgRepository{pool: pool}}
|
||||
|
||||
links, err := rep.FindAll(limit, offset)
|
||||
links, err := serv.GetAll(limit, offset)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError)
|
||||
return MapErrToHTTPErr(err)
|
||||
}
|
||||
|
||||
models := make([]ResourceModel, len(links))
|
||||
for i, link := range links {
|
||||
models[i] = MapEntityToModel(&link)
|
||||
for i, v := range links {
|
||||
models[i] = MapEntityToModel(v)
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, models)
|
||||
|
|
@ -110,10 +94,10 @@ func allRetrievalHandler(c echo.Context, pool *pgxpool.Pool) error {
|
|||
func removalHandler(c echo.Context, pool *pgxpool.Pool) error {
|
||||
linkId := c.Param("id")
|
||||
|
||||
rep := PgRepository{pool: pool}
|
||||
serv := PgService{rep: PgRepository{pool: pool}}
|
||||
|
||||
if err := rep.DeleteById(linkId); err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError)
|
||||
if err := serv.DeleteById(linkId); err != nil {
|
||||
return MapErrToHTTPErr(err)
|
||||
}
|
||||
|
||||
return c.NoContent(http.StatusNoContent)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue