summaryrefslogtreecommitdiff
path: root/model/recipe.go
blob: c6aa63c8717938946edf14584efd75032402c6c0 (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
package model

type Recipe struct {
	Id       string  // FIXME has to be uint
	Title    string
	Portions string  // FIXME has to be uint
	URL      string
	Notes    string
	Steps    []RecipeStep
}

func (d *Recipe) FromDB() error {
	query, err := GetSql(`recipe-select`)
	if err != nil {
		return err
	}

	err = db.QueryRow(query, d.Id).Scan(
		&d.Id,
		&d.Title,
		&d.Portions,
		&d.URL,
		&d.Notes,
	)
	if err != nil {
		return err
	}

	query, err = GetSql(`steps-for-recipe`)
	if err != nil {
		return err
	}

	rows, err := db.Query(query, d.Id)
	if err != nil {
		return err
	}
	for rows.Next() {
		var step RecipeStep
		err = rows.Scan(&step.Text)
		if err != nil {
			return err
		}
		d.Steps = append(d.Steps, step)
	}

	return nil
}

func (d *Recipe) ToDB() error {
	query, err := GetSql(`recipe-update`)
	if err != nil {
		return err
	}

	_, err = db.Exec(query, d.Title, d.Portions, d.URL, d.Notes, d.Id)

	return err
}