service_context.go 3.5 KB
package svc

import (
	"github.com/zeromicro/go-zero/core/stores/redis"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/config"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/repository"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/cache"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/database"
	"gorm.io/gorm"
)

type ServiceContext struct {
	Config config.Config
	DB     *gorm.DB
	Redis  *redis.Redis

	ArticleBackupRepository  domain.ArticleBackupRepository
	ArticleCommentRepository domain.ArticleCommentRepository
	ArticleDraftRepository   domain.ArticleDraftRepository
	ArticleRepository        domain.ArticleRepository
	ArticleSectionRepository domain.ArticleSectionRepository
	ArticleTagRepository     domain.ArticleTagRepository

	CompanyRepository         domain.CompanyRepository
	CommentRepository         domain.CommentRepository // 待移除
	DepartmentRepository      domain.DepartmentRepository
	MessageBusinessRepository domain.MessageBusinessRepository
	MessageSystemRepository   domain.MessageSystemRepository
	RoleRepository            domain.RoleRepository
	UserFollowRepository      domain.UserFollowRepository
	UserLoveFlagRepository    domain.UserLoveFlagRepository
	UserRepository            domain.UserRepository
}

func NewServiceContext(c config.Config) *ServiceContext {

	db := database.OpenGormPGDB(c.DB.DataSource, c.Log.Mode)

	mlCache := cache.NewMultiLevelCache([]string{c.Redis.Host}, c.Redis.Pass)
	redis, _ := redis.NewRedis(redis.RedisConf{Host: c.Redis.Host, Pass: c.Redis.Pass, Type: "node"})

	return &ServiceContext{
		Config:                    c,
		DB:                        db,
		Redis:                     redis,
		CommentRepository:         repository.NewCommentRepository(cache.NewCachedRepository(mlCache)),
		ArticleBackupRepository:   repository.NewArticleBackupRepository(cache.NewCachedRepository(mlCache)),
		ArticleCommentRepository:  repository.NewArticleCommentRepository(cache.NewCachedRepository(mlCache)),
		ArticleDraftRepository:    repository.NewArticleDraftRepository(cache.NewCachedRepository(mlCache)),
		ArticleRepository:         repository.NewArticleRepository(cache.NewCachedRepository(mlCache)),
		ArticleSectionRepository:  repository.NewArticleSectionRepository(cache.NewCachedRepository(mlCache)),
		CompanyRepository:         repository.NewCompanyRepository(cache.NewCachedRepository(mlCache)),
		DepartmentRepository:      repository.NewDepartmentRepository(cache.NewCachedRepository(mlCache)),
		MessageBusinessRepository: repository.NewMessageBusinessRepository(cache.NewCachedRepository(mlCache)),
		MessageSystemRepository:   repository.NewMessageSystemRepository(cache.NewCachedRepository(mlCache)),
		RoleRepository:            repository.NewRoleRepository(cache.NewCachedRepository(mlCache)),
		UserFollowRepository:      repository.NewUserFollowRepository(cache.NewCachedRepository(mlCache)),
		UserLoveFlagRepository:    repository.NewUserLoveFlagRepository(cache.NewCachedRepository(mlCache)),
		UserRepository:            repository.NewUserRepository(cache.NewCachedRepository(mlCache)),
		ArticleTagRepository:      repository.NewArticleTagRepository(cache.NewCachedRepository(mlCache)),
	}
}

func (svc *ServiceContext) DefaultDBConn() transaction.Conn {
	return transaction.NewTransactionContext(svc.DB)
}