fix linter's warnings

This commit is contained in:
yash 2024-01-21 15:48:47 +03:00
parent 99a57be4c4
commit f94adb67dd
3 changed files with 41 additions and 13 deletions

View File

@ -6,3 +6,5 @@ migrate_up:
CONFIG_PATH=./config/local.yaml go run ./cmd/migrator/main.go --migrations-path ./migrations
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

View File

@ -32,6 +32,8 @@ var (
NotSuccessReqErr = errors.New("not success request")
EmptyLinkErr = errors.New("empty link")
RecipeExistsErr = errors.New("recipe already exists")
FailUpdatePHPSESSID = errors.New("failed to update PHPSESSID")
FailUpdateKEY = errors.New("failed to update KEY")
)
type pictureSaver interface {
@ -59,7 +61,10 @@ func SaveAllPages(log *slog.Logger, ps pictureSaver, rs recipeSaver, rp recipePr
for i := 2; i <= total; i++ {
log.Debug(fmt.Sprintf("Сохраняю страницу %d...\n", i))
_, err = SavePage(log, i, ps, rs, rp)
if err != nil {
log.Error("Страница не сохранена", "err", fmt.Errorf("%s: %w", op, err))
continue
}
log.Debug(fmt.Sprintf("Страница %d сохранена\n", i))
}
return nil
@ -86,10 +91,16 @@ func SavePage(log *slog.Logger, page int, ps pictureSaver, rs recipeSaver, rp re
if err != nil {
return 0, fmt.Errorf("%s: %w", op, err)
}
// update PHPSESSID and key
// update PHPSESSID and KEY
if resp.Message == "Could not load config" {
GetPHPSESSID(log)
GetKey(log)
err = GetPHPSESSID(log)
if err != nil {
return 0, fmt.Errorf("%s: %w", op, FailUpdatePHPSESSID)
}
err = GetKey(log)
if err != nil {
return 0, fmt.Errorf("%s: %w", op, FailUpdateKEY)
}
continue
}
if !resp.Success {

View File

@ -44,6 +44,9 @@ func (s *Storage) AddRecipe(ctx context.Context, recipe models.Recipe) error {
"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,
).Scan(&id)
if err != nil {
return fmt.Errorf("%s: %w", op, err)
}
// insert ingredients
for _, r := range recipe.Ingredients {
@ -71,29 +74,38 @@ func (s *Storage) AddRecipe(ctx context.Context, recipe models.Recipe) error {
// insert steps
for i, step := range recipe.Recipe_steps {
s.db.Exec(
_, err = s.db.Exec(
ctx,
"insert into recipe_steps (recipe_id, step_num, step_text) values ($1, $2, $3)",
id, i, step,
)
if err != nil {
return fmt.Errorf("%s: %w", op, err)
}
}
// insert advices
for _, a := range recipe.Advices {
s.db.Exec(
_, err = s.db.Exec(
ctx,
"insert into recipe_advices (recipe_id, advice) values ($1, $2)",
id, a,
)
if err != nil {
return fmt.Errorf("%s: %w", op, err)
}
}
// insert categories
for _, c := range recipe.Categories {
s.db.Exec(
_, err = s.db.Exec(
ctx,
"insert into recipe_categories (recipe_id, category) values ($1, $2)",
id, c,
)
if err != nil {
return fmt.Errorf("%s: %w", op, err)
}
}
// commit transaction
@ -233,7 +245,10 @@ func (s *Storage) GetRecipesByCategory(ctx context.Context, offset, limit int, c
return nil, fmt.Errorf("%s: %w", op, err)
}
s.AddRecipeInformation(ctx, &r)
err = s.AddRecipeInformation(ctx, &r)
if err != nil {
return nil, fmt.Errorf("%s: %w", op, err)
}
recipes = append(recipes, r)
}