diff --git a/backend/api/handlers.go b/backend/api/handlers.go index 1cd32c7..531c795 100644 --- a/backend/api/handlers.go +++ b/backend/api/handlers.go @@ -7,6 +7,7 @@ import ( "time" "github.com/H1K0/SkazaNull/db" + "github.com/H1K0/SkazaNull/models" "github.com/gin-contrib/sessions" "github.com/gin-gonic/gin" ) @@ -148,6 +149,57 @@ func quoteAdd(c *gin.Context) { c.JSON(http.StatusCreated, quote) } +func quoteUpdate(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") + var body map[string]string + err := c.BindJSON(&body) + if err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": "И чё за шнягу ты мне кинул?"}) + return + } + var quote models.Quote + ctx := context.Background() + newText, ok := body["text"] + if ok && newText != "" { + quote, err = db.QuoteUpdateText(ctx, user_id, quote_id, newText) + if err != nil { + status, message := HandleDBError(err) + c.JSON(status, gin.H{"error": message}) + return + } + } + newAuthor, ok := body["author"] + if ok && newAuthor != "" { + quote, err = db.QuoteUpdateAuthor(ctx, user_id, quote_id, newAuthor) + if err != nil { + status, message := HandleDBError(err) + c.JSON(status, gin.H{"error": message}) + return + } + } + datetimeStr, ok := body["datetime"] + if ok && datetimeStr != "" { + newDatetime, err := time.Parse(time.RFC3339, datetimeStr) + if err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": "Чёт дата и время у тебя какие-то кривые..."}) + return + } + quote, err = db.QuoteUpdateDatetime(context.Background(), user_id, quote_id, newDatetime) + if err != nil { + status, message := HandleDBError(err) + c.JSON(status, gin.H{"error": message}) + return + } + } + c.JSON(http.StatusOK, quote) +} + func quoteDelete(c *gin.Context) { session := sessions.Default(c) user_id, ok := session.Get("user_id").(string) diff --git a/backend/api/routes.go b/backend/api/routes.go index 4c668fc..c7f2c70 100644 --- a/backend/api/routes.go +++ b/backend/api/routes.go @@ -24,6 +24,7 @@ func RegisterRoutes(r *gin.Engine) { api.GET("/quotes", quotesGet) api.POST("/quotes", quoteAdd) api.GET("/quotes/:id", quoteGet) + api.PATCH("/quotes/:id", quoteUpdate) api.DELETE("/quotes/:id", quoteDelete) } }