summaryrefslogtreecommitdiff
path: root/model/step.go
blob: b996e3a24a1778209e9693f03d0e397f833cfa2d (plain)
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package model

import (
	"database/sql"
	"encoding/json"
	"errors"
	"fmt"
)

type Step struct {
	Id          string       `json:"id"`
	Index       string       `json:"index"`
	Text        string       `json:"text"`
	Recipe      string       `json:"recipe"`
	Ingredients []Ingredient `json:"ingredients"`
}

func (s Step) String() string {
	b, _ := json.MarshalIndent(s, "", "    ")
	return string(b)
}

func (s *Step) Validate() error {
	var err error

	if s.Id != "" {
		err = isPositiveOrZeroInt(s.Id)
		if err != nil {
			return fmt.Errorf("Invalid step ID: %w", err)
		}
	}

	if s.Index != "" {
		err = isPositiveOrZeroInt(s.Index)
		if err != nil {
			return fmt.Errorf("Invalid step index: %w", err)
		}
	}

	if s.Text == "" {
		return fmt.Errorf("Step text must not be empty")
	}

	err = isPositiveOrZeroInt(s.Recipe)
	if err != nil {
		return fmt.Errorf("Step does not reference a valid recipe ID: %w", err)
	}

	return nil
}

func (s *Step) Create(tx *sql.Tx) error {
	if s.Id != "" {
		return fmt.Errorf("Cannot create step if ID is given")
	}

	err := s.Validate()
	if err != nil {
		return err
	}

	cmd := `
INSERT INTO steps
    ('index', text, recipe)
VALUES
    (?, ?, ?)
`

	result, err := tx.Exec(cmd, s.Index, s.Text, s.Recipe)
	if err != nil {
		return err
	}

	id, err := result.LastInsertId()
	if err != nil {
		return err
	}

	s.Id = fmt.Sprint(id)

	for i := range s.Ingredients {
		s.Ingredients[i].Step = s.Id
		s.Ingredients[i].Index = fmt.Sprint(i)
		err = s.Ingredients[i].Create(tx)
		if err != nil {
			return err
		}
	}

	return nil
}

func (s *Step) getIngredientIds(tx *sql.Tx) ([]Ingredient, error) {
	retval := make([]Ingredient, 0)

	cmd := `
SELECT
    id
FROM
    ingredients
WHERE
    step = ?
ORDER BY
    'index' ASC
`

	rows, err := tx.Query(cmd, s.Id)
	if err != nil {
		return retval, err
	}
	defer rows.Close()

	for rows.Next() {
		i := Ingredient{}

		err = rows.Scan(&i.Id)
		if err != nil {
			return retval, err
		}

		retval = append(retval, i)
	}

	return retval, nil
}

func (s *Step) Read(tx *sql.Tx) error {
	cmd := `
SELECT
    "index", text, recipe
FROM
    steps
WHERE
    id = ?
`

	rows, err := tx.Query(cmd, s.Id)
	if err != nil {
		return err
	}
	defer rows.Close()

	if !rows.Next() {
		return sql.ErrNoRows
	}

	err = rows.Scan(&s.Index, &s.Text, &s.Recipe)
	if err != nil {
		return err
	}

	s.Ingredients, err = s.getIngredientIds(tx)
	if err != nil {
		return err
	}

	for i := range s.Ingredients {
		err = s.Ingredients[i].Read(tx)
		if err != nil {
			return err
		}
	}

	return s.Validate()
}

func (s *Step) Update(tx *sql.Tx) error {
	err := s.Validate()
	if err != nil {
		return err
	}

	oldIngredients, err := s.getIngredientIds(tx)
	if err != nil {
		return err
	}

	for i := range oldIngredients {
		err = oldIngredients[i].Delete(tx)
		if err != nil {
			return err
		}
	}

	for i := range s.Ingredients {
		s.Ingredients[i].Index = fmt.Sprint(i)
		s.Ingredients[i].Step = s.Id
		err = s.Ingredients[i].Create(tx)
		if err != nil {
			return err
		}
	}

	cmd := `
UPDATE
    steps
SET
    index = ?,
    text = ?,
    recipe = ?
WHERE
    id = ?`

	res, err := tx.Exec(cmd, s.Index, s.Text, s.Recipe, s.Id)
	if err != nil {
		return err
	}

	affected, err := res.RowsAffected()
	if err != nil {
		return err
	}
	if affected != 1 {
		return fmt.Errorf("Step update affected %d rows instead of 1", affected)
	}

	return nil
}

func (s *Step) Delete(tx *sql.Tx) error {
	oldIngredients, err := s.getIngredientIds(tx)
	if err != nil {
		return err
	}

	for i := range oldIngredients {
		err = oldIngredients[i].Delete(tx)
		if err != nil {
			return err
		}
	}

	cmd := `
DELETE FROM
    steps
WHERE
    id = ?
`

	result, err := tx.Exec(cmd, s.Id)
	if err != nil {
		return err
	}

	rows, err := result.RowsAffected()
	if rows != 1 {
		return errors.New("Step deletion did not affect exactly one row")
	}

	return nil
}