diff --git a/.gitignore b/.gitignore index f38719fb..637aa471 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ # Expo local state (in sub-projects) **/.expo/ +test_address.sh diff --git a/backend/gestion/db/db_address.go b/backend/gestion/db/db_address.go index ed74a738..e83a63eb 100644 --- a/backend/gestion/db/db_address.go +++ b/backend/gestion/db/db_address.go @@ -23,3 +23,27 @@ func (d *Database) CheckAddress(addressByUser *models.Command) error { addressByUser.DeliveryAddress = correctedAddress return fmt.Errorf("Adresse invalide %s", correctedAddress) } + +func (d *Database) AddAddress(CorrectAddressByAdmin string, InvalidAddressByAdmin string) error { + _, err := d.DB.Exec( + `INSERT INTO adresse_correction + (invalid_address, correct_address) + VALUES ($1, $2)`, + InvalidAddressByAdmin, CorrectAddressByAdmin, + ) + if err != nil { + return fmt.Errorf("addAddress: %w", err) + } + return nil +} + +func (d *Database) DeleteAddress(InvalidAddressByAdmin string, CorrectAddressByAdmin string) error { + _, err := d.DB.Exec( + `DELETE FROM adresse_correction WHERE invalid_address = $1 AND correct_address = $2`, + InvalidAddressByAdmin, CorrectAddressByAdmin, + ) + if err != nil { + return fmt.Errorf("deleteAddress: %w", err) + } + return nil +} diff --git a/backend/gestion/db/db_product_price.go b/backend/gestion/db/db_product_price.go index 69ee0fdd..4b2500af 100644 --- a/backend/gestion/db/db_product_price.go +++ b/backend/gestion/db/db_product_price.go @@ -29,7 +29,7 @@ func (db *Database) GetProductPrices(productID int) ([]models.ProductPrice, erro log.Printf("❌ [GetProductPrices] Erreur scan: %v", err) return nil, err } - log.Printf(" 💰 Price: qty=%d, price=%.2f", price.Quantity, price.Price) + log.Printf(" 💰 Price: qty=%.0f, price=%.2f", price.Quantity, price.Price) prices = append(prices, price) } diff --git a/backend/gestion/handlers/address.go b/backend/gestion/handlers/address.go new file mode 100644 index 00000000..349eda8f --- /dev/null +++ b/backend/gestion/handlers/address.go @@ -0,0 +1,59 @@ +package handlers + +import ( + "gestion/db" + "net/http" + + "github.com/gin-gonic/gin" +) + +func AddAddress(c *gin.Context) { + database := c.MustGet("database").(*db.Database) + + userRole := c.GetString("role") + if userRole != "admin" { + c.JSON(http.StatusForbidden, gin.H{"error": "Accès réservé aux administrateurs"}) + return + } + + var req struct { + CorrectAddress string `json:"correct_address" binding:"required"` + InvalidAddress string `json:"invalid_address" binding:"required"` + } + if err := c.ShouldBindJSON(&req); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + + if err := database.AddAddress(req.CorrectAddress, req.InvalidAddress); err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + return + } + + c.JSON(http.StatusCreated, gin.H{"message": "Adresses ajoutées avec succès"}) +} + +func DeleteAddress(c *gin.Context) { + database := c.MustGet("database").(*db.Database) + + userRole := c.GetString("role") + if userRole != "admin" { + c.JSON(http.StatusForbidden, gin.H{"error": "Accès réservé aux administrateurs"}) + return + } + var req struct { + CorrectAddress string `json:"correct_address" binding:"required"` + InvalidAddress string `json:"invalid_address" binding:"required"` + } + if err := c.ShouldBindJSON(&req); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + + if err := database.DeleteAddress(req.InvalidAddress, req.CorrectAddress); err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + return + } + + c.JSON(http.StatusOK, gin.H{"message": "Adresses supprimées avec succès"}) +} diff --git a/backend/gestion/routes/routes.go b/backend/gestion/routes/routes.go index 8fb9f944..2f366b08 100644 --- a/backend/gestion/routes/routes.go +++ b/backend/gestion/routes/routes.go @@ -106,6 +106,7 @@ func SetupRoutes(router *gin.Engine, database *db.Database, geoService *services // ============================================ adminAuthGroupV2 := router.Group("/api/v2/admin/auth") { + adminAuthGroupV2.POST("/register", handlers.RegisterAdmin) adminAuthGroupV2.POST("/login", handlers.LoginAdmin) adminAuthGroupV2.POST("/logout", handlers.LogoutAdmin) } @@ -130,6 +131,11 @@ func SetupRoutes(router *gin.Engine, database *db.Database, geoService *services // Get location livreur adminGroupV2.GET("/commands/:id/deliveryman/location", handlers.GetDeliverymanLocationForCommand) // ============================================ + // ADDRESSES - GESTION + // ============================================ + adminGroupV2.POST("/add/address", handlers.AddAddress) + adminGroupV2.DELETE("/delete/address", handlers.DeleteAddress) + // ============================================ // PRODUITS - GESTION // ============================================ adminGroupV2.POST("/products", handlers.CreateProduct)