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, }, } }