summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxengineering <me@xengineering.eu>2024-04-07 10:42:03 +0200
committerxengineering <me@xengineering.eu>2024-04-07 10:47:36 +0200
commitc4a4a8b5f60a568abd2af614ca4a5d06855bc3a1 (patch)
tree864d9022dc9a1ad6f832f1c97201af817c7b883b
parent537bbcea3b2477eeae7d86422a073558185cf4eb (diff)
downloadceres-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.
-rw-r--r--controller/recipe.go48
-rw-r--r--model/object.go15
-rw-r--r--view/recipe.go16
3 files changed, 23 insertions, 56 deletions
diff --git a/controller/recipe.go b/controller/recipe.go
index da58d35..9427b0d 100644
--- a/controller/recipe.go
+++ b/controller/recipe.go
@@ -18,20 +18,8 @@ func RecipeCreate(w http.ResponseWriter, r *http.Request) {
recipe.LastChanged = fmt.Sprint(time.Now().Unix())
recipe.Created = recipe.LastChanged
- tx, err := model.NewTx()
- if err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
- return
- }
-
- err = recipe.Create(tx)
- if err != nil {
- model.Rollback(tx)
- http.Error(w, err.Error(), http.StatusInternalServerError)
- return
- }
-
- err = tx.Commit()
+ var obj model.Object = &recipe
+ err := model.SafeCrud(obj.Create)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
@@ -61,20 +49,8 @@ func RecipeUpdate(w http.ResponseWriter, r *http.Request) {
recipe.LastChanged = fmt.Sprint(time.Now().Unix())
- tx, err := model.NewTx()
- if err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
- return
- }
-
- err = recipe.Update(tx)
- if err != nil {
- model.Rollback(tx)
- http.Error(w, err.Error(), http.StatusInternalServerError)
- return
- }
-
- err = tx.Commit()
+ var obj model.Object = &recipe
+ err = model.SafeCrud(obj.Update)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
@@ -87,20 +63,8 @@ func RecipeDelete(w http.ResponseWriter, r *http.Request) {
recipe := model.Recipe{}
recipe.Id = mux.Vars(r)[`id`]
- tx, err := model.NewTx()
- if err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
- return
- }
-
- err = recipe.Delete(tx)
- if err != nil {
- model.Rollback(tx)
- http.Error(w, err.Error(), http.StatusInternalServerError)
- return
- }
-
- err = tx.Commit()
+ var obj model.Object = &recipe
+ err := model.SafeCrud(obj.Delete)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
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()
+}
diff --git a/view/recipe.go b/view/recipe.go
index 52b7a7e..ba670a2 100644
--- a/view/recipe.go
+++ b/view/recipe.go
@@ -12,20 +12,8 @@ func RecipeRead(w http.ResponseWriter, r *http.Request) {
recipe := model.Recipe{}
recipe.Id = mux.Vars(r)[`id`]
- tx, err := model.NewTx()
- if err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
- return
- }
-
- err = recipe.Read(tx)
- if err != nil {
- model.Rollback(tx)
- http.Error(w, err.Error(), http.StatusInternalServerError)
- return
- }
-
- err = tx.Commit()
+ var obj model.Object = &recipe
+ err := model.SafeCrud(obj.Read)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return