summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTucker Evans <tuckerevans24@gmail.com>2019-11-12 09:25:05 -0500
committerTucker Evans <tuckerevans24@gmail.com>2019-11-12 09:26:42 -0500
commit16e187899c1cd008a5d36d4ac772a7bbc854e165 (patch)
tree9dd2e483c7130f0591242e5641c7bca08493a1d3
parentb84c2d1ec6acb70f76a03010b1fdc868cf3d485e (diff)
Fix change QueryRow to Exec for ingredient/step inserts
Changed database schema so ingredients & steps does not have an id, and therefore can't return it so instead we just exec the insert statement without returning anything. We do have to check that there were affected rows in order to accurately update the recipe object.
-rw-r--r--backend/main.go2
-rw-r--r--backend/recipe.go27
2 files changed, 13 insertions, 16 deletions
diff --git a/backend/main.go b/backend/main.go
index b4cd76f..856cd80 100644
--- a/backend/main.go
+++ b/backend/main.go
@@ -173,7 +173,7 @@ func SingleRecipe(w http.ResponseWriter, r *http.Request) {
var status int
var msg string
- if res.RowsAffected() == 0 {
+ if ra, _ := res.RowsAffected(); ra == 0 {
status = http.StatusNotFound
msg = "Recipe Not found"
} else {
diff --git a/backend/recipe.go b/backend/recipe.go
index e14c79b..fc34ba7 100644
--- a/backend/recipe.go
+++ b/backend/recipe.go
@@ -158,42 +158,39 @@ func AddRecipeDB(r *Recipe, db *sql.DB) error {
r.Rating, //7
r.Num_cooked, //8
).Scan(&id)
-
if err != nil {
return err
}
- var ingr_id int
for i, ingr := range r.Ingredients {
- err := db.QueryRow(`INSERT INTO ingredients
+ res, err := db.Exec(`INSERT INTO ingredients
(name, amount, unit, recipe_id)
- VALUES ($1, $2, $3, $4)
- RETURNING id`,
+ VALUES ($1, $2, $3, $4)`,
ingr.Name,
ingr.Amount,
ingr.Unit,
id,
- ).Scan(&ingr_id)
- if err != nil {
+ )
+
+ if ra, _ := res.RowsAffected(); ra == 0 || err != nil {
r.Ingredients = append(r.Ingredients[:i],
r.Ingredients[i+1:]...)
}
}
- var step_id int
for i, step := range r.Steps {
- err := db.QueryRow(`INSERT INTO steps
+ res, err := db.Exec(`INSERT INTO steps
(step, description, timer, recipe_id)
- VALUES ($1, $2, $3, $4)
- RETURNING id`,
+ VALUES ($1, $2, $3, $4)`,
step.Num,
step.Desc,
step.Time,
id,
- ).Scan(&step_id)
- if err != nil {
- r.Steps = append(r.Steps[:i],
- r.Steps[i+1:]...)
+ )
+
+ if ra, _ := res.RowsAffected(); ra == 0 || err != nil {
+ r.Ingredients = append(r.Ingredients[:i],
+ r.Ingredients[i+1:]...)
}
}