diff options
-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 |