1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
package model
import (
"database/sql"
"encoding/json"
"errors"
"fmt"
)
type Recipe struct {
Id string `json:"id"`
Title string `json:"title"`
Portions string `json:"portions"`
Url string `json:"url"`
Notes string `json:"notes"`
Created string `json:"created"`
LastChanged string `json:"last_changed"`
Steps []Step `json:"steps"`
}
func (r Recipe) String() string {
b, _ := json.MarshalIndent(r, "", " ")
return string(b)
}
func (r *Recipe) Create(tx *sql.Tx) error {
cmd := `
INSERT INTO recipes
(title, portions, url, notes, created, last_changed)
VALUES
(?, ?, ?, ?, ?, ?)
`
result, err := tx.Exec(cmd, r.Title, r.Portions, r.Url, r.Notes, r.Created,
r.LastChanged)
if err != nil {
return err
}
id, err := result.LastInsertId()
if err != nil {
return err
}
r.Id = fmt.Sprint(id)
err = r.CreateSteps(tx)
if err != nil {
return err
}
return nil
}
func (r *Recipe) Read(tx *sql.Tx) error {
cmd := `
SELECT id, title, portions, url, notes, created, last_changed
FROM recipes
WHERE id = ?
`
rows, err := tx.Query(cmd, r.Id)
if err != nil {
return err
}
defer rows.Close()
if !rows.Next() {
return sql.ErrNoRows
}
err = rows.Scan(
&r.Id,
&r.Title,
&r.Portions,
&r.Url,
&r.Notes,
&r.Created,
&r.LastChanged,
)
if err != nil {
return err
}
err = r.ReadSteps(tx)
if err != nil {
return err
}
return nil
}
func (r *Recipe) Update(tx *sql.Tx) error {
err := r.UpdateSteps(tx)
if err != nil {
return err
}
query := `UPDATE
recipes
SET
title = ?,
portions = ?,
url = ?,
notes = ?,
last_changed = ?
WHERE
id = ?`
res, err := tx.Exec(query, r.Title, r.Portions, r.Url, r.Notes,
r.LastChanged, r.Id)
if err != nil {
return err
}
affected, err := res.RowsAffected()
if err != nil {
return err
}
if affected != 1 {
return fmt.Errorf("Recipe update affected %d rows instead of 1", affected)
}
return nil
}
func (r *Recipe) Delete(tx *sql.Tx) error {
err := r.DeleteSteps(tx)
if err != nil {
return err
}
query := `
DELETE FROM
recipes
WHERE
id = ?
`
result, err := tx.Exec(query, r.Id)
if err != nil {
return err
}
rows, err := result.RowsAffected()
if rows != 1 {
return errors.New("Recipe deletion did not affect exactly one row")
}
return nil
}
func RecipeTestData() []Recipe {
return []Recipe{
{
Id: "1",
Title: "Pancakes",
Portions: "4",
Url: "https://example.org",
Notes: "Very fluffy",
Created: "",
LastChanged: "",
Steps: []Step{
{Text: "Stir the dough"},
{Text: "Heat up pan"},
{Text: "Make pancakes!"},
},
},
{
Id: "2",
Title: "Burger",
Portions: "2",
Url: "https://xengineering.eu/git/ceres",
Notes: "Delicious!",
Created: "",
LastChanged: "",
Steps: []Step{},
},
}
}
|