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
|
package link
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
. "cgnolink/err"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/jackc/pgx/v4/pgxpool"
|
"github.com/jackc/pgx/v4/pgxpool"
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
|
|
@ -11,15 +12,11 @@ import (
|
||||||
func redirectHandler(c echo.Context, pool *pgxpool.Pool) error {
|
func redirectHandler(c echo.Context, pool *pgxpool.Pool) error {
|
||||||
linkId := c.Param("id")
|
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 {
|
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.Redirect(http.StatusSeeOther, link.RedirectURL.String())
|
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.")
|
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 = serv.Create(entity); err != nil {
|
||||||
if err != nil {
|
return MapErrToHTTPErr(err)
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.NoContent(http.StatusCreated)
|
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 {
|
func retrievalByIdHandler(c echo.Context, pool *pgxpool.Pool) error {
|
||||||
linkId := c.Param("id")
|
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 {
|
if err != nil {
|
||||||
return echo.NewHTTPError(http.StatusInternalServerError)
|
return MapErrToHTTPErr(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if link == nil {
|
return c.JSON(http.StatusOK, MapEntityToModel(l))
|
||||||
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 {
|
func allRetrievalHandler(c echo.Context, pool *pgxpool.Pool) error {
|
||||||
|
|
@ -92,16 +76,16 @@ func allRetrievalHandler(c echo.Context, pool *pgxpool.Pool) error {
|
||||||
offset = num
|
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 {
|
if err != nil {
|
||||||
return echo.NewHTTPError(http.StatusInternalServerError)
|
return MapErrToHTTPErr(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
models := make([]ResourceModel, len(links))
|
models := make([]ResourceModel, len(links))
|
||||||
for i, link := range links {
|
for i, v := range links {
|
||||||
models[i] = MapEntityToModel(&link)
|
models[i] = MapEntityToModel(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.JSON(http.StatusOK, models)
|
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 {
|
func removalHandler(c echo.Context, pool *pgxpool.Pool) error {
|
||||||
linkId := c.Param("id")
|
linkId := c.Param("id")
|
||||||
|
|
||||||
rep := PgRepository{pool: pool}
|
serv := PgService{rep: PgRepository{pool: pool}}
|
||||||
|
|
||||||
if err := rep.DeleteById(linkId); err != nil {
|
if err := serv.DeleteById(linkId); err != nil {
|
||||||
return echo.NewHTTPError(http.StatusInternalServerError)
|
return MapErrToHTTPErr(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.NoContent(http.StatusNoContent)
|
return c.NoContent(http.StatusNoContent)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue