From bae70e1569641129baa8ef743bd4c5d21f51f35b Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Sat, 16 Sep 2023 23:06:24 -0400 Subject: [PATCH] Ajouter dbclient.DBClient.InsertPresence --- dbclient/dbclient.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/dbclient/dbclient.go b/dbclient/dbclient.go index 5e763ea..d6731e3 100644 --- a/dbclient/dbclient.go +++ b/dbclient/dbclient.go @@ -5,6 +5,7 @@ import ( "fmt" "git.agecem.com/agecem/bottin-ag/dbschema" + "git.agecem.com/agecem/bottin-ag/dbstruct" "github.com/jmoiron/sqlx" ) @@ -44,3 +45,30 @@ func (d *DBClient) CreateTablesIfNotExist() error { _, err := d.DB.Exec(dbschema.Schema) return err } + +/* +InsertPresence inserts a dbstruct.Presence into the database, returning the +values of the inserted presence and any error that occured. +*/ +func (d *DBClient) InsertPresence(presence dbstruct.Presence) (dbstruct.Presence, error) { + var insertedPresence dbstruct.Presence + var rows *sqlx.Rows + + rows, err := d.DB.NamedQuery(` + INSERT INTO presences (id, at, name, programme_id) + VALUES (:id, current_timestamp, :name, :programme_id) + RETURNING id, at, name, programme_id + `, presence) + if err != nil { + return insertedPresence, err + } + defer rows.Close() + + for rows.Next() { + if err := rows.Scan(&insertedPresence.ID, &insertedPresence.At, &insertedPresence.Name, &insertedPresence.ProgrammeID); err != nil { + return insertedPresence, err + } + } + + return insertedPresence, nil +}