123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- package sp
- import (
- "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/interfaces"
- "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/repositories"
- authService "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/services/auth"
- cacheService "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/services/cache"
- dbService "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/services/db"
- envService "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/services/env"
- mailService "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/services/mailer"
- )
- type ServiceProvider struct {
- env interfaces.Env
- cache interfaces.Cache
- mailer interfaces.Mailer
- auth interfaces.Auth
- articleRepository interfaces.ArticleRepository
- tagRepository interfaces.TagRepository
- articleTagRepository interfaces.ArticleTagRepository
- userRepository interfaces.UserRepository
- }
- func Init() (interfaces.ServiceProvider, error) {
- sp := &ServiceProvider{}
- // Init services
- env, err := envService.Init()
- if err != nil {
- return nil, err
- }
- sp.env = env
- cache, err := cacheService.Init()
- if err != nil {
- return nil, err
- }
- sp.cache = cache
- mailer, err := mailService.Init(sp.env)
- if err != nil {
- return nil, err
- }
- sp.mailer = mailer
- auth, err := authService.Init(sp.env)
- if err != nil {
- return nil, err
- }
- sp.auth = auth
- db, err := dbService.Init(env)
- if err != nil {
- return nil, err
- }
- // Init repositories
- sp.articleRepository = repositories.InitArticleRepository(db)
- sp.tagRepository = repositories.InitTagRepository(db)
- sp.articleTagRepository = repositories.InitArticleTagRepository(db)
- sp.userRepository = repositories.InitUserRepository(db)
- return sp, nil
- }
- func (sp *ServiceProvider) GetEnvService() interfaces.Env {
- return sp.env
- }
- func (sp *ServiceProvider) GetCacheService() interfaces.Cache {
- return sp.cache
- }
- func (sp *ServiceProvider) GetMailerService() interfaces.Mailer {
- return sp.mailer
- }
- func (sp *ServiceProvider) GetAuthService() interfaces.Auth {
- return sp.auth
- }
- func (sp *ServiceProvider) GetArticleRepository() interfaces.ArticleRepository {
- return sp.articleRepository
- }
- func (sp *ServiceProvider) GetTagRepository() interfaces.TagRepository {
- return sp.tagRepository
- }
- func (sp *ServiceProvider) GetArticleTagRepository() interfaces.ArticleTagRepository {
- return sp.articleTagRepository
- }
- func (sp *ServiceProvider) GetUserRepository() interfaces.UserRepository {
- return sp.userRepository
- }
- func InitMock(deps ...interface{}) interfaces.ServiceProvider {
- sp := ServiceProvider{}
- for _, d := range deps {
- switch s := d.(type) {
- case interfaces.Cache:
- sp.cache = s
- case interfaces.Auth:
- sp.auth = s
- case interfaces.Env:
- sp.env = s
- case interfaces.Mailer:
- sp.mailer = s
- case interfaces.ArticleRepository:
- sp.articleRepository = s
- case interfaces.ArticleTagRepository:
- sp.articleTagRepository = s
- case interfaces.TagRepository:
- sp.tagRepository = s
- case interfaces.UserRepository:
- sp.userRepository = s
- }
- }
- return &sp
- }
|