diff --git a/backend/gestion/db/db_alert.go b/backend/gestion/db/db_alert.go index 63950898..cee7aec9 100644 --- a/backend/gestion/db/db_alert.go +++ b/backend/gestion/db/db_alert.go @@ -27,7 +27,7 @@ func (d *Database) GetAlertPolicy(id int) (models.AlertPolicy, error) { func (d *Database) GetAllAlerts() ([]models.AlertPolicy, error) { var alerts []models.AlertPolicy - if err := d.GDB.Find(&alerts).Error; err != nil { + if err := d.GDB.Order("created_at DESC").Limit(500).Find(&alerts).Error; err != nil { return nil, err } return alerts, nil @@ -65,7 +65,7 @@ func (d *Database) ActivateAlert(id int) error { func (d *Database) GetActiveAlerts() ([]models.AlertPolicy, error) { var alerts []models.AlertPolicy - if err := d.GDB.Where("status = 'true'").Order("created_at DESC").Find(&alerts).Error; err != nil { + if err := d.GDB.Where("status = 'true'").Order("created_at DESC").Limit(100).Find(&alerts).Error; err != nil { return nil, err } return alerts, nil @@ -73,7 +73,7 @@ func (d *Database) GetActiveAlerts() ([]models.AlertPolicy, error) { func (d *Database) GetAlertsByUsername(username string) ([]models.AlertPolicy, error) { var alerts []models.AlertPolicy - if err := d.GDB.Where("username = ?", username).Order("created_at DESC").Find(&alerts).Error; err != nil { + if err := d.GDB.Where("username = ?", username).Order("created_at DESC").Limit(200).Find(&alerts).Error; err != nil { return nil, err } return alerts, nil diff --git a/backend/gestion/db/db_livreur_rating.go b/backend/gestion/db/db_livreur_rating.go index a44c51ef..1cbfa1ef 100644 --- a/backend/gestion/db/db_livreur_rating.go +++ b/backend/gestion/db/db_livreur_rating.go @@ -36,7 +36,7 @@ func (d *Database) GetOrderRating(orderID int) (*LivreurRating, error) { func (d *Database) GetLivreurRatings(livreurUsername string) ([]LivreurRating, float64, error) { var ratings []LivreurRating if err := d.GDB.Raw(` - SELECT * FROM livreur_ratings WHERE livreur_username = ? ORDER BY created_at DESC + SELECT * FROM livreur_ratings WHERE livreur_username = ? ORDER BY created_at DESC LIMIT 200 `, livreurUsername).Scan(&ratings).Error; err != nil { return nil, 0, err } diff --git a/backend/gestion/db/db_media.go b/backend/gestion/db/db_media.go index 1c2b4b8a..0f2f2292 100644 --- a/backend/gestion/db/db_media.go +++ b/backend/gestion/db/db_media.go @@ -174,6 +174,20 @@ func (d *Database) GetMediaByID(mediaID int) (*models.Media, error) { return &media, nil } +// GetMediaBatch charge les médias de plusieurs produits en une seule requête. +func (d *Database) GetMediaBatch(productIDs []int) map[int][]models.Media { + result := make(map[int][]models.Media, len(productIDs)) + if len(productIDs) == 0 { + return result + } + var mediaList []models.Media + d.GDB.Raw(`SELECT id, product_id, url, type, key, created_at FROM media WHERE product_id IN ? ORDER BY product_id ASC, id ASC`, productIDs).Scan(&mediaList) + for _, m := range mediaList { + result[m.ProductID] = append(result[m.ProductID], m) + } + return result +} + func (d *Database) GetMediaByProductID(productID int) ([]models.Media, error) { log.Printf("🖼️ [GetMediaByProductID] START - ProductID=%d", productID) diff --git a/backend/gestion/db/db_product.go b/backend/gestion/db/db_product.go index e8cb11b8..f339b57c 100644 --- a/backend/gestion/db/db_product.go +++ b/backend/gestion/db/db_product.go @@ -149,23 +149,22 @@ func (d *Database) GetAllProducts() ([]models.Product, error) { return nil, err } + productIDs := make([]int, len(products)) + for i, p := range products { + productIDs[i] = p.ID + } + allPrices := d.GetProductPricesBatch(productIDs) + allMedia := d.GetMediaBatch(productIDs) for i := range products { - prices, err := d.GetProductPrices(products[i].ID) - if err != nil { - log.Printf("⚠️ [GetAllProducts] Erreur loading prices for product %d: %v", products[i].ID, err) - products[i].Prices = []models.ProductPrice{} - } else { + if prices, ok := allPrices[products[i].ID]; ok { products[i].Prices = prices - log.Printf("✅ [GetAllProducts] Loaded %d prices for product %d", len(prices), products[i].ID) - } - - media, err := d.GetMediaByProductID(products[i].ID) - if err != nil { - log.Printf("⚠️ [GetAllProducts] Erreur loading media for product %d: %v", products[i].ID, err) - products[i].Media = []models.Media{} } else { + products[i].Prices = []models.ProductPrice{} + } + if media, ok := allMedia[products[i].ID]; ok { products[i].Media = media - log.Printf("✅ [GetAllProducts] Loaded %d media for product %d", len(media), products[i].ID) + } else { + products[i].Media = []models.Media{} } } @@ -188,14 +187,16 @@ func (d *Database) GetProductsByCategory(category string) ([]models.Product, err return nil, fmt.Errorf("erreur lors de la récupération des produits: %w", err) } + catProductIDs := make([]int, len(products)) + for i, p := range products { + catProductIDs[i] = p.ID + } + catPrices := d.GetProductPricesBatch(catProductIDs) for i := range products { - prices, err := d.GetProductPrices(products[i].ID) - if err != nil { - log.Printf("⚠️ [GetProductsByCategory] Erreur loading prices for product %d: %v", products[i].ID, err) - products[i].Prices = []models.ProductPrice{} - } else { + if prices, ok := catPrices[products[i].ID]; ok { products[i].Prices = prices - log.Printf("✅ [GetProductsByCategory] Loaded %d prices for product %d", len(prices), products[i].ID) + } else { + products[i].Prices = []models.ProductPrice{} } } diff --git a/backend/gestion/db/db_product_price.go b/backend/gestion/db/db_product_price.go index 5643184f..cd92fae8 100644 --- a/backend/gestion/db/db_product_price.go +++ b/backend/gestion/db/db_product_price.go @@ -13,6 +13,20 @@ func (d *Database) GetProductPrices(productID int) ([]models.ProductPrice, error return prices, nil } +// GetProductPricesBatch charge les prix de plusieurs produits en une seule requête. +func (d *Database) GetProductPricesBatch(productIDs []int) map[int][]models.ProductPrice { + result := make(map[int][]models.ProductPrice, len(productIDs)) + if len(productIDs) == 0 { + return result + } + var prices []models.ProductPrice + d.GDB.Where("product_id IN ?", productIDs).Order("product_id ASC, quantity ASC").Find(&prices) + for _, p := range prices { + result[p.ProductID] = append(result[p.ProductID], p) + } + return result +} + func (d *Database) AddActivePrice(priceID int) error { result := d.GDB.Model(&models.ProductPrice{}). Where("id = ?", priceID). diff --git a/backend/gestion/db/db_users.go b/backend/gestion/db/db_users.go index 3675bad8..bbd87475 100644 --- a/backend/gestion/db/db_users.go +++ b/backend/gestion/db/db_users.go @@ -15,7 +15,7 @@ func (d *Database) CreateUser(user *models.User) error { func (d *Database) GetAllUsers() ([]*models.User, error) { var users []*models.User - if err := d.GDB.Order("created_at DESC").Find(&users).Error; err != nil { + if err := d.GDB.Order("created_at DESC").Limit(500).Find(&users).Error; err != nil { return nil, fmt.Errorf("erreur lors de la récupération des utilisateurs: %w", err) } return users, nil @@ -23,7 +23,7 @@ func (d *Database) GetAllUsers() ([]*models.User, error) { func (d *Database) GetAllDeliveryMen() ([]*models.User, error) { var users []*models.User - if err := d.GDB.Where("role = ?", "livreur").Find(&users).Error; err != nil { + if err := d.GDB.Where("role = ?", "livreur").Limit(100).Find(&users).Error; err != nil { return nil, fmt.Errorf("erreur lors de la récupération des livreurs: %w", err) } return users, nil