From a87e9a1441775009f4f3e2317d38c2f8160821e3 Mon Sep 17 00:00:00 2001 From: Xor290 Date: Tue, 24 Feb 2026 18:26:20 +0100 Subject: [PATCH] chore: add check address --- backend/gestion/db/db_address.go | 25 +++++++++++++++++++++++++ backend/gestion/db/db_init.go | 8 ++++++++ backend/gestion/handlers/panier.go | 8 ++++++++ backend/gestion/models/address.go | 11 +++++++++++ 4 files changed, 52 insertions(+) create mode 100644 backend/gestion/db/db_address.go create mode 100644 backend/gestion/models/address.go diff --git a/backend/gestion/db/db_address.go b/backend/gestion/db/db_address.go new file mode 100644 index 00000000..ed74a738 --- /dev/null +++ b/backend/gestion/db/db_address.go @@ -0,0 +1,25 @@ +package db + +import ( + "database/sql" + "errors" + "fmt" + "gestion/models" +) + +func (d *Database) CheckAddress(addressByUser *models.Command) error { + var correction models.Address + err := d.DB.QueryRow( + `SELECT invalid_address, correct_address FROM adresse_correction WHERE invalid_address = $1`, + addressByUser.DeliveryAddress, + ).Scan(&correction.InvalidAddress, &correction.CorrectAddress) + if errors.Is(err, sql.ErrNoRows) { + return nil + } + if err != nil { + return fmt.Errorf("checkAddress: %w", err) + } + var correctedAddress = correction.CorrectAddress + addressByUser.DeliveryAddress = correctedAddress + return fmt.Errorf("Adresse invalide %s", correctedAddress) +} diff --git a/backend/gestion/db/db_init.go b/backend/gestion/db/db_init.go index 538d0998..90e50a0e 100644 --- a/backend/gestion/db/db_init.go +++ b/backend/gestion/db/db_init.go @@ -251,6 +251,14 @@ func (db *Database) createTables() error { created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP );`, + + `CREATE TABLE IF NOT EXISTS adresse_correction ( + id SERIAL PRIMARY KEY, + invalid_address VARCHAR(255) NOT NULL UNIQUE, + correct_address VARCHAR(255) NOT NULL UNIQUE, + created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP + );`, // ============================ // INDEXES - ✅ AJOUT D'INDEX POUR CANCELLATIONS_COUNT // ============================ diff --git a/backend/gestion/handlers/panier.go b/backend/gestion/handlers/panier.go index 3606a41c..9e8aab37 100644 --- a/backend/gestion/handlers/panier.go +++ b/backend/gestion/handlers/panier.go @@ -6,6 +6,7 @@ package handlers import ( "gestion/db" + "gestion/models" "gestion/services" "log" "net/http" @@ -305,6 +306,13 @@ func ValidateBasket(c *gin.Context) { return } + cmd := &models.Command{DeliveryAddress: req.DeliveryAddress} + if err := database.CheckAddress(cmd); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "corrected_address": cmd.DeliveryAddress}) + return + } + req.DeliveryAddress = cmd.DeliveryAddress + log.Printf("🛒 [CHECKOUT] Début checkout pour: %s", usernameStr) // ============================================ diff --git a/backend/gestion/models/address.go b/backend/gestion/models/address.go new file mode 100644 index 00000000..65cd58ea --- /dev/null +++ b/backend/gestion/models/address.go @@ -0,0 +1,11 @@ +package models + +import "time" + +type Address struct { + ID int64 `db:"id"` + InvalidAddress string `db:"invalid_address"` + CorrectAddress string `db:"correct_address"` + CreatedAt time.Time `db:"created_at"` + UpdatedAt time.Time `db:"updated_at"` +}