summaryrefslogtreecommitdiff
path: root/model/recipe.go
diff options
context:
space:
mode:
Diffstat (limited to 'model/recipe.go')
-rw-r--r--model/recipe.go100
1 files changed, 84 insertions, 16 deletions
diff --git a/model/recipe.go b/model/recipe.go
index acd9a3b..6750426 100644
--- a/model/recipe.go
+++ b/model/recipe.go
@@ -15,6 +15,7 @@ type Recipe struct {
Notes string `json:"notes"`
Created string `json:"created"`
LastChanged string `json:"last_changed"`
+ Steps []Step `json:"steps"`
}
func (r Recipe) String() string {
@@ -23,38 +24,63 @@ func (r Recipe) String() string {
}
func (r *Recipe) Create() error {
- query := `INSERT INTO recipes
+ tx, err := db.Begin()
+ if err != nil {
+ return err
+ }
+
+ cmd := `
+INSERT INTO recipes
(title, portions, url, notes, created, last_changed)
VALUES
- (?, ?, ?, ?, ?, ?)`
+ (?, ?, ?, ?, ?, ?)
+`
- result, err := db.Exec(query, r.Title, r.Portions, r.Url, r.Notes,
- r.Created, r.LastChanged)
+ result, err := tx.Exec(cmd, r.Title, r.Portions, r.Url, r.Notes, r.Created,
+ r.LastChanged)
if err != nil {
+ rollback(tx)
return err
}
id, err := result.LastInsertId()
if err != nil {
+ rollback(tx)
return err
}
+
r.Id = fmt.Sprint(id)
- return nil
+ err = r.CreateSteps(tx)
+ if err != nil {
+ rollback(tx)
+ return err
+ }
+
+ return tx.Commit()
}
func (r *Recipe) Read() error {
- query := `SELECT id, title, portions, url, notes, created, last_changed
+ tx, err := db.Begin()
+ if err != nil {
+ return err
+ }
+
+ cmd := `
+SELECT id, title, portions, url, notes, created, last_changed
FROM recipes
-WHERE id = ?`
+WHERE id = ?
+`
- rows, err := db.Query(query, r.Id)
+ rows, err := tx.Query(cmd, r.Id)
if err != nil {
+ rollback(tx)
return err
}
defer rows.Close()
if !rows.Next() {
+ rollback(tx)
return sql.ErrNoRows
}
@@ -68,17 +94,31 @@ WHERE id = ?`
&r.LastChanged,
)
if err != nil {
+ rollback(tx)
return err
}
- if rows.Next() {
- return errors.New("model: More than one object found on read")
+ err = r.ReadSteps(tx)
+ if err != nil {
+ rollback(tx)
+ return err
}
- return nil
+ return tx.Commit()
}
func (r *Recipe) Update() error {
+ tx, err := db.Begin()
+ if err != nil {
+ return err
+ }
+
+ err = r.UpdateSteps(tx)
+ if err != nil {
+ rollback(tx)
+ return err
+ }
+
query := `UPDATE
recipes
SET
@@ -90,36 +130,58 @@ SET
WHERE
id = ?`
- res, err := db.Exec(query, r.Title, r.Portions, r.Url, r.Notes,
+ res, err := tx.Exec(query, r.Title, r.Portions, r.Url, r.Notes,
r.LastChanged, r.Id)
if err != nil {
+ rollback(tx)
return err
}
+
affected, err := res.RowsAffected()
if err != nil {
+ rollback(tx)
return err
}
if affected != 1 {
+ rollback(tx)
return fmt.Errorf("Recipe update affected %d rows instead of 1", affected)
}
- return nil
+ return tx.Commit()
}
func (r *Recipe) Delete() error {
- query := `DELETE FROM recipes WHERE id = ?`
+ tx, err := db.Begin()
+ if err != nil {
+ return err
+ }
+
+ err = r.DeleteSteps(tx)
+ if err != nil {
+ rollback(tx)
+ return err
+ }
+
+ query := `
+DELETE FROM
+ recipes
+WHERE
+ id = ?
+`
- result, err := db.Exec(query, r.Id)
+ result, err := tx.Exec(query, r.Id)
if err != nil {
+ rollback(tx)
return err
}
rows, err := result.RowsAffected()
if rows != 1 {
+ rollback(tx)
return errors.New("Recipe deletion did not affect exactly one row")
}
- return nil
+ return tx.Commit()
}
func RecipeTestData() []Recipe {
@@ -132,6 +194,11 @@ func RecipeTestData() []Recipe {
Notes: "Very fluffy",
Created: "",
LastChanged: "",
+ Steps: []Step{
+ {Text: "Stir the dough"},
+ {Text: "Heat up pan"},
+ {Text: "Make pancakes!"},
+ },
},
{
Id: "2",
@@ -141,6 +208,7 @@ func RecipeTestData() []Recipe {
Notes: "Delicious!",
Created: "",
LastChanged: "",
+ Steps: []Step{},
},
}
}