From 3e37e8ffef67f50817cc1788b627a2fce9a82653 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Thu, 25 May 2023 05:03:46 -0400 Subject: [PATCH] =?UTF-8?q?Am=C3=A9liorer=20insert=20error=20management?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- v4/data/data.go | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/v4/data/data.go b/v4/data/data.go index db8addb..4572dea 100644 --- a/v4/data/data.go +++ b/v4/data/data.go @@ -1,6 +1,7 @@ package data import ( + "errors" "fmt" "git.agecem.com/agecem/bottin/v4/models" @@ -68,12 +69,23 @@ func (d *DataClient) InsertMembres(membres []models.Membre) (int64, error) { } for _, membre := range membres { - _, err := tx.NamedExec("INSERT INTO membres (id, last_name, first_name, prefered_name, programme_id) VALUES (:id, :last_name, :first_name, :prefered_name, :programme_id);", &membre) + if membre.ID == "" { + tx.Rollback() + return 0, errors.New("Cannot insert membre with no membre_id") + } + result, err := tx.NamedExec("INSERT INTO membres (id, last_name, first_name, prefered_name, programme_id) VALUES (:id, :last_name, :first_name, :prefered_name, :programme_id);", &membre) if err != nil { tx.Rollback() - return rowsInserted, err + return 0, err } - rowsInserted++ + + rows, err := result.RowsAffected() + if err != nil { + tx.Rollback() + return 0, err + } + + rowsInserted += rows } err = tx.Commit() @@ -93,12 +105,24 @@ func (d *DataClient) InsertProgrammes(programmes []models.Programme) (int64, err } for _, programme := range programmes { - _, err := tx.NamedExec("INSERT INTO programmes (id, titre) VALUES (:id, :titre);", &programme) + if programme.ID == "" { + tx.Rollback() + return 0, errors.New("Cannot insert programme with no programme_id") + } + + result, err := tx.NamedExec("INSERT INTO programmes (id, titre) VALUES (:id, :titre);", &programme) if err != nil { tx.Rollback() - return rowsInserted, err + return 0, err } - rowsInserted++ + + rows, err := result.RowsAffected() + if err != nil { + tx.Rollback() + return 0, err + } + + rowsInserted += rows } err = tx.Commit()