1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- 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.IEnv
- cache interfaces.ICache
- mailer interfaces.IMailer
- articleRepository interfaces.IArticleRepository
- tagRepository interfaces.ITagRepository
- articleTagRepository interfaces.IArticleTagRepository
- }
- func Init() (interfaces.IServiceProvider, error) {
- sp := &ServiceProvider{}
- // Init env service
- env, err := envService.Init()
- if err != nil {
- return nil, err
- }
- sp.env = env
- 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)
- // Init cache service
- sp.cache = cacheService.Init()
- // Init mailer
- sp.mailer = mailService.Init(sp.env)
- return sp, nil
- }
- func (sp *ServiceProvider) GetEnvService() interfaces.IEnv {
- return sp.env
- }
- func (sp *ServiceProvider) GetCacheService() interfaces.ICache {
- return sp.cache
- }
- func (sp *ServiceProvider) GetMailerService() interfaces.IMailer {
- return sp.mailer
- }
- func (sp *ServiceProvider) GetArticleRepository() interfaces.IArticleRepository {
- return sp.articleRepository
- }
- func (sp *ServiceProvider) GetTagRepository() interfaces.ITagRepository {
- return sp.tagRepository
- }
- func (sp *ServiceProvider) GetArticleTagRepository() interfaces.IArticleTagRepository {
- return sp.articleTagRepository
- }
- func InitMock(deps ...interface{}) interfaces.IServiceProvider {
- sp := ServiceProvider{}
- for _, d := range deps {
- switch s := d.(type) {
- case interfaces.ICache:
- sp.cache = s
- case interfaces.IEnv:
- sp.env = s
- case interfaces.IMailer:
- sp.mailer = s
- case interfaces.IArticleRepository:
- sp.articleRepository = s
- case interfaces.IArticleTagRepository:
- sp.articleTagRepository = s
- case interfaces.ITagRepository:
- sp.tagRepository = s
- }
- }
- return &sp
- }
|