diff options
author | Tucker Evans <tuckerevans24@gmail.com> | 2019-11-10 11:28:08 -0500 |
---|---|---|
committer | Tucker Evans <tuckerevans24@gmail.com> | 2019-11-10 11:28:08 -0500 |
commit | 4f408d2b9521a2e0ff22495f6a6a6646e5a3135d (patch) | |
tree | 88d7f648db352fdb7ceb87641aad2427181d7762 /backend | |
parent | 04b3defc449973ec06f731b443acebe64cc64362 (diff) |
Fix MakeRecipeFromId
MakeRecipeFromId was creating a empty recipe because rows.Next() was not
being checked.
Diffstat (limited to 'backend')
-rw-r--r-- | backend/recipe.go | 97 |
1 files changed, 49 insertions, 48 deletions
diff --git a/backend/recipe.go b/backend/recipe.go index 1d6da90..98308d3 100644 --- a/backend/recipe.go +++ b/backend/recipe.go @@ -57,63 +57,64 @@ func RecipeFromId(id int, db *sql.DB) *Recipe { var title, photo_urls, keywords, desc string defer rows.Close() - rows.Next() - rows.Scan(&id, &title, &photo_urls, &keywords, &desc, &servings, &cook_time, &rating, &num_cooked) - rec = Recipe{ - Id: id, - Title: title, - Desc: desc, - Photos: make([]string, 0), - Serving_size: servings, - Cook_time: cook_time, - Rating: rating, - Keywords: make([]string, 0), - Ingredients: make([]Ingredient, 0), - Steps: make([]Step, 0)} - - if len(photo_urls) > 0 { - photos := strings.Split(photo_urls, "|") - for _, p := range photos { - rec.Photos = append(rec.Photos, p) + if rows.Next() { + rows.Scan(&id, &title, &photo_urls, &keywords, &desc, &servings, &cook_time, &rating, &num_cooked) + rec = Recipe{ + Id: id, + Title: title, + Desc: desc, + Photos: make([]string, 0), + Serving_size: servings, + Cook_time: cook_time, + Rating: rating, + Keywords: make([]string, 0), + Ingredients: make([]Ingredient, 0), + Steps: make([]Step, 0)} + + if len(photo_urls) > 0 { + photos := strings.Split(photo_urls, "|") + for _, p := range photos { + rec.Photos = append(rec.Photos, p) + } } - } - if len(keywords) > 0 { - keyword_split := strings.Split(keywords, "|") - for _, k := range keyword_split { - rec.Keywords = append(rec.Keywords, k) + if len(keywords) > 0 { + keyword_split := strings.Split(keywords, "|") + for _, k := range keyword_split { + rec.Keywords = append(rec.Keywords, k) + } } - } - var name, unit string - var amount float64 - rows, err = db.Query("SELECT name, amount, unit FROM ingredients WHERE recipe_id = $1", id) - if err == nil { - for rows.Next() { - rows.Scan(&name, &amount, &unit) - ingr = Ingredient{ - Name: name, - Amount: amount, - Unit: unit, + var name, unit string + var amount float64 + rows, err = db.Query("SELECT name, amount, unit FROM ingredients WHERE recipe_id = $1", id) + if err == nil { + for rows.Next() { + rows.Scan(&name, &amount, &unit) + ingr = Ingredient{ + Name: name, + Amount: amount, + Unit: unit, + } + rec.Ingredients = append(rec.Ingredients, ingr) } - rec.Ingredients = append(rec.Ingredients, ingr) } - } - var num, timer int - rows, err = db.Query("SELECT step, description, timer FROM steps WHERE recipe_id = $1", id) - if err == nil { - for rows.Next() { - rows.Scan(&num, &desc, &timer) - step = Step{ - Num: num, - Desc: desc, - Time: timer, + var num, timer int + rows, err = db.Query("SELECT step, description, timer FROM steps WHERE recipe_id = $1", id) + if err == nil { + for rows.Next() { + rows.Scan(&num, &desc, &timer) + step = Step{ + Num: num, + Desc: desc, + Time: timer, + } + rec.Steps = append(rec.Steps, step) } - rec.Steps = append(rec.Steps, step) } - } - return &rec + return &rec + } } return nil |