123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- package sp
- import (
- "github.com/dmitriygnatenko/internal/interfaces"
- "github.com/dmitriygnatenko/internal/repositories"
- cacheService "github.com/dmitriygnatenko/internal/services/cache"
- dbService "github.com/dmitriygnatenko/internal/services/db"
- envService "github.com/dmitriygnatenko/internal/services/env"
- mailService "github.com/dmitriygnatenko/internal/services/mailer"
- )
- type ServiceProvider struct {
- env interfaces.Env
- cache interfaces.Cache
- mailer interfaces.Mailer
- articleRepository interfaces.ArticleRepository
- tagRepository interfaces.TagRepository
- articleTagRepository interfaces.ArticleTagRepository
- }
- 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
- 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)
- 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) 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 InitMock(deps ...interface{}) interfaces.ServiceProvider {
- sp := ServiceProvider{}
- for _, d := range deps {
- switch s := d.(type) {
- case interfaces.Cache:
- sp.cache = 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
- }
- }
- return &sp
- }
|