chore: add address routes

This commit is contained in:
2026-02-24 20:52:44 +01:00
parent a87e9a1441
commit b05f844940
5 changed files with 91 additions and 1 deletions
+1
View File
@@ -1,2 +1,3 @@
# Expo local state (in sub-projects)
**/.expo/
test_address.sh
+24
View File
@@ -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
}
+1 -1
View File
@@ -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)
}
+59
View File
@@ -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"})
}
+6
View File
@@ -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)