From fb63855d9bf0420e203573422ac8ddd3758a7ca5 Mon Sep 17 00:00:00 2001 From: Xor290 Date: Wed, 13 May 2026 10:31:52 +0200 Subject: [PATCH] feat: desactive and active price for product --- backend/gestion/db/db_product_price.go | 28 ++++++++++++++++ backend/gestion/handlers/product.go | 44 ++++++++++++++++++++++++++ backend/gestion/models/product.go | 11 ++++--- backend/gestion/routes/routes.go | 7 ++-- 4 files changed, 83 insertions(+), 7 deletions(-) diff --git a/backend/gestion/db/db_product_price.go b/backend/gestion/db/db_product_price.go index f9cd7949..b8ef59d2 100644 --- a/backend/gestion/db/db_product_price.go +++ b/backend/gestion/db/db_product_price.go @@ -43,3 +43,31 @@ func (d *Database) DeleteProductPrice(priceID int) error { } return nil } + +func (d *Database) AddActivePrice(priceID int) error { + result := d.GDB.Model(&models.ProductPrice{}). + Where("id = ?", priceID). + Update("active_price", true) + + if result.Error != nil { + return fmt.Errorf("erreur lors de l'activation du prix: %w", result.Error) + } + if result.RowsAffected == 0 { + return fmt.Errorf("prix introuvable") + } + return nil +} + +func (d *Database) DeActivePrice(priceID int) error { + result := d.GDB.Model(&models.ProductPrice{}). + Where("id = ?", priceID). + Update("active_price", false) + + if result.Error != nil { + return fmt.Errorf("erreur lors de l'activation du prix: %w", result.Error) + } + if result.RowsAffected == 0 { + return fmt.Errorf("prix introuvable") + } + return nil +} diff --git a/backend/gestion/handlers/product.go b/backend/gestion/handlers/product.go index 6b7fd506..b8d29e10 100644 --- a/backend/gestion/handlers/product.go +++ b/backend/gestion/handlers/product.go @@ -849,6 +849,50 @@ func UploadMedia(c *gin.Context) { }) } +func ActivePrice(c *gin.Context) { + database := c.MustGet("database").(*db.Database) + role := c.GetString("role") + if role != "admin" { + c.JSON(http.StatusForbidden, gin.H{"error": "Accès refusé"}) + return + } + + id, err := strconv.Atoi(c.Param("id")) + if err != nil || id <= 0 { + c.JSON(http.StatusBadRequest, gin.H{"error": "ID invalide"}) + return + } + + if err := database.AddActivePrice(id); err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + return + } + + c.JSON(http.StatusOK, gin.H{"message": "Prix activé avec succès"}) +} + +func DesActivePrice(c *gin.Context) { + database := c.MustGet("database").(*db.Database) + role := c.GetString("role") + if role != "admin" { + c.JSON(http.StatusForbidden, gin.H{"error": "Accès refusé"}) + return + } + + id, err := strconv.Atoi(c.Param("id")) + if err != nil || id <= 0 { + c.JSON(http.StatusBadRequest, gin.H{"error": "ID invalide"}) + return + } + + if err := database.DeActivePrice(id); err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + return + } + + c.JSON(http.StatusOK, gin.H{"message": "Prix désactivé avec succès"}) +} + // ============================================ // DELETE PRODUCT - VERSION SÉCURISÉE // ============================================ diff --git a/backend/gestion/models/product.go b/backend/gestion/models/product.go index b75ccdad..0103c371 100644 --- a/backend/gestion/models/product.go +++ b/backend/gestion/models/product.go @@ -18,11 +18,12 @@ type Product struct { func (Product) TableName() string { return "products" } type ProductPrice struct { - ID int `json:"id" gorm:"primaryKey;autoIncrement"` - ProductID int `json:"product_id" gorm:"column:product_id;index"` - Quantity float64 `json:"quantity" gorm:"column:quantity" binding:"required"` - Price float64 `json:"price" gorm:"column:price" binding:"required"` - CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime"` + ID int `json:"id" gorm:"primaryKey;autoIncrement"` + ProductID int `json:"product_id" gorm:"column:product_id;index"` + Quantity float64 `json:"quantity" gorm:"column:quantity" binding:"required"` + Price float64 `json:"price" gorm:"column:price" binding:"required"` + CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime"` + ActivePrice bool `json:"active_price" gorm:"column:active_price;default:true" binding:"required"` } func (ProductPrice) TableName() string { return "product_prices" } diff --git a/backend/gestion/routes/routes.go b/backend/gestion/routes/routes.go index f2299808..bd737ec9 100644 --- a/backend/gestion/routes/routes.go +++ b/backend/gestion/routes/routes.go @@ -199,6 +199,9 @@ func SetupRoutes(router *gin.Engine, database *db.Database, geoService *services // ============================================ adminGroupV2.GET("/stats", handlers.GetAdminStats) + adminGroupV2.POST("/active/product/price/:id", handlers.ActivePrice) + adminGroupV2.POST("/desactive/product/price/:id", handlers.DesActivePrice) + // ============================================ // COMMANDES - GESTION DE BASE // ============================================ @@ -348,8 +351,8 @@ func SetupRoutes(router *gin.Engine, database *db.Database, geoService *services livreurGroupV1.GET("/deliveries", handlers.GetMyDeliveries) // ✅ Données filtrées livreurGroupV1.GET("/deliveries/:id", handlers.GetDeliveryDetails) // ✅ Détail filtré livreurGroupV1.POST("/deliveries/:id/start", handlers.StartDelivery) - livreurGroupV1.PUT("/deliveries/:id/status", handlers.UpdateDeliveryStatus) // ✅ Avec GPS - livreurGroupV1.POST("/deliveries/:id/issue", handlers.ReportDeliveryIssue) // Motif non-livraison + livreurGroupV1.PUT("/deliveries/:id/status", handlers.UpdateDeliveryStatus) // ✅ Avec GPS + livreurGroupV1.POST("/deliveries/:id/issue", handlers.ReportDeliveryIssue) // Motif non-livraison livreurGroupV1.GET("/deliveries/:id/nav-link", handlers.GetLivreurNavLink) // Lien Waze App // ============================================