chore: fix product

This commit is contained in:
2026-02-22 14:36:56 +01:00
parent 2d9bec7392
commit dd10da1536
9 changed files with 38 additions and 20 deletions
+2 -2
View File
@@ -152,7 +152,7 @@ func (db *Database) createTables() error {
`CREATE TABLE IF NOT EXISTS product_prices (
id SERIAL PRIMARY KEY,
product_id INTEGER NOT NULL REFERENCES products(id) ON DELETE CASCADE,
quantity INTEGER NOT NULL,
quantity NUMERIC(10,3) NOT NULL,
price NUMERIC(10,2) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
UNIQUE(product_id, quantity)
@@ -211,7 +211,7 @@ func (db *Database) createTables() error {
id SERIAL PRIMARY KEY,
username VARCHAR(255) NOT NULL,
product_id INTEGER NOT NULL REFERENCES products(id) ON DELETE CASCADE,
quantity INTEGER NOT NULL,
quantity NUMERIC(10,3) NOT NULL,
price NUMERIC(10,2) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);`,
+1 -1
View File
@@ -77,7 +77,7 @@ func (db *Database) CreateProduct(product interface{}) error {
log.Printf("❌ [DB CreateProduct] Erreur insertion prix[%d]: %v", i, err)
return fmt.Errorf("erreur insertion prix: %v", err)
}
log.Printf("✅ [DB CreateProduct] Prix[%d] inséré: quantity=%d, price=%.2f",
log.Printf("✅ [DB CreateProduct] Prix[%d] inséré: quantity=%g, price=%.2f",
i, price.Quantity, price.Price)
}
+2 -2
View File
@@ -38,7 +38,7 @@ func (db *Database) GetProductPrices(productID int) ([]models.ProductPrice, erro
}
// CreateProductPrice ajoute un nouveau prix pour un produit
func (db *Database) CreateProductPrice(productID, quantity int, price float64) error {
func (db *Database) CreateProductPrice(productID int, quantity float64, price float64) error {
query := `INSERT INTO product_prices (product_id, quantity, price)
VALUES ($1, $2, $3)`
_, err := db.Exec(query, productID, quantity, price)
@@ -49,7 +49,7 @@ func (db *Database) CreateProductPrice(productID, quantity int, price float64) e
}
// UpdateProductPrice met à jour un prix
func (db *Database) UpdateProductPrice(priceID, quantity int, price float64) error {
func (db *Database) UpdateProductPrice(priceID int, quantity float64, price float64) error {
query := `UPDATE product_prices SET quantity = $1, price = $2 WHERE id = $3`
result, err := db.Exec(query, quantity, price, priceID)
if err != nil {
+2 -2
View File
@@ -98,7 +98,7 @@ func validateStock(stock float64) error {
return nil
}
func validatePrice(quantity int, price float64) error {
func validatePrice(quantity float64, price float64) error {
if quantity <= 0 {
return fmt.Errorf("quantité doit être > 0")
}
@@ -258,7 +258,7 @@ func CreateProduct(c *gin.Context) {
break
}
quantity, err := strconv.Atoi(quantityStr)
quantity, err := strconv.ParseFloat(quantityStr, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Quantité invalide"})
return
+8 -8
View File
@@ -17,19 +17,19 @@ type Product struct {
type ProductPrice struct {
ID int `json:"id"`
ProductID int `json:"product_id"`
Quantity int `json:"quantity" binding:"required"`
Quantity float64 `json:"quantity" binding:"required"`
Price float64 `json:"price" binding:"required"`
CreatedAt time.Time `json:"created_at"`
}
type StockInfo struct {
ProductID int `json:"product_id"`
ProductName string `json:"product_name"`
Category string `json:"category"`
Quantity int `json:"quantity"`
Reserved int `json:"reserved"`
Available int `json:"available"`
LastUpdated string `json:"last_updated"`
ProductID int `json:"product_id"`
ProductName string `json:"product_name"`
Category string `json:"category"`
Quantity float64 `json:"quantity"`
Reserved float64 `json:"reserved"`
Available float64 `json:"available"`
LastUpdated string `json:"last_updated"`
}
// ============================================