chore: update
This commit is contained in:
@@ -114,6 +114,22 @@ func validatePrice(quantity float64, price float64) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateUnit(unit string) error {
|
||||
validUnits := map[string]bool{
|
||||
"u": true, // unité
|
||||
"kg": true, // kilogramme
|
||||
"g": true, // gramme
|
||||
"bag": true, // sac
|
||||
"l": true, // litre
|
||||
"cl": true, // centilitre
|
||||
"pcs": true, // pièces
|
||||
}
|
||||
if !validUnits[unit] {
|
||||
return fmt.Errorf("unité invalide : valeurs acceptées : u, kg, g, bag, l, cl, pcs")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateCategory(category string) error {
|
||||
// Nettoyage
|
||||
category = strings.ToLower(strings.TrimSpace(category))
|
||||
@@ -205,6 +221,10 @@ func CreateProduct(c *gin.Context) {
|
||||
category := strings.TrimSpace(c.PostForm("category"))
|
||||
description := strings.TrimSpace(c.PostForm("description"))
|
||||
stockStr := c.PostForm("stock")
|
||||
unit := strings.ToLower(strings.TrimSpace(c.PostForm("unit")))
|
||||
if unit == "" {
|
||||
unit = "u"
|
||||
}
|
||||
|
||||
// ✅ VALIDATION STRICTE
|
||||
if err := validateProductName(name); err != nil {
|
||||
@@ -231,6 +251,11 @@ func CreateProduct(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if err := validateUnit(unit); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
// ✅ VALIDER LE STOCK
|
||||
stock, err := strconv.ParseFloat(stockStr, 64)
|
||||
if err != nil {
|
||||
@@ -296,6 +321,7 @@ func CreateProduct(c *gin.Context) {
|
||||
Category: category,
|
||||
Description: description,
|
||||
Stock: stock,
|
||||
Unit: unit,
|
||||
Prices: prices,
|
||||
}
|
||||
|
||||
@@ -581,6 +607,7 @@ func UpdateProduct(c *gin.Context) {
|
||||
Category string `json:"category"`
|
||||
Description string `json:"description"`
|
||||
Stock float64 `json:"stock"`
|
||||
Unit string `json:"unit"`
|
||||
Prices []models.ProductPrice `json:"prices"`
|
||||
}
|
||||
|
||||
@@ -605,6 +632,14 @@ func UpdateProduct(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if updateData.Unit == "" {
|
||||
updateData.Unit = "u"
|
||||
}
|
||||
if err := validateUnit(updateData.Unit); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if err := validateStock(updateData.Stock); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
@@ -627,8 +662,8 @@ func UpdateProduct(c *gin.Context) {
|
||||
// ✅ UPDATE PRODUIT
|
||||
updateQuery := `
|
||||
UPDATE products
|
||||
SET name = $1, category = $2, description = $3, stock = $4, updated_at = $5
|
||||
WHERE id = $6
|
||||
SET name = $1, category = $2, description = $3, stock = $4, unit = $5, updated_at = $6
|
||||
WHERE id = $7
|
||||
`
|
||||
|
||||
_, err = database.Exec(updateQuery,
|
||||
@@ -636,6 +671,7 @@ func UpdateProduct(c *gin.Context) {
|
||||
updateData.Category,
|
||||
updateData.Description,
|
||||
updateData.Stock,
|
||||
updateData.Unit,
|
||||
time.Now(),
|
||||
id,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user