add postgres.AddRecipeInformation
This commit is contained in:
parent
f94adb67dd
commit
58b8d016e8
2
Makefile
2
Makefile
|
@ -8,3 +8,5 @@ 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
|
||||
lint:
|
||||
golangci-lint run
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue