Skip to content

Conversation

@anon-d
Copy link
Owner

@anon-d anon-d commented Jan 20, 2026

Добавлена работа с БД. Устранены замечания

anon-d added 16 commits January 8, 2026 22:27
- feat(repository): added DB connection;
- feat(handler): added `ping` path to check DB connection;
- feat(repository): added methods Select and Insert;
- refactor(all): refactored project: splited repository layer, splited service layer;
- update(tests): updated handler tests;
- feat(handler): added urls batch handler;
- feat(repository): added transaction method for inserting urls batch;
- chore(tests): update tests;
- feat(handler): added error catching for unique url;
- feat(repository): added method for getting unique entry;
- chore(tests): update tests;
var dbService *serviceDB.DBService

// Try to connect to database if DSN is provided
if cfg.DSN != "" {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

См. мой ответ. Если ты завершишься с ошибкой при заданном cfg.DSN, то это не сломает тесты предыдущих спринтов

if errors.As(err, &conflictErr) {
// URL уже существует, возвращаем 409
c.Writer.WriteHeader(http.StatusConflict)
_ = json.NewEncoder(c.Writer).Encode(APIResponse{Result: shortURL})
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ошибку здесь можно залогировать для информации

var dbErr error
if s.DB != nil {
// Safely call DB.Insert with panic recovery
func() {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Паники лучше отлавливать при помощи middleware. Тогда хендлеры паник не придется регистрировать на каждом уровне

}
}

if s.Disk != nil {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Это очень сложный код. Почему бы не определить единый интерфейс для работы с БД и с файловым хранилищем, и не пользоваться им? Тогда для слоя сервисов будет безразлично, каким именно хранилищем он пользутеся. Это сильно упростит код этого слоя

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Это не надо пушить в репозиторий

- refactor(repository): define single interface for all storages;

- feat(middleware): added panic recovery middleware;
@anon-d anon-d merged commit d492842 into main Jan 24, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants