sp.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package sp
  2. import (
  3. "git.dmitriygnatenko.ru/dima/homethings/internal/interfaces"
  4. "git.dmitriygnatenko.ru/dima/homethings/internal/repositories"
  5. authService "git.dmitriygnatenko.ru/dima/homethings/internal/services/auth"
  6. dbService "git.dmitriygnatenko.ru/dima/homethings/internal/services/db"
  7. envService "git.dmitriygnatenko.ru/dima/homethings/internal/services/env"
  8. mailerService "git.dmitriygnatenko.ru/dima/homethings/internal/services/mailer"
  9. )
  10. type ServiceProvider struct {
  11. env interfaces.Env
  12. auth interfaces.Auth
  13. mailer interfaces.Mailer
  14. placeRepository interfaces.PlaceRepository
  15. thingRepository interfaces.ThingRepository
  16. tagRepository interfaces.TagRepository
  17. placeThingRepository interfaces.PlaceThingRepository
  18. placeImageRepository interfaces.PlaceImageRepository
  19. thingImageRepository interfaces.ThingImageRepository
  20. thingTagRepository interfaces.ThingTagRepository
  21. userRepository interfaces.UserRepository
  22. thingNotificationRepository interfaces.ThingNotificationRepository
  23. fileRepository interfaces.FileRepository
  24. }
  25. func Init() (interfaces.ServiceProvider, error) {
  26. sp := &ServiceProvider{}
  27. env, err := envService.Init()
  28. if err != nil {
  29. return nil, err
  30. }
  31. sp.env = env
  32. auth, err := authService.Init(env)
  33. if err != nil {
  34. return nil, err
  35. }
  36. sp.auth = auth
  37. mailer, err := mailerService.Init(env)
  38. if err != nil {
  39. return nil, err
  40. }
  41. sp.mailer = mailer
  42. db, err := dbService.Init(env)
  43. if err != nil {
  44. return nil, err
  45. }
  46. // Init repositories
  47. sp.placeRepository = repositories.InitPlaceRepository(db)
  48. sp.thingRepository = repositories.InitThingRepository(db)
  49. sp.placeThingRepository = repositories.InitPlaceThingRepository(db)
  50. sp.placeImageRepository = repositories.InitPlaceImageRepository(db)
  51. sp.thingImageRepository = repositories.InitThingImageRepository(db)
  52. sp.tagRepository = repositories.InitTagRepository(db)
  53. sp.thingTagRepository = repositories.InitThingTagRepository(db)
  54. sp.userRepository = repositories.InitUserRepository(db)
  55. sp.thingNotificationRepository = repositories.InitThingNotificationRepository(db)
  56. sp.fileRepository = repositories.InitFileRepository()
  57. return sp, nil
  58. }
  59. func (sp *ServiceProvider) GetEnvService() interfaces.Env {
  60. return sp.env
  61. }
  62. func (sp *ServiceProvider) GetAuthService() interfaces.Auth {
  63. return sp.auth
  64. }
  65. func (sp *ServiceProvider) GetMailerService() interfaces.Mailer {
  66. return sp.mailer
  67. }
  68. func (sp *ServiceProvider) GetPlaceRepository() interfaces.PlaceRepository {
  69. return sp.placeRepository
  70. }
  71. func (sp *ServiceProvider) GetThingRepository() interfaces.ThingRepository {
  72. return sp.thingRepository
  73. }
  74. func (sp *ServiceProvider) GetTagRepository() interfaces.TagRepository {
  75. return sp.tagRepository
  76. }
  77. func (sp *ServiceProvider) GetPlaceThingRepository() interfaces.PlaceThingRepository {
  78. return sp.placeThingRepository
  79. }
  80. func (sp *ServiceProvider) GetPlaceImageRepository() interfaces.PlaceImageRepository {
  81. return sp.placeImageRepository
  82. }
  83. func (sp *ServiceProvider) GetThingImageRepository() interfaces.ThingImageRepository {
  84. return sp.thingImageRepository
  85. }
  86. func (sp *ServiceProvider) GetThingTagRepository() interfaces.ThingTagRepository {
  87. return sp.thingTagRepository
  88. }
  89. func (sp *ServiceProvider) GetThingNotificationRepository() interfaces.ThingNotificationRepository {
  90. return sp.thingNotificationRepository
  91. }
  92. func (sp *ServiceProvider) GetUserRepository() interfaces.UserRepository {
  93. return sp.userRepository
  94. }
  95. func (sp *ServiceProvider) GetFileRepository() interfaces.FileRepository {
  96. return sp.fileRepository
  97. }
  98. func InitMock(deps ...interface{}) interfaces.ServiceProvider {
  99. sp := ServiceProvider{}
  100. for _, d := range deps {
  101. switch s := d.(type) {
  102. case interfaces.Env:
  103. sp.env = s
  104. case interfaces.Auth:
  105. sp.auth = s
  106. case interfaces.PlaceThingRepository:
  107. sp.placeThingRepository = s
  108. case interfaces.ThingRepository:
  109. sp.thingRepository = s
  110. case interfaces.PlaceRepository:
  111. sp.placeRepository = s
  112. case interfaces.PlaceImageRepository:
  113. sp.placeImageRepository = s
  114. case interfaces.ThingImageRepository:
  115. sp.thingImageRepository = s
  116. case interfaces.TagRepository:
  117. sp.tagRepository = s
  118. case interfaces.ThingTagRepository:
  119. sp.thingTagRepository = s
  120. case interfaces.ThingNotificationRepository:
  121. sp.thingNotificationRepository = s
  122. case interfaces.UserRepository:
  123. sp.userRepository = s
  124. case interfaces.FileRepository:
  125. sp.fileRepository = s
  126. }
  127. }
  128. return &sp
  129. }