diff options
author | xengineering <me@xengineering.eu> | 2024-04-22 21:50:26 +0200 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2024-05-09 12:07:48 +0200 |
commit | 0455da359b9fe36bd6808b7e506fe24e48257bea (patch) | |
tree | cc1bbb01652619d9caec299362ddac59c3ec326b | |
parent | fd7ee91110cf1de780f3c46822dd69385b3b6318 (diff) | |
download | ceres-0455da359b9fe36bd6808b7e506fe24e48257bea.tar ceres-0455da359b9fe36bd6808b7e506fe24e48257bea.tar.zst ceres-0455da359b9fe36bd6808b7e506fe24e48257bea.zip |
model: Enforce recipe titles
If a recipe has no title it is hard to reference in the front end.
Especially the /recipes page makes problems in that case since it is
impossible to click on that recipes and thus also to remove it.
-rw-r--r-- | model/recipe.go | 20 |
1 files changed, 19 insertions, 1 deletions
diff --git a/model/recipe.go b/model/recipe.go index c4f0d47..d8c0f2f 100644 --- a/model/recipe.go +++ b/model/recipe.go @@ -23,11 +23,24 @@ func (r Recipe) String() string { return string(b) } +func (r *Recipe) Validate() error { + if r.Title == "" { + return fmt.Errorf("Recipes must have a title") + } + + return nil +} + func (r *Recipe) Create(tx *sql.Tx) error { if r.Id != "" { return fmt.Errorf("Cannot create recipe if ID is given") } + err := r.Validate() + if err != nil { + return err + } + cmd := ` INSERT INTO recipes (title, portions, url, notes, created, last_changed) @@ -138,10 +151,15 @@ WHERE } } - return nil + return r.Validate() } func (r *Recipe) Update(tx *sql.Tx) error { + err := r.Validate() + if err != nil { + return err + } + oldSteps, err := r.getStepIds(tx) if err != nil { return err |