From a28beb0e9ccefa56b12782b4f4bc816e07cad525 Mon Sep 17 00:00:00 2001 From: yash Date: Mon, 22 Jan 2024 20:07:02 +0300 Subject: [PATCH] fix transaction --- internal/storage/postgresql/postgresql.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/internal/storage/postgresql/postgresql.go b/internal/storage/postgresql/postgresql.go index a0fff0e..87e1d8c 100644 --- a/internal/storage/postgresql/postgresql.go +++ b/internal/storage/postgresql/postgresql.go @@ -42,7 +42,7 @@ func (s *Storage) AddRecipe(ctx context.Context, recipe models.Recipe) error { var id uint // insert recipe - err = s.db.QueryRow( + err = tx.QueryRow( ctx, "insert into recipe (title, description, image, cooking_time, servings, cal) values ($1, $2, $3, $4, $5, $6) returning id", recipe.Title, recipe.Description, recipe.Image, recipe.CookingTime, recipe.ServingsNum, recipe.Calories, @@ -54,7 +54,7 @@ func (s *Storage) AddRecipe(ctx context.Context, recipe models.Recipe) error { // insert ingredients for _, r := range recipe.Ingredients { var ing_id uint - err = s.db.QueryRow( + err = tx.QueryRow( ctx, "insert into recipe_ingredients_group (recipe_id, title) values ($1, $2) returning id", id, r.Title, @@ -64,7 +64,7 @@ func (s *Storage) AddRecipe(ctx context.Context, recipe models.Recipe) error { } for _, i := range r.Ingredients { - _, err = s.db.Exec( + _, err = tx.Exec( ctx, "insert into recipe_ingredients (recipe_ingredients_group_id, ingredient) values ($1, $2)", ing_id, i, @@ -77,7 +77,7 @@ func (s *Storage) AddRecipe(ctx context.Context, recipe models.Recipe) error { // insert steps for i, step := range recipe.Recipe_steps { - _, err = s.db.Exec( + _, err = tx.Exec( ctx, "insert into recipe_steps (recipe_id, step_num, step_text) values ($1, $2, $3)", id, i, step, @@ -89,7 +89,7 @@ func (s *Storage) AddRecipe(ctx context.Context, recipe models.Recipe) error { // insert advices for _, a := range recipe.Advices { - _, err = s.db.Exec( + _, err = tx.Exec( ctx, "insert into recipe_advices (recipe_id, advice) values ($1, $2)", id, a, @@ -101,7 +101,7 @@ func (s *Storage) AddRecipe(ctx context.Context, recipe models.Recipe) error { // insert categories for _, c := range recipe.Categories { - _, err = s.db.Exec( + _, err = tx.Exec( ctx, "insert into recipe_categories (recipe_id, category) values ($1, $2)", id, c,