chore: build

This commit is contained in:
Xor290
2026-09-13 16:11:26 +02:00
parent 91745636ec
commit f2f537a194
10 changed files with 806 additions and 16 deletions
+13 -5
View File
@@ -137,9 +137,6 @@ func (d *Database) AddToBasket(username string, productID int, quantity float64)
if err := tx.Raw(`SELECT stock, category FROM products WHERE id = ? FOR UPDATE`, productID).Scan(&productInfo).Error; err != nil {
return fmt.Errorf("erreur lecture stock: %w", err)
}
if productInfo.Stock < quantity {
return fmt.Errorf("stock insuffisant")
}
var priceResult struct {
Price float64 `gorm:"column:price"`
@@ -158,6 +155,17 @@ func (d *Database) AddToBasket(username string, productID int, quantity float64)
priceResult.Price = discounted
}
// Offre "achetez X, Y offert" : le client reçoit une quantité
// supplémentaire du même produit, gratuite, sans changer le prix déjà
// calculé sur la quantité demandée — la quantité livrée/décomptée du
// stock est donc supérieure à la quantité facturée.
freeQuantity := d.ResolveFreeGiftQuantity(productID, productInfo.Category, quantity)
deliveredQuantity := quantity + freeQuantity
if productInfo.Stock < deliveredQuantity {
return fmt.Errorf("stock insuffisant")
}
var existing struct {
ID int `gorm:"column:id"`
Quantity float64 `gorm:"column:quantity"`
@@ -171,14 +179,14 @@ func (d *Database) AddToBasket(username string, productID int, quantity float64)
return tx.Raw(`
UPDATE baskets SET quantity = ?, price = ?, created_at = CURRENT_TIMESTAMP
WHERE id = ? AND is_reward = false RETURNING id, username, product_id, quantity, price, is_reward, created_at`,
existing.Quantity+quantity, existing.Price+priceResult.Price,
existing.Quantity+deliveredQuantity, existing.Price+priceResult.Price,
existing.ID).Scan(&basket).Error
}
return tx.Raw(`
INSERT INTO baskets (username, product_id, quantity, price, is_reward, created_at)
VALUES (?, ?, ?, ?, false, CURRENT_TIMESTAMP)
RETURNING id, username, product_id, quantity, price, is_reward, created_at`,
username, productID, quantity, priceResult.Price).Scan(&basket).Error
username, productID, deliveredQuantity, priceResult.Price).Scan(&basket).Error
})
if err != nil {
return nil, err