diff options
author | Tucker Evans <tuckerevans24@gmail.com> | 2019-11-10 13:41:59 -0500 |
---|---|---|
committer | Tucker Evans <tuckerevans24@gmail.com> | 2019-11-10 13:41:59 -0500 |
commit | 25e9d6a91903b45d6a62a152e230bfdbc6a41af0 (patch) | |
tree | e879f39881b9fb2d16c7b62a6c3240c44a305ca1 | |
parent | 2efe3ce53cebd373bf56ba7580bb63df16871bac (diff) |
Fix RecipeFromId expects only one row from database
Now uses sql.QueryRow rather than sql.Query so it doesnt have to check
row.Next(), just have to check that row was returned (on scan call)
-rw-r--r-- | backend/recipe.go | 127 |
1 files changed, 67 insertions, 60 deletions
diff --git a/backend/recipe.go b/backend/recipe.go index 98308d3..ceae402 100644 --- a/backend/recipe.go +++ b/backend/recipe.go @@ -51,71 +51,78 @@ func RecipeFromId(id int, db *sql.DB) *Recipe { var ingr Ingredient var step Step - rows, err := db.Query("SELECT id, title, photo_urls, keywords, description, serving_size, cook_time, rating, num_cooked FROM recipes WHERE id = $1", id) - if err == nil { - var id, rating, num_cooked, servings, cook_time int - var title, photo_urls, keywords, desc string - - defer rows.Close() - 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) - } - } + rows := db.QueryRow(`SELECT title, photo_urls, keywords, + description, serving_size, cook_time, rating, + num_cooked FROM recipes WHERE id = $1`, id) - 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) - } - } + var rating, num_cooked, servings, cook_time int + var title, photo_urls, keywords, desc string + + err := rows.Scan(&title, &photo_urls, &keywords, &desc, + &servings, &cook_time, &rating, &num_cooked) + + if err == sql.ErrNoRows { + return nil + } + + 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)} - 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) - } + 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) + } + } + + var name, unit string + var amount float64 + rows_ingr, err := db.Query(`SELECT name, amount, unit + FROM ingredients WHERE recipe_id = $1`, + id) + defer rows_ingr.Close() + if err == nil { + for rows_ingr.Next() { + rows_ingr.Scan(&name, &amount, &unit) + ingr = Ingredient{ + Name: name, + Amount: amount, + Unit: unit, } + rec.Ingredients = append(rec.Ingredients, ingr) + } + } - return &rec + var num, timer int + rows_steps, err := db.Query(`SELECT step, description, timer + FROM steps WHERE recipe_id = $1`, id) + defer rows_steps.Close() + if err == nil { + for rows_steps.Next() { + rows_steps.Scan(&num, &desc, &timer) + step = Step{ + Num: num, + Desc: desc, + Time: timer, + } + rec.Steps = append(rec.Steps, step) } } - return nil + return &rec } |