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

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

type RecipesElement struct {
	Id         int64 // TODO change to string
	Title      string
	IsFavorite bool
}

type Recipes []RecipesElement

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, is_favorite FROM recipes ORDER BY is_favorite DESC, created DESC`

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

	for rows.Next() {
		element := RecipesElement{}
		err = rows.Scan(&element.Id, &element.Title, &element.IsFavorite)
		if err != nil {
			return err
		}
		*r = append(*r, element)
	}

	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{
		{
			Id:         1,
			Title:      "Pancakes",
			IsFavorite: true,
		},
		{
			Id:         2,
			Title:      "Burger",
			IsFavorite: false,
		},
	}
}