diff options
author | xengineering <me@xengineering.eu> | 2024-04-07 10:42:03 +0200 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2024-04-07 10:47:36 +0200 |
commit | c4a4a8b5f60a568abd2af614ca4a5d06855bc3a1 (patch) | |
tree | 864d9022dc9a1ad6f832f1c97201af817c7b883b /model | |
parent | 537bbcea3b2477eeae7d86422a073558185cf4eb (diff) | |
download | ceres-c4a4a8b5f60a568abd2af614ca4a5d06855bc3a1.tar ceres-c4a4a8b5f60a568abd2af614ca4a5d06855bc3a1.tar.zst ceres-c4a4a8b5f60a568abd2af614ca4a5d06855bc3a1.zip |
model: Add helper function for safe CRUD
This removes the redundant setup of a database/sql.Tx in each HTTP
handler.
Diffstat (limited to 'model')
-rw-r--r-- | model/object.go | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/model/object.go b/model/object.go index 63ef419..bcbba3e 100644 --- a/model/object.go +++ b/model/object.go @@ -10,3 +10,18 @@ type Object interface { Update(tx *sql.Tx) error Delete(tx *sql.Tx) error } + +func SafeCrud(crud func(tx *sql.Tx) error) error { + tx, err := NewTx() + if err != nil { + return err + } + + err = crud(tx) + if err != nil { + Rollback(tx) + return err + } + + return tx.Commit() +} |