summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxengineering <me@xengineering.eu>2024-05-17 22:49:15 +0200
committerxengineering <me@xengineering.eu>2024-10-12 15:24:33 +0200
commit9d3a5fb85dc02e87fab879855c4c3bace5f753f2 (patch)
treefa56a768a78c07e65e1402f6413411ffd801d87a
parent6d59ff7a68af2823b5566dd24e3ff4aa77cbc5fe (diff)
downloadceres-9d3a5fb85dc02e87fab879855c4c3bace5f753f2.tar
ceres-9d3a5fb85dc02e87fab879855c4c3bace5f753f2.tar.zst
ceres-9d3a5fb85dc02e87fab879855c4c3bace5f753f2.zip
model: Fix direct access to database
Instead a database transaction has to be used. Each database interaction should be wrapped into a transaction to make sure any possible change (even side-effects) can be rolled back in case of errors.
-rw-r--r--model/recipes.go18
-rw-r--r--view/recipes.go3
2 files changed, 18 insertions, 3 deletions
diff --git a/model/recipes.go b/model/recipes.go
index fd8115f..324589a 100644
--- a/model/recipes.go
+++ b/model/recipes.go
@@ -1,7 +1,9 @@
package model
import (
+ "database/sql"
"errors"
+ "fmt"
)
type RecipesElement struct {
@@ -11,14 +13,18 @@ type RecipesElement struct {
type Recipes []RecipesElement
-func (r *Recipes) Read() error {
+func (r *Recipes) Create(tx *sql.Tx) error {
+ return fmt.Errorf("Impossible to create a recipe list")
+}
+
+func (r *Recipes) Read(tx *sql.Tx) error {
if len(*r) != 0 {
return errors.New("Recipes has to contain zero elements for .Read()")
}
query := `SELECT id, title FROM recipes`
- rows, err := db.Query(query)
+ rows, err := tx.Query(query)
if err != nil {
return err
}
@@ -36,6 +42,14 @@ func (r *Recipes) Read() error {
return nil
}
+func (r *Recipes) Update(tx *sql.Tx) error {
+ return fmt.Errorf("Impossible to update a recipe list")
+}
+
+func (r *Recipes) Delete(tx *sql.Tx) error {
+ return fmt.Errorf("Impossible to delete a recipe list")
+}
+
func RecipesTestData() Recipes {
return []RecipesElement{
{
diff --git a/view/recipes.go b/view/recipes.go
index bd4c72b..fe995b2 100644
--- a/view/recipes.go
+++ b/view/recipes.go
@@ -9,7 +9,8 @@ import (
func RecipesRead(w http.ResponseWriter, r *http.Request) {
recipes := make(model.Recipes, 0)
- err := recipes.Read()
+ var obj model.Object = &recipes
+ err := model.Transaction(obj.Read)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return