fix: add unit type for piece items
This commit is contained in:
@@ -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": "Недостаточно товара на складе"})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user