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 CONFIG_PATH=./config/local.yaml go run ./cmd/migrator/main.go --migrations-path ./migrations
migrate_down: migrate_down:
CONFIG_PATH=./config/local.yaml go run ./cmd/migrator/main.go --migrations-path ./migrations --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

@ -27,11 +27,13 @@ var PHPSESSID string
var parseKey string var parseKey string
var ( var (
KeyNotFoundErr = errors.New("key not found") KeyNotFoundErr = errors.New("key not found")
CookieNotFoundErr = errors.New("cookie not found") CookieNotFoundErr = errors.New("cookie not found")
NotSuccessReqErr = errors.New("not success request") NotSuccessReqErr = errors.New("not success request")
EmptyLinkErr = errors.New("empty link") EmptyLinkErr = errors.New("empty link")
RecipeExistsErr = errors.New("recipe already exists") RecipeExistsErr = errors.New("recipe already exists")
FailUpdatePHPSESSID = errors.New("failed to update PHPSESSID")
FailUpdateKEY = errors.New("failed to update KEY")
) )
type pictureSaver interface { type pictureSaver interface {
@ -59,7 +61,10 @@ func SaveAllPages(log *slog.Logger, ps pictureSaver, rs recipeSaver, rp recipePr
for i := 2; i <= total; i++ { for i := 2; i <= total; i++ {
log.Debug(fmt.Sprintf("Сохраняю страницу %d...\n", i)) log.Debug(fmt.Sprintf("Сохраняю страницу %d...\n", i))
_, err = SavePage(log, i, ps, rs, rp) _, 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)) log.Debug(fmt.Sprintf("Страница %d сохранена\n", i))
} }
return nil return nil
@ -86,10 +91,16 @@ func SavePage(log *slog.Logger, page int, ps pictureSaver, rs recipeSaver, rp re
if err != nil { if err != nil {
return 0, fmt.Errorf("%s: %w", op, err) return 0, fmt.Errorf("%s: %w", op, err)
} }
// update PHPSESSID and key // update PHPSESSID and KEY
if resp.Message == "Could not load config" { if resp.Message == "Could not load config" {
GetPHPSESSID(log) err = GetPHPSESSID(log)
GetKey(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 continue
} }
if !resp.Success { 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", "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, recipe.Title, recipe.Description, recipe.Image, recipe.CookingTime, recipe.ServingsNum, recipe.Calories,
).Scan(&id) ).Scan(&id)
if err != nil {
return fmt.Errorf("%s: %w", op, err)
}
// insert ingredients // insert ingredients
for _, r := range recipe.Ingredients { for _, r := range recipe.Ingredients {
@ -71,29 +74,38 @@ func (s *Storage) AddRecipe(ctx context.Context, recipe models.Recipe) error {
// insert steps // insert steps
for i, step := range recipe.Recipe_steps { for i, step := range recipe.Recipe_steps {
s.db.Exec( _, err = s.db.Exec(
ctx, ctx,
"insert into recipe_steps (recipe_id, step_num, step_text) values ($1, $2, $3)", "insert into recipe_steps (recipe_id, step_num, step_text) values ($1, $2, $3)",
id, i, step, id, i, step,
) )
if err != nil {
return fmt.Errorf("%s: %w", op, err)
}
} }
// insert advices // insert advices
for _, a := range recipe.Advices { for _, a := range recipe.Advices {
s.db.Exec( _, err = s.db.Exec(
ctx, ctx,
"insert into recipe_advices (recipe_id, advice) values ($1, $2)", "insert into recipe_advices (recipe_id, advice) values ($1, $2)",
id, a, id, a,
) )
if err != nil {
return fmt.Errorf("%s: %w", op, err)
}
} }
// insert categories // insert categories
for _, c := range recipe.Categories { for _, c := range recipe.Categories {
s.db.Exec( _, err = s.db.Exec(
ctx, ctx,
"insert into recipe_categories (recipe_id, category) values ($1, $2)", "insert into recipe_categories (recipe_id, category) values ($1, $2)",
id, c, id, c,
) )
if err != nil {
return fmt.Errorf("%s: %w", op, err)
}
} }
// commit transaction // 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) 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) recipes = append(recipes, r)
} }