Add endpoint for deleting link

This commit is contained in:
Andrey Chervyakov 2021-03-13 02:48:03 +06:00
parent 2cfd175414
commit 99be0824cd

View file

@ -53,11 +53,28 @@ func redirectHandler(c echo.Context, pool *pgxpool.Pool) error {
return c.Redirect(http.StatusSeeOther, link.RedirectURL.String())
}
func removalHandler(c echo.Context, pool *pgxpool.Pool) error {
linkId := c.Param("id")
rep := PgRepository{pool: pool}
if err := rep.DeleteById(linkId); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError)
}
return c.NoContent(http.StatusNoContent)
}
func AddHandlers(s *echo.Echo, pool *pgxpool.Pool) {
s.POST("", func(ctx echo.Context) error {
return creationHandler(ctx, pool)
})
s.GET("/:id", func(ctx echo.Context) error {
return redirectHandler(ctx, pool)
})
s.DELETE("/:id", func(ctx echo.Context) error {
return removalHandler(ctx, pool)
})
}