From 58b8d016e8703a79af4d29b74ca8d0ced6efb30c Mon Sep 17 00:00:00 2001 From: yash Date: Sun, 21 Jan 2024 17:10:21 +0300 Subject: [PATCH] add postgres.AddRecipeInformation --- Makefile | 4 +- internal/storage/postgresql/postgresql.go | 111 +++++++++++++++++++++- 2 files changed, 110 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index 9df4b5a..5055355 100644 --- a/Makefile +++ b/Makefile @@ -7,4 +7,6 @@ migrate_up: migrate_down: CONFIG_PATH=./config/local.yaml go run ./cmd/migrator/main.go --migrations-path ./migrations --down run_parser: - go run ./cmd/parser/main.go -config ./config/local.yaml \ No newline at end of file + go run ./cmd/parser/main.go -config ./config/local.yaml +lint: + golangci-lint run diff --git a/internal/storage/postgresql/postgresql.go b/internal/storage/postgresql/postgresql.go index e1b8ec6..0de4056 100644 --- a/internal/storage/postgresql/postgresql.go +++ b/internal/storage/postgresql/postgresql.go @@ -12,6 +12,10 @@ type Storage struct { db *pgxpool.Pool } +// TODO +// handling special errors +// AddRecipeInformation + func New(ctx context.Context, user, password, addr, dbname string) (*Storage, error) { const op = "storage.postgresql.New" @@ -195,21 +199,120 @@ func (s *Storage) GetRecipe(ctx context.Context, r_id uint) (models.Recipe, erro return recipe, nil } -// TODO -// handling special errors -// AddRecipeInformation - // AddRecipeInformation adds to recipe struct info about ingredients, steps, advices, categories. func (s *Storage) AddRecipeInformation(ctx context.Context, r *models.Recipe) error { const op = "storage.postgresql.AddRecipeInformation" // select ingredients + ingredientsg_rows, err := s.db.Query( + ctx, + "select id, title from recipe_ingredients_group where recipe_id = $1", + r.ID, + ) + if err != nil { + return fmt.Errorf("%s: %w", op, err) + } + + defer ingredientsg_rows.Close() + + for ingredientsg_rows.Next() { + var ingredients_group_id uint + var recipe_ingredients models.RecipeIngredients + + err = ingredientsg_rows.Scan( + &ingredients_group_id, + &recipe_ingredients.Title, + ) + if err != nil { + return fmt.Errorf("%s: %w", op, err) + } + + ingredient_rows, err := s.db.Query( + ctx, + "select ingredient from recipe_ingredients where recipe_ingredients_group_id = $1", + ingredients_group_id, + ) + if err != nil { + return fmt.Errorf("%s: %w", op, err) + } + + defer ingredient_rows.Close() + + for ingredient_rows.Next() { + var ingredient string + + err = ingredient_rows.Scan( + &ingredient, + ) + if err != nil { + return fmt.Errorf("%s: %w", op, err) + } + + recipe_ingredients.Ingredients = append(recipe_ingredients.Ingredients, ingredient) + } + + r.Ingredients = append(r.Ingredients, recipe_ingredients) + } // select steps + step_rows, err := s.db.Query(ctx, "select step_text from recipe_steps where recipe_id = $1 order by step_num", r.ID) + if err != nil { + return fmt.Errorf("%s: %w", op, err) + } + defer step_rows.Close() + + for step_rows.Next() { + var step string + + err = step_rows.Scan( + &step, + ) + if err != nil { + return fmt.Errorf("%s: %w", op, err) + } + + r.Recipe_steps = append(r.Recipe_steps, step) + } // select advices + advice_rows, err := s.db.Query(ctx, "select advice from recipe_advices where recipe_id = $1", r.ID) + if err != nil { + return fmt.Errorf("%s: %w", op, err) + } + defer advice_rows.Close() + + for advice_rows.Next() { + var advice string + + err = advice_rows.Scan( + &advice, + ) + if err != nil { + return fmt.Errorf("%s: %w", op, err) + } + + r.Advices = append(r.Advices, advice) + } // select categories + category_rows, err := s.db.Query(ctx, "select category from recipe_categories where recipe_id = $1", r.ID) + if err != nil { + return fmt.Errorf("%s: %w", op, err) + } + defer category_rows.Close() + + for category_rows.Next() { + var category string + + err = category_rows.Scan( + &category, + ) + if err != nil { + return fmt.Errorf("%s: %w", op, err) + } + + r.Categories = append(r.Categories, category) + } return nil }