block-accounting/backend/internal/factory/repositories.go

51 lines
1.3 KiB
Go
Raw Normal View History

2024-05-24 17:44:24 +00:00
package factory
import (
"database/sql"
"log/slog"
"github.com/emochka2007/block-accounting/internal/pkg/config"
"github.com/emochka2007/block-accounting/internal/usecase/repository/auth"
"github.com/emochka2007/block-accounting/internal/usecase/repository/cache"
"github.com/emochka2007/block-accounting/internal/usecase/repository/organizations"
"github.com/emochka2007/block-accounting/internal/usecase/repository/transactions"
"github.com/emochka2007/block-accounting/internal/usecase/repository/users"
"github.com/redis/go-redis/v9"
)
func provideUsersRepository(db *sql.DB) users.Repository {
return users.NewRepository(db)
}
func provideOrganizationsRepository(
db *sql.DB,
uRepo users.Repository,
) organizations.Repository {
return organizations.NewRepository(db, uRepo)
}
func provideTxRepository(db *sql.DB) transactions.Repository {
return transactions.NewRepository(db)
}
func provideAuthRepository(db *sql.DB) auth.Repository {
return auth.NewRepository(db)
}
func provideRedisConnection(c config.Config) (*redis.Client, func()) {
r := redis.NewClient(&redis.Options{
Addr: c.DB.CacheHost,
Username: c.DB.CacheUser,
Password: c.DB.CacheSecret,
})
return r, func() { r.Close() }
}
func provideRedisCache(c *redis.Client, log *slog.Logger) cache.Cache {
return cache.NewRedisCache(
log.WithGroup("redis-cache"),
c,
)
}