diff --git a/backend/handlers/cart_handler.go b/backend/handlers/cart_handler.go index 7540f60..da0ff3c 100644 --- a/backend/handlers/cart_handler.go +++ b/backend/handlers/cart_handler.go @@ -1,7 +1,6 @@ package handlers import ( - "fmt" "math" "net/http" @@ -57,17 +56,18 @@ func AddToCart(c *gin.Context) { } if !isValidQuantity(item, req.Quantity) { - c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Errorf("товар %s продается поштучно, дробное количество недопустимо", item.Name)}) + c.JSON(http.StatusBadRequest, gin.H{"error": "товар продается поштучно, дробное количество недопустимо"}) + return } if req.Quantity > item.Stock { c.JSON(http.StatusBadRequest, gin.H{"error": "Недостаточно товара на складе"}) + return } var cartItem models.CartItem err := database.DB.Where("user_id = ? AND item_id = ?", userID, req.ItemID).First(&cartItem).Error if err != nil { - cartItem = models.CartItem{ UserID: userID, ItemID: req.ItemID, diff --git a/backend/handlers/order_handler.go b/backend/handlers/order_handler.go index 5ec6c9f..093bd22 100644 --- a/backend/handlers/order_handler.go +++ b/backend/handlers/order_handler.go @@ -121,3 +121,33 @@ func GetAllOrders(c *gin.Context) { c.JSON(http.StatusOK, orders) } + +func UpdateOrderStatus(c *gin.Context) { + orderID := c.Param("id") + + var req struct { + Status string `json:"status" binding:"required"` + } + + if err := c.ShouldBindJSON(&req); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": "Не указан статус заказа"}) + return + } + + if req.Status != "new" && req.Status != "packed" && req.Status != "delivered" && req.Status != "cancel" { + c.JSON(http.StatusBadRequest, gin.H{"error": "Неверно указанный статус заказа"}) + } + + result := database.DB.Model(&models.Order{}).Where("id = ? ", orderID).Update("status", req.Status) + if result.Error != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": "Ошибка при изменении статуса заказа"}) + return + } + + if result.RowsAffected == 0 { + c.JSON(http.StatusNotFound, gin.H{"error": "Заказ не найден"}) + return + } + + c.JSON(http.StatusAccepted, "Статус заказа успешно изменен") +} diff --git a/backend/main.go b/backend/main.go index e5a82f8..eb93bb7 100644 --- a/backend/main.go +++ b/backend/main.go @@ -63,6 +63,7 @@ func main() { { admin.POST("/register", handlers.RegisterAdmin) admin.GET("/orders", handlers.GetAllOrders) + admin.PATCH("/orders/:id", handlers.UpdateOrderStatus) } }