diff options
author | xengineering <me@xengineering.eu> | 2024-05-17 22:49:15 +0200 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2024-10-12 15:24:33 +0200 |
commit | 9d3a5fb85dc02e87fab879855c4c3bace5f753f2 (patch) | |
tree | fa56a768a78c07e65e1402f6413411ffd801d87a /model | |
parent | 6d59ff7a68af2823b5566dd24e3ff4aa77cbc5fe (diff) | |
download | ceres-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.
Diffstat (limited to 'model')
-rw-r--r-- | model/recipes.go | 18 |
1 files changed, 16 insertions, 2 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{ { |