chore: build

This commit is contained in:
2026-06-13 19:18:12 +02:00
parent 59d4f89a14
commit 093a7e0c42
8 changed files with 110 additions and 35 deletions
+14 -13
View File
@@ -45,7 +45,7 @@ func (d *Database) GetAllProductsInBasket(username string) ([]models.Panier, err
// AddRewardToBasket ajoute un produit récompense au panier (prix = 0, is_reward = true).
// Pas de vérification de stock — les récompenses sont gérées par l'admin.
func (d *Database) AddRewardToBasket(username string, productID int, quantity float64) (*models.Panier, error) {
func (d *Database) AddRewardToBasket(username string, productID int, quantity float64, poolKey string) (*models.Panier, error) {
var basket models.Panier
err := d.GDB.Transaction(func(tx *gorm.DB) error {
// Vérifier que le produit existe
@@ -53,14 +53,14 @@ func (d *Database) AddRewardToBasket(username string, productID int, quantity fl
if err := tx.Raw(`SELECT name FROM products WHERE id = ?`, productID).Scan(&productName).Error; err != nil || productName == "" {
return fmt.Errorf("produit récompense introuvable (id=%d)", productID)
}
// Supprimer tout article récompense existant pour ce produit (remplacement)
tx.Exec(`DELETE FROM baskets WHERE username = ? AND product_id = ? AND is_reward = true`, username, productID)
// Insérer avec prix 0 et is_reward = true
// Supprimer tout article récompense existant (remplacement)
tx.Exec(`DELETE FROM baskets WHERE username = ? AND is_reward = true`, username)
// Insérer avec prix 0, is_reward = true et le pool_key
return tx.Raw(`
INSERT INTO baskets (username, product_id, quantity, price, is_reward, created_at)
VALUES (?, ?, ?, 0, true, CURRENT_TIMESTAMP)
RETURNING id, username, product_id, quantity, price, is_reward, created_at`,
username, productID, quantity).Scan(&basket).Error
INSERT INTO baskets (username, product_id, quantity, price, is_reward, reward_pool_key, created_at)
VALUES (?, ?, ?, 0, true, ?, CURRENT_TIMESTAMP)
RETURNING id, username, product_id, quantity, price, is_reward, reward_pool_key, created_at`,
username, productID, quantity, poolKey).Scan(&basket).Error
})
if err != nil {
return nil, err
@@ -113,20 +113,21 @@ func (d *Database) AddToBasket(username string, productID int, quantity float64)
Quantity float64 `gorm:"column:quantity"`
Price float64 `gorm:"column:price"`
}
tx.Raw(`SELECT id, quantity, price FROM baskets WHERE username = ? AND product_id = ?`,
// Chercher uniquement un item normal (non-récompense) pour ce produit
tx.Raw(`SELECT id, quantity, price FROM baskets WHERE username = ? AND product_id = ? AND is_reward = false`,
username, productID).Scan(&existing)
if existing.ID != 0 {
return tx.Raw(`
UPDATE baskets SET quantity = ?, price = ?, created_at = CURRENT_TIMESTAMP
WHERE id = ? RETURNING id, username, product_id, quantity, price, created_at`,
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.ID).Scan(&basket).Error
}
return tx.Raw(`
INSERT INTO baskets (username, product_id, quantity, price, created_at)
VALUES (?, ?, ?, ?, CURRENT_TIMESTAMP)
RETURNING id, username, product_id, quantity, price, created_at`,
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
})
if err != nil {