fix: change route & lower case error print
This commit is contained in:
@@ -45,7 +45,7 @@ func CreateOrder(c *gin.Context) {
|
|||||||
result := tx.Model(&models.Item{}).Where("id = ? AND stock >= ?", item.ID, cartItem.Quantity).Update("stock", gorm.Expr("stock - ? ", cartItem.Quantity))
|
result := tx.Model(&models.Item{}).Where("id = ? AND stock >= ?", item.ID, cartItem.Quantity).Update("stock", gorm.Expr("stock - ? ", cartItem.Quantity))
|
||||||
|
|
||||||
if result.RowsAffected == 0 {
|
if result.RowsAffected == 0 {
|
||||||
return fmt.Errorf("Недостаточно товара '%s' на складе", item.Name)
|
return fmt.Errorf("недостаточно товара '%s' на складе", item.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
itemTotal := float64(item.Price) * cartItem.Quantity
|
itemTotal := float64(item.Price) * cartItem.Quantity
|
||||||
@@ -83,3 +83,15 @@ func CreateOrder(c *gin.Context) {
|
|||||||
|
|
||||||
c.JSON(http.StatusCreated, order)
|
c.JSON(http.StatusCreated, order)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetUserOrders(c *gin.Context) {
|
||||||
|
userID := getUserID(c)
|
||||||
|
|
||||||
|
var orders []models.Order
|
||||||
|
if err := database.DB.Preload("Items.Item").Where("user_id = ? ", userID).Find(&orders).Error; err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Не удалось вывести заказы"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, orders)
|
||||||
|
}
|
||||||
|
|||||||
@@ -53,6 +53,7 @@ func main() {
|
|||||||
protected.PATCH("/cart/:id", handlers.UpdateCartItem)
|
protected.PATCH("/cart/:id", handlers.UpdateCartItem)
|
||||||
protected.DELETE("/cart/:id", handlers.RemoveFromCart)
|
protected.DELETE("/cart/:id", handlers.RemoveFromCart)
|
||||||
protected.POST("/orders/", handlers.CreateOrder)
|
protected.POST("/orders/", handlers.CreateOrder)
|
||||||
|
protected.GET("/orders", handlers.GetUserOrders)
|
||||||
}
|
}
|
||||||
|
|
||||||
admin := protected.Group("/")
|
admin := protected.Group("/")
|
||||||
|
|||||||
Reference in New Issue
Block a user