summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxengineering <me@xengineering.eu>2024-11-03 15:49:43 +0100
committerxengineering <me@xengineering.eu>2024-11-03 16:05:55 +0100
commit7cd3a096a975801a07fb3ff06b6fac70e17234ce (patch)
tree36ce667817f6dc393dd2363e61ef9a6bafb424e1
parent7361801b45640ead9db82769434eb181c31e57e6 (diff)
downloadceres-7cd3a096a975801a07fb3ff06b6fac70e17234ce.tar
ceres-7cd3a096a975801a07fb3ff06b6fac70e17234ce.tar.zst
ceres-7cd3a096a975801a07fb3ff06b6fac70e17234ce.zip
model: Implement favorite recipes
This adds the ability to store a flag `is_favorite` inside the database. The corresponding SQL migration is part of this commit. Furthermore the Go code in the model package is adapted.
-rw-r--r--model/recipe.go17
-rw-r--r--model/recipes.go19
-rw-r--r--model/sql/migration002.sql30
3 files changed, 52 insertions, 14 deletions
diff --git a/model/recipe.go b/model/recipe.go
index fc2ae3a..684b158 100644
--- a/model/recipe.go
+++ b/model/recipe.go
@@ -16,6 +16,7 @@ type Recipe struct {
Created string `json:"created"`
LastChanged string `json:"last_changed"`
Steps []Step `json:"steps"`
+ IsFavorite bool `json:"is_favorite"`
}
func (r Recipe) String() string {
@@ -69,13 +70,13 @@ func (r *Recipe) Create(tx *sql.Tx) error {
cmd := `
INSERT INTO recipes
- (title, portions, url, notes, created, last_changed)
+ (title, portions, url, notes, created, last_changed, is_favorite)
VALUES
- (?, ?, ?, ?, ?, ?)
+ (?, ?, ?, ?, ?, ?, ?)
`
result, err := tx.Exec(cmd, r.Title, r.Portions, r.Url, r.Notes, r.Created,
- r.LastChanged)
+ r.LastChanged, r.IsFavorite)
if err != nil {
return err
}
@@ -136,7 +137,7 @@ ORDER BY
func (r *Recipe) Read(tx *sql.Tx) error {
cmd := `
SELECT
- title, portions, url, notes, created, last_changed
+ title, portions, url, notes, created, last_changed, is_favorite
FROM
recipes
WHERE
@@ -160,6 +161,7 @@ WHERE
&r.Notes,
&r.Created,
&r.LastChanged,
+ &r.IsFavorite,
)
if err != nil {
return err
@@ -216,12 +218,13 @@ SET
url = ?,
notes = ?,
created = ?,
- last_changed = ?
+ last_changed = ?,
+ is_favorite = ?
WHERE
id = ?`
res, err := tx.Exec(cmd, r.Title, r.Portions, r.Url, r.Notes, r.Created,
- r.LastChanged, r.Id)
+ r.LastChanged, r.IsFavorite, r.Id)
if err != nil {
return err
}
@@ -296,6 +299,7 @@ func RecipeTestData() []Recipe {
},
},
},
+ IsFavorite: true,
},
{
Title: "Burger",
@@ -305,6 +309,7 @@ func RecipeTestData() []Recipe {
Created: "1715658069",
LastChanged: "1715958070",
Steps: []Step{},
+ IsFavorite: false,
},
}
}
diff --git a/model/recipes.go b/model/recipes.go
index 324589a..e4fd1b8 100644
--- a/model/recipes.go
+++ b/model/recipes.go
@@ -7,8 +7,9 @@ import (
)
type RecipesElement struct {
- Id int64 // TODO change to string
- Title string
+ Id int64 // TODO change to string
+ Title string
+ IsFavorite bool
}
type Recipes []RecipesElement
@@ -22,7 +23,7 @@ func (r *Recipes) Read(tx *sql.Tx) error {
return errors.New("Recipes has to contain zero elements for .Read()")
}
- query := `SELECT id, title FROM recipes`
+ query := `SELECT id, title, is_favorite FROM recipes ORDER BY is_favorite DESC, created DESC`
rows, err := tx.Query(query)
if err != nil {
@@ -32,7 +33,7 @@ func (r *Recipes) Read(tx *sql.Tx) error {
for rows.Next() {
element := RecipesElement{}
- err = rows.Scan(&element.Id, &element.Title)
+ err = rows.Scan(&element.Id, &element.Title, &element.IsFavorite)
if err != nil {
return err
}
@@ -53,12 +54,14 @@ func (r *Recipes) Delete(tx *sql.Tx) error {
func RecipesTestData() Recipes {
return []RecipesElement{
{
- Id: 1,
- Title: "Pancakes",
+ Id: 1,
+ Title: "Pancakes",
+ IsFavorite: true,
},
{
- Id: 2,
- Title: "Burger",
+ Id: 2,
+ Title: "Burger",
+ IsFavorite: false,
},
}
}
diff --git a/model/sql/migration002.sql b/model/sql/migration002.sql
new file mode 100644
index 0000000..89837b3
--- /dev/null
+++ b/model/sql/migration002.sql
@@ -0,0 +1,30 @@
+-- Database schema 2 adds the ability to save recipes as favorites.
+
+CREATE TABLE recipes_new (
+ id INTEGER PRIMARY KEY,
+ title TEXT NOT NULL,
+ portions INTEGER NOT NULL,
+ url TEXT NOT NULL,
+ notes TEXT NOT NULL,
+ created INTEGER NOT NULL, -- Unix time stamp
+ last_changed INTEGER NOT NULL, -- Unix time stamp
+ is_favorite BOOLEAN NOT NULL DEFAULT FALSE
+);
+
+INSERT INTO
+ recipes_new
+ (id, title, portions, url, notes, created, last_changed, is_favorite)
+SELECT
+ id, title, portions, url, notes, created, last_changed, FALSE
+FROM recipes;
+
+DROP TABLE recipes;
+
+ALTER TABLE recipes_new RENAME TO recipes;
+
+UPDATE
+ metadata
+SET
+ value = '3'
+WHERE
+ key = 'schema_version';