From bb23987fff201681e300fd7f00181e6a75048621 Mon Sep 17 00:00:00 2001 From: Masahiko AMANO Date: Mon, 6 Jan 2025 16:23:03 +0300 Subject: [PATCH] init(api): add quote delete handler --- backend/api/handlers.go | 17 +++++++++++++++++ backend/api/routes.go | 1 + 2 files changed, 18 insertions(+) diff --git a/backend/api/handlers.go b/backend/api/handlers.go index 0d4be0d..1cd32c7 100644 --- a/backend/api/handlers.go +++ b/backend/api/handlers.go @@ -148,4 +148,21 @@ func quoteAdd(c *gin.Context) { c.JSON(http.StatusCreated, quote) } +func quoteDelete(c *gin.Context) { + session := sessions.Default(c) + user_id, ok := session.Get("user_id").(string) + if !ok { + c.JSON(http.StatusUnauthorized, gin.H{"error": "Ты это, залогинься сначала что ли, а то чё как крыса"}) + return + } + quote_id := c.Param("id") + err := db.QuoteDelete(context.Background(), user_id, quote_id) + if err != nil { + status, message := HandleDBError(err) + c.JSON(status, gin.H{"error": message}) + return + } + c.JSON(http.StatusNoContent, nil) +} + //#endregion Quotes diff --git a/backend/api/routes.go b/backend/api/routes.go index b7daca2..4c668fc 100644 --- a/backend/api/routes.go +++ b/backend/api/routes.go @@ -24,5 +24,6 @@ func RegisterRoutes(r *gin.Engine) { api.GET("/quotes", quotesGet) api.POST("/quotes", quoteAdd) api.GET("/quotes/:id", quoteGet) + api.DELETE("/quotes/:id", quoteDelete) } }