package svc import ( "code.30cm.net/digimon/app-cloudep-product-service/internal/config" "code.30cm.net/digimon/app-cloudep-product-service/pkg/domain/usecase" repo "code.30cm.net/digimon/app-cloudep-product-service/pkg/repository" uc "code.30cm.net/digimon/app-cloudep-product-service/pkg/usecase" mgo "code.30cm.net/digimon/library-go/mongo" "github.com/zeromicro/go-zero/core/stores/cache" "github.com/zeromicro/go-zero/core/stores/mon" ) type ServiceContext struct { Config config.Config CategoryUseCase usecase.CategoryUseCase TagsUseCase usecase.ProductBaseTags KYCUseCase usecase.KYCUseCase ProductItemUseCase usecase.ProductItemUseCase } func NewServiceContext(c config.Config) *ServiceContext { return &ServiceContext{ CategoryUseCase: MustCategory(c), TagsUseCase: MustTags(c), KYCUseCase: MustKYC(c), ProductItemUseCase: MustProductItem(c), Config: c, } } func MustCategory(c config.Config) usecase.CategoryUseCase { // 準備Mongo Config conf := &mgo.Conf{ Schema: c.Mongo.Schema, Host: c.Mongo.Host, Database: c.Mongo.Database, MaxStaleness: c.Mongo.MaxStaleness, MaxPoolSize: c.Mongo.MaxPoolSize, MinPoolSize: c.Mongo.MinPoolSize, MaxConnIdleTime: c.Mongo.MaxConnIdleTime, Compressors: c.Mongo.Compressors, EnableStandardReadWriteSplitMode: c.Mongo.EnableStandardReadWriteSplitMode, ConnectTimeoutMs: c.Mongo.ConnectTimeoutMs, } if c.Mongo.User != "" { conf.User = c.Mongo.User conf.Password = c.Mongo.Password } // 快取選項 cacheOpts := []cache.Option{ cache.WithExpiry(c.CacheExpireTime), cache.WithNotFoundExpiry(c.CacheWithNotFoundExpiry), } dbOpts := []mon.Option{ mgo.SetCustomDecimalType(), mgo.InitMongoOptions(*conf), } categoryRepo := repo.MustCategoryRepository(repo.CategoryRepositoryParam{ Conf: conf, CacheConf: c.Cache, CacheOpts: cacheOpts, DBOpts: dbOpts, }) return uc.MustCategoryUseCase(uc.CategoryUseCaseParam{ CategoryRepo: categoryRepo}) } func MustTags(c config.Config) usecase.ProductBaseTags { // 準備Mongo Config conf := &mgo.Conf{ Schema: c.Mongo.Schema, Host: c.Mongo.Host, Database: c.Mongo.Database, MaxStaleness: c.Mongo.MaxStaleness, MaxPoolSize: c.Mongo.MaxPoolSize, MinPoolSize: c.Mongo.MinPoolSize, MaxConnIdleTime: c.Mongo.MaxConnIdleTime, Compressors: c.Mongo.Compressors, EnableStandardReadWriteSplitMode: c.Mongo.EnableStandardReadWriteSplitMode, ConnectTimeoutMs: c.Mongo.ConnectTimeoutMs, } if c.Mongo.User != "" { conf.User = c.Mongo.User conf.Password = c.Mongo.Password } // 快取選項 cacheOpts := []cache.Option{ cache.WithExpiry(c.CacheExpireTime), cache.WithNotFoundExpiry(c.CacheWithNotFoundExpiry), } dbOpts := []mon.Option{ mgo.SetCustomDecimalType(), mgo.InitMongoOptions(*conf), } tagsRepo := repo.NewTagsRepository(repo.TagsRepositoryParam{ Conf: conf, CacheConf: c.Cache, CacheOpts: cacheOpts, DBOpts: dbOpts, }) return uc.MustTagsUseCase(uc.TagsUseCaseParam{ TagsRepo: tagsRepo}) } func MustKYC(c config.Config) usecase.KYCUseCase { // 準備Mongo Config conf := &mgo.Conf{ Schema: c.Mongo.Schema, Host: c.Mongo.Host, Database: c.Mongo.Database, MaxStaleness: c.Mongo.MaxStaleness, MaxPoolSize: c.Mongo.MaxPoolSize, MinPoolSize: c.Mongo.MinPoolSize, MaxConnIdleTime: c.Mongo.MaxConnIdleTime, Compressors: c.Mongo.Compressors, EnableStandardReadWriteSplitMode: c.Mongo.EnableStandardReadWriteSplitMode, ConnectTimeoutMs: c.Mongo.ConnectTimeoutMs, } if c.Mongo.User != "" { conf.User = c.Mongo.User conf.Password = c.Mongo.Password } // 快取選項 cacheOpts := []cache.Option{ cache.WithExpiry(c.CacheExpireTime), cache.WithNotFoundExpiry(c.CacheWithNotFoundExpiry), } dbOpts := []mon.Option{ mgo.SetCustomDecimalType(), mgo.InitMongoOptions(*conf), } kycRepo := repo.NewKYCRepository(repo.KYCRepositoryParam{ Conf: conf, CacheConf: c.Cache, CacheOpts: cacheOpts, DBOpts: dbOpts, }) return uc.MustKYCUseCase(uc.KYCUseCaseParam{ KYCRepo: kycRepo}) } func MustProductItem(c config.Config) usecase.ProductItemUseCase { // 準備Mongo Config conf := &mgo.Conf{ Schema: c.Mongo.Schema, Host: c.Mongo.Host, Database: c.Mongo.Database, MaxStaleness: c.Mongo.MaxStaleness, MaxPoolSize: c.Mongo.MaxPoolSize, MinPoolSize: c.Mongo.MinPoolSize, MaxConnIdleTime: c.Mongo.MaxConnIdleTime, Compressors: c.Mongo.Compressors, EnableStandardReadWriteSplitMode: c.Mongo.EnableStandardReadWriteSplitMode, ConnectTimeoutMs: c.Mongo.ConnectTimeoutMs, } if c.Mongo.User != "" { conf.User = c.Mongo.User conf.Password = c.Mongo.Password } // 快取選項 cacheOpts := []cache.Option{ cache.WithExpiry(c.CacheExpireTime), cache.WithNotFoundExpiry(c.CacheWithNotFoundExpiry), } dbOpts := []mon.Option{ mgo.SetCustomDecimalType(), mgo.InitMongoOptions(*conf), } productItemRepo := repo.NewProductItemRepository(repo.ProductItemRepositoryParam{ Conf: conf, CacheConf: c.Cache, CacheOpts: cacheOpts, DBOpts: dbOpts, }) return uc.MustProductItemUseCase(uc.ProductItemUseCaseParam{ ProductItems: productItemRepo}) }