fix: add unit type for piece items

This commit is contained in:
2026-06-23 14:25:09 +03:00
parent 1d226d9aaf
commit 349b092d02
3 changed files with 28 additions and 8 deletions
+13
View File
@@ -1,6 +1,8 @@
package handlers
import (
"fmt"
"math"
"net/http"
"github.com/gin-gonic/gin"
@@ -27,6 +29,13 @@ func GetCart(c *gin.Context) {
c.JSON(http.StatusOK, cartItems)
}
func isValidQuantity(item models.Item, quantity float64) bool {
if item.UnitType == "piece" {
return quantity == math.Trunc(quantity)
}
return true
}
func AddToCart(c *gin.Context) {
userID := getUserID(c)
@@ -47,6 +56,10 @@ func AddToCart(c *gin.Context) {
return
}
if !isValidQuantity(item, req.Quantity) {
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Errorf("товар %s продается поштучно, дробное количество недопустимо", item.Name)})
}
if req.Quantity > item.Stock {
c.JSON(http.StatusBadRequest, gin.H{"error": "Недостаточно товара на складе"})
}