From 83b2f61e416bdc7dba3ae9b20361715859e81dde Mon Sep 17 00:00:00 2001 From: Vasiliy Vasilyuk Date: Sat, 17 Feb 2024 16:39:34 +0300 Subject: [PATCH] Add interface do DB driver to repository This abstraction is needed to add transactional cleanup in the future. --- user_repository.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/user_repository.go b/user_repository.go index dee3616..fd9c5bf 100644 --- a/user_repository.go +++ b/user_repository.go @@ -12,12 +12,17 @@ import ( "github.com/google/uuid" ) -func NewUserRepository(db *sql.DB) *UserRepository { +type DB interface { + QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row + ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error) +} + +func NewUserRepository(db DB) *UserRepository { return &UserRepository{db: db} } type UserRepository struct { - db *sql.DB + db DB } func (r *UserRepository) ReadUser(ctx context.Context, userID uuid.UUID) (User, error) {