From 99be0824cd19f18e78d77764513a188916d8be53 Mon Sep 17 00:00:00 2001 From: Andrey Chervyakov Date: Sat, 13 Mar 2021 02:48:03 +0600 Subject: [PATCH] Add endpoint for deleting link --- link/handlers.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/link/handlers.go b/link/handlers.go index a54e644..da24868 100644 --- a/link/handlers.go +++ b/link/handlers.go @@ -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) + }) }