feat: desactive and active price for product

This commit is contained in:
Xor290
2026-05-13 10:31:52 +02:00
parent 9a2f0d91ad
commit fb63855d9b
4 changed files with 83 additions and 7 deletions
+28
View File
@@ -43,3 +43,31 @@ func (d *Database) DeleteProductPrice(priceID int) error {
} }
return nil 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
}
+44
View File
@@ -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 // DELETE PRODUCT - VERSION SÉCURISÉE
// ============================================ // ============================================
+1
View File
@@ -23,6 +23,7 @@ type ProductPrice struct {
Quantity float64 `json:"quantity" gorm:"column:quantity" binding:"required"` Quantity float64 `json:"quantity" gorm:"column:quantity" binding:"required"`
Price float64 `json:"price" gorm:"column:price" binding:"required"` Price float64 `json:"price" gorm:"column:price" binding:"required"`
CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime"` 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" } func (ProductPrice) TableName() string { return "product_prices" }
+3
View File
@@ -199,6 +199,9 @@ func SetupRoutes(router *gin.Engine, database *db.Database, geoService *services
// ============================================ // ============================================
adminGroupV2.GET("/stats", handlers.GetAdminStats) 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 // COMMANDES - GESTION DE BASE
// ============================================ // ============================================