This commit is contained in:
Andrey Chervyakov 2021-03-17 18:54:45 +06:00
parent e426559cec
commit ddaf0dcbfc
6 changed files with 42 additions and 40 deletions

View file

@ -9,20 +9,20 @@ import (
"strconv"
)
func redirectHandler(c echo.Context, serv *PgService) error {
linkId := c.Param("id")
func redirectHandler(ctx echo.Context, serv Service) error {
linkId := ctx.Param("id")
link, err := serv.GetById(linkId)
if err != nil {
return err
}
return c.Redirect(http.StatusSeeOther, link.RedirectURL.String())
return ctx.Redirect(http.StatusSeeOther, link.RedirectURL.String())
}
func creationHandler(c echo.Context, serv *PgService) error {
func creationHandler(ctx echo.Context, serv Service) error {
var model CreationModel
if err := json.NewDecoder(c.Request().Body).Decode(&model); err != nil {
if err := json.NewDecoder(ctx.Request().Body).Decode(&model); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid data format.")
}
@ -35,23 +35,23 @@ func creationHandler(c echo.Context, serv *PgService) error {
return err
}
return c.NoContent(http.StatusCreated)
return ctx.NoContent(http.StatusCreated)
}
func retrievalByIdHandler(c echo.Context, serv *PgService) error {
linkId := c.Param("id")
func retrievalByIdHandler(ctx echo.Context, serv Service) error {
linkId := ctx.Param("id")
l, err := serv.GetById(linkId)
if err != nil {
return err
}
return c.JSON(http.StatusOK, MapEntityToModel(l))
return ctx.JSON(http.StatusOK, MapEntityToModel(l))
}
func allRetrievalHandler(c echo.Context, serv *PgService) error {
func allRetrievalHandler(ctx echo.Context, serv Service) error {
limit := 20
if v := c.QueryParam("limit"); v != "" {
if v := ctx.QueryParam("limit"); v != "" {
num, err := strconv.Atoi(v)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid limit value.")
@ -61,7 +61,7 @@ func allRetrievalHandler(c echo.Context, serv *PgService) error {
}
offset := 0
if v := c.QueryParam("offset"); v != "" {
if v := ctx.QueryParam("offset"); v != "" {
num, err := strconv.Atoi(v)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid offset value.")
@ -80,14 +80,14 @@ func allRetrievalHandler(c echo.Context, serv *PgService) error {
models[i] = MapEntityToModel(v)
}
return c.JSON(http.StatusOK, models)
return ctx.JSON(http.StatusOK, models)
}
func updateHandler(c echo.Context, serv *PgService) error {
linkId := c.Param("id")
func updateHandler(ctx echo.Context, serv Service) error {
linkId := ctx.Param("id")
var model UpdateModel
if err := json.NewDecoder(c.Request().Body).Decode(&model); err != nil {
if err := json.NewDecoder(ctx.Request().Body).Decode(&model); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid data format.")
}
@ -118,46 +118,46 @@ func updateHandler(c echo.Context, serv *PgService) error {
}
}
return c.NoContent(http.StatusOK)
return ctx.NoContent(http.StatusOK)
}
func removalHandler(c echo.Context, serv *PgService) error {
linkId := c.Param("id")
func removalHandler(ctx echo.Context, serv Service) error {
linkId := ctx.Param("id")
if err := serv.DeleteById(linkId); err != nil {
return err
}
return c.NoContent(http.StatusNoContent)
return ctx.NoContent(http.StatusNoContent)
}
func AddHandlers(s *echo.Echo, pool *pgxpool.Pool) {
serv := NewPgService(PgRepository{pool: pool})
func AddHandlers(server *echo.Echo, pool *pgxpool.Pool) {
serv := NewService(NewRepository(pool))
linksGroup := s.Group("/links")
linksGroup := server.Group("/links")
exactLinkGroup := linksGroup.Group("/:id")
linksGroup.POST("", func(ctx echo.Context) error {
return creationHandler(ctx, &serv)
return creationHandler(ctx, serv)
})
linksGroup.GET("", func(ctx echo.Context) error {
return allRetrievalHandler(ctx, &serv)
return allRetrievalHandler(ctx, serv)
})
exactLinkGroup.GET("", func(ctx echo.Context) error {
return retrievalByIdHandler(ctx, &serv)
return retrievalByIdHandler(ctx, serv)
})
exactLinkGroup.PATCH("", func(ctx echo.Context) error {
return updateHandler(ctx, &serv)
return updateHandler(ctx, serv)
})
exactLinkGroup.DELETE("", func(ctx echo.Context) error {
return removalHandler(ctx, &serv)
return removalHandler(ctx, serv)
})
s.GET("/:id", func(ctx echo.Context) error {
return redirectHandler(ctx, &serv)
server.GET("/:id", func(ctx echo.Context) error {
return redirectHandler(ctx, serv)
})
}

View file

@ -24,6 +24,10 @@ type PgRepository struct {
pool *pgxpool.Pool
}
func NewRepository(pool *pgxpool.Pool) Repository {
return &PgRepository{pool: pool}
}
func (r *PgRepository) Save(link *Link) error {
ctx, cancel := context.WithTimeout(context.Background(), defaultContextTimeout)
defer cancel()

View file

@ -14,12 +14,12 @@ type Service interface {
}
type PgService struct {
rep PgRepository
rep Repository
cache *cache.Cache
}
func NewPgService(rep PgRepository) PgService {
return PgService{
func NewService(rep Repository) Service {
return &PgService{
rep: rep,
cache: cache.New(cache.NoExpiration, 0),
}