db funcs
This commit is contained in:
parent
632d69ccda
commit
86c1389df7
|
@ -0,0 +1 @@
|
||||||
|
package main
|
|
@ -0,0 +1 @@
|
||||||
|
package parser
|
|
@ -122,7 +122,7 @@ func (s *Storage) RecipeExists(ctx context.Context, title string) (bool, error)
|
||||||
return exists, nil
|
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) {
|
func (s *Storage) GetRecipes(ctx context.Context, offset, limit int) ([]models.Recipe, error) {
|
||||||
const op = "storage.postgresql.GetRecipes"
|
const op = "storage.postgresql.GetRecipes"
|
||||||
|
|
||||||
|
@ -159,10 +159,84 @@ func (s *Storage) GetRecipes(ctx context.Context, offset, limit int) ([]models.R
|
||||||
return recipes, nil
|
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
|
var recipe models.Recipe
|
||||||
// func (db *PostgresqlDatabase) AddRecipeInformation(r *structs.Recipe)
|
|
||||||
|
|
||||||
// get recipes by category
|
err := s.db.QueryRow(
|
||||||
// func (db *PostgresqlDatabase) GetRecipesByCategory(offset, limit int, category string) ([]structs.Recipe, error)
|
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
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue