diff --git a/Makefile b/Makefile index 826be4d..9df4b5a 100644 --- a/Makefile +++ b/Makefile @@ -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 \ No newline at end of file diff --git a/internal/parser/parser.go b/internal/parser/parser.go index 7d9e9b2..f6cab77 100644 --- a/internal/parser/parser.go +++ b/internal/parser/parser.go @@ -27,11 +27,13 @@ var PHPSESSID string var parseKey string var ( - KeyNotFoundErr = errors.New("key not found") - CookieNotFoundErr = errors.New("cookie not found") - NotSuccessReqErr = errors.New("not success request") - EmptyLinkErr = errors.New("empty link") - RecipeExistsErr = errors.New("recipe already exists") + KeyNotFoundErr = errors.New("key not found") + CookieNotFoundErr = errors.New("cookie not found") + 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 { diff --git a/internal/storage/postgresql/postgresql.go b/internal/storage/postgresql/postgresql.go index 6aeefc4..e1b8ec6 100644 --- a/internal/storage/postgresql/postgresql.go +++ b/internal/storage/postgresql/postgresql.go @@ -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) }