From 875452004de1dc3b556be8be02adaa4d061b5904 Mon Sep 17 00:00:00 2001 From: Masahiko AMANO Date: Mon, 6 Jan 2025 15:23:19 +0300 Subject: [PATCH] init(api): add single quote get 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 bfff4ac..9effb8a 100644 --- a/backend/api/handlers.go +++ b/backend/api/handlers.go @@ -88,4 +88,21 @@ func quotesGet(c *gin.Context) { c.JSON(http.StatusOK, quotes) } +func quoteGet(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") + quote, err := db.QuoteGet(context.Background(), user_id, quote_id) + if err != nil { + status, message := HandleDBError(err) + c.JSON(status, gin.H{"error": message}) + return + } + c.JSON(http.StatusOK, quote) +} + //#endregion Quotes diff --git a/backend/api/routes.go b/backend/api/routes.go index 106b256..08f8ba6 100644 --- a/backend/api/routes.go +++ b/backend/api/routes.go @@ -22,5 +22,6 @@ func RegisterRoutes(r *gin.Engine) { { api.POST("/auth", userAuth) api.GET("/quotes", quotesGet) + api.GET("/quotes/:id", quoteGet) } }