diff options
| author | xengineering <me@xengineering.eu> | 2024-02-11 21:07:05 +0100 | 
|---|---|---|
| committer | xengineering <me@xengineering.eu> | 2024-02-11 21:08:32 +0100 | 
| commit | 43fdfde44bce659abd30186150f667d8ba24cf2b (patch) | |
| tree | b0db4fcc5e7660c6f12e4e186c179a5f7568f836 /model/recipes.go | |
| parent | daa2934451ddb381a6a8dd8f902aacb9095200e0 (diff) | |
| download | ceres-43fdfde44bce659abd30186150f667d8ba24cf2b.tar ceres-43fdfde44bce659abd30186150f667d8ba24cf2b.tar.zst ceres-43fdfde44bce659abd30186150f667d8ba24cf2b.zip | |
model: Add type 'Recipes' with Read() method
This type is provided to render overview pages easily with a list of all
recipes.
Diffstat (limited to 'model/recipes.go')
| -rw-r--r-- | model/recipes.go | 37 | 
1 files changed, 37 insertions, 0 deletions
| diff --git a/model/recipes.go b/model/recipes.go new file mode 100644 index 0000000..bdd1cc1 --- /dev/null +++ b/model/recipes.go @@ -0,0 +1,37 @@ +package model + +import ( +	"errors" +) + +type RecipesElement struct { +	Id          int64 +	Title       string +} + +type Recipes []RecipesElement + +func (r *Recipes) Read() error { +	if len(*r) != 0 { +		return errors.New("Recipes has to contain zero elements for .Read()") +	} + +	query := `SELECT id, title FROM recipes` + +	rows, err := db.Query(query) +	if err != nil { +		return err +	} +	defer rows.Close() + +	for rows.Next() { +		element := RecipesElement{} +		err = rows.Scan(&element.Id, &element.Title) +		if err != nil { +			return err +		} +		*r = append(*r, element) +	} + +	return nil +} | 
