Refactor postgres base repository with error constants extraction - #9
Merged
Conversation
…ion support Key Changes: 1. Common Base Repository Structure: - Created `internal/repository/postgres/base_repository.go` with shared functionality for all repositories - Implemented `BaseRepository` struct with common methods: `WithContext()`, `SetTransaction()`, `ClearTransaction()`, `Exec()`, `QueryRow()`, and `Query()` - Refactored both `PostgresNoteRepository` and `PostgresUserRepository` to embed the base repository - Centralized context management with configurable timeouts 2. Enhanced Error Handling System: - Extended `internal/repository/postgres/errors/errors.go` with structured error hierarchy - Added `ErrorCode` type and `RepositoryError` struct with error codes, messages, and entity information - Implemented specific error creation functions (e.g., `NewNoteNotFoundError`, `NewUserCreationError`) - Maintained backward compatibility by preserving original error variables 3. Repository Options Pattern: - Created `internal/repository/postgres/repository_options.go` with `RepositoryOption` type - Added `WithTimeout` option for configurable timeouts - Implemented `NewPostgresNoteRepositoryWithOpts` and `NewPostgresUserRepositoryWithOpts` for advanced configuration - Preserved original constructors for backward compatibility 4. Transaction Support: - Added `internal/repository/postgres/transaction_manager.go` with `TransactionManager` interface - Implemented `PostgresTransactionManager` with `WithinTransaction` method - Enhanced base repository to support transaction operations - Added methods to set and clear transactions on repository instances 5. Repository Refactoring: - Updated `note_repository.go` and `user_repository.go` to use base repository methods - Replaced direct database calls with base repository methods (`Exec`, `QueryRow`, `Query`) - Added proper error wrapping using enhanced error system - Implemented proper PostgreSQL error handling (e.g., checking for `pgx.ErrNoRows`) 6. Backward Compatibility: - All existing interfaces remain unchanged - Original constructor signatures preserved (`NewPostgresNoteRepository`, `NewPostgresUserRepository`) - All existing tests continue to pass without modification - Applications (web-server, grpc-server) continue to work without changes - Benchmark tests remain functional
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR includes several improvements to the database layer: