From 86c1389df77af370a40628989c7917a7f6645e61 Mon Sep 17 00:00:00 2001 From: yash Date: Fri, 19 Jan 2024 22:00:03 +0300 Subject: [PATCH] db funcs --- cmd/parser/main.go | 1 + internal/parser/parser.go | 1 + internal/storage/postgresql/postgresql.go | 86 +++++++++++++++++++++-- 3 files changed, 82 insertions(+), 6 deletions(-) create mode 100644 cmd/parser/main.go create mode 100644 internal/parser/parser.go diff --git a/cmd/parser/main.go b/cmd/parser/main.go new file mode 100644 index 0000000..06ab7d0 --- /dev/null +++ b/cmd/parser/main.go @@ -0,0 +1 @@ +package main diff --git a/internal/parser/parser.go b/internal/parser/parser.go new file mode 100644 index 0000000..0bfe2c2 --- /dev/null +++ b/internal/parser/parser.go @@ -0,0 +1 @@ +package parser diff --git a/internal/storage/postgresql/postgresql.go b/internal/storage/postgresql/postgresql.go index 6a21ccc..2de69bc 100644 --- a/internal/storage/postgresql/postgresql.go +++ b/internal/storage/postgresql/postgresql.go @@ -122,7 +122,7 @@ func (s *Storage) RecipeExists(ctx context.Context, title string) (bool, error) return exists, nil } -// func (db *PostgresqlDatabase) GetRecipes(offset, limit int) ([]structs.Recipe, error) +// GetRecipes gets recipes by offset and limit. func (s *Storage) GetRecipes(ctx context.Context, offset, limit int) ([]models.Recipe, error) { const op = "storage.postgresql.GetRecipes" @@ -159,10 +159,84 @@ func (s *Storage) GetRecipes(ctx context.Context, offset, limit int) ([]models.R return recipes, nil } -// func (db *PostgresqlDatabase) GetRecipe(r_id uint) (structs.Recipe, error) +// GetRecipe gets recipe by id. +func (s *Storage) GetRecipe(ctx context.Context, r_id uint) (models.Recipe, error) { + const op = "storage.postgresql.GetRecipe" -// add to recipe info about ingredients, steps, advices, categories -// func (db *PostgresqlDatabase) AddRecipeInformation(r *structs.Recipe) + var recipe models.Recipe -// get recipes by category -// func (db *PostgresqlDatabase) GetRecipesByCategory(offset, limit int, category string) ([]structs.Recipe, error) + err := s.db.QueryRow( + ctx, + "select title, description, image, cooking_time, servings, cal from recipe where id = $1", + r_id, + ).Scan(&recipe) + + if err != nil { + return models.Recipe{}, fmt.Errorf("%s: %w", op, err) + } + + err = s.AddRecipeInformation(ctx, &recipe) + if err != nil { + return models.Recipe{}, fmt.Errorf("%s: %w", op, err) + } + + 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 + + // select steps + + // select advices + + // select categories + + return nil +} + +// GetRecipesByCategory gets recipes by category, offset and limit. +func (s *Storage) GetRecipesByCategory(ctx context.Context, offset, limit int, category string) ([]models.Recipe, error) { + const op = "storage.postgresql.GetRecipesByCategory" + + rows, err := s.db.Query( + ctx, + "select r.id, r.title, r.image, r.cooking_time, r.cal from recipe r inner join recipe_categories c on r.id = c.recipe_id where c.category = $1 order by r.id limit $2 offset $3", + category, limit, offset, + ) + if err != nil { + return nil, fmt.Errorf("%s: %w", op, err) + } + + defer rows.Close() + + var recipes []models.Recipe = make([]models.Recipe, 0, limit) + + for rows.Next() { + var r models.Recipe + + err = rows.Scan( + &r.ID, + &r.Title, + &r.Image, + &r.CookingTime, + &r.Calories, + ) + if err != nil { + return nil, fmt.Errorf("%s: %w", op, err) + } + + s.AddRecipeInformation(ctx, &r) + + recipes = append(recipes, r) + } + + return recipes, nil +}