tercul-backend/internal/app/application_builder.go
google-labs-jules[bot] bb5e18d162 refactor: Introduce application layer and dataloaders
This commit introduces a new application layer to the codebase, which decouples the GraphQL resolvers from the data layer. The resolvers now call application services, which in turn call the repositories. This change improves the separation of concerns and makes the code more testable and maintainable.

Additionally, this commit introduces dataloaders to solve the N+1 problem in the GraphQL resolvers. The dataloaders are used to batch and cache database queries, which significantly improves the performance of the API.

The following changes were made:
- Created application services for most of the domains.
- Refactored the GraphQL resolvers to use the new application services.
- Implemented dataloaders for the `Author` aggregate.
- Updated the `app.Application` struct to hold the application services instead of the repositories.
- Fixed a large number of compilation errors in the test files that arose from these changes.

There are still some compilation errors in the `internal/adapters/graphql/integration_test.go` file. These errors are due to the test files still trying to access the repositories directly from the `app.Application` struct. The remaining work is to update these tests to use the new application services.
2025-09-08 10:19:43 +00:00

262 lines
9.4 KiB
Go

package app
import (
"tercul/internal/app/auth"
"tercul/internal/app/author"
"tercul/internal/app/bookmark"
"tercul/internal/app/category"
"tercul/internal/app/collection"
"tercul/internal/app/comment"
"tercul/internal/app/copyright"
"tercul/internal/app/like"
"tercul/internal/app/localization"
"tercul/internal/app/analytics"
"tercul/internal/app/monetization"
app_search "tercul/internal/app/search"
"tercul/internal/app/tag"
"tercul/internal/app/translation"
"tercul/internal/app/user"
"tercul/internal/app/work"
"tercul/internal/data/sql"
"tercul/internal/platform/cache"
"tercul/internal/platform/config"
"tercul/internal/platform/db"
"tercul/internal/platform/log"
auth_platform "tercul/internal/platform/auth"
platform_search "tercul/internal/platform/search"
"tercul/internal/jobs/linguistics"
"github.com/hibiken/asynq"
"github.com/weaviate/weaviate-go-client/v5/weaviate"
"gorm.io/gorm"
)
// ApplicationBuilder handles the initialization of all application components
type ApplicationBuilder struct {
dbConn *gorm.DB
redisCache cache.Cache
weaviateWrapper platform_search.WeaviateWrapper
asynqClient *asynq.Client
App *Application
linguistics *linguistics.LinguisticsFactory
}
// NewApplicationBuilder creates a new ApplicationBuilder
func NewApplicationBuilder() *ApplicationBuilder {
return &ApplicationBuilder{}
}
// BuildDatabase initializes the database connection
func (b *ApplicationBuilder) BuildDatabase() error {
log.LogInfo("Initializing database connection")
dbConn, err := db.InitDB()
if err != nil {
log.LogFatal("Failed to initialize database", log.F("error", err))
return err
}
b.dbConn = dbConn
log.LogInfo("Database initialized successfully")
return nil
}
// BuildCache initializes the Redis cache
func (b *ApplicationBuilder) BuildCache() error {
log.LogInfo("Initializing Redis cache")
redisCache, err := cache.NewDefaultRedisCache()
if err != nil {
log.LogWarn("Failed to initialize Redis cache, continuing without caching", log.F("error", err))
} else {
b.redisCache = redisCache
log.LogInfo("Redis cache initialized successfully")
}
return nil
}
// BuildWeaviate initializes the Weaviate client
func (b *ApplicationBuilder) BuildWeaviate() error {
log.LogInfo("Connecting to Weaviate", log.F("host", config.Cfg.WeaviateHost))
wClient, err := weaviate.NewClient(weaviate.Config{
Scheme: config.Cfg.WeaviateScheme,
Host: config.Cfg.WeaviateHost,
})
if err != nil {
log.LogFatal("Failed to create Weaviate client", log.F("error", err))
return err
}
b.weaviateWrapper = platform_search.NewWeaviateWrapper(wClient)
log.LogInfo("Weaviate client initialized successfully")
return nil
}
// BuildBackgroundJobs initializes Asynq for background job processing
func (b *ApplicationBuilder) BuildBackgroundJobs() error {
log.LogInfo("Setting up background job processing")
redisOpt := asynq.RedisClientOpt{
Addr: config.Cfg.RedisAddr,
Password: config.Cfg.RedisPassword,
DB: config.Cfg.RedisDB,
}
b.asynqClient = asynq.NewClient(redisOpt)
log.LogInfo("Background job client initialized successfully")
return nil
}
// BuildLinguistics initializes the linguistics components
func (b *ApplicationBuilder) BuildLinguistics() error {
log.LogInfo("Initializing linguistic analyzer")
// Create sentiment provider
var sentimentProvider linguistics.SentimentProvider
sentimentProvider, err := linguistics.NewGoVADERSentimentProvider()
if err != nil {
log.LogWarn("Failed to initialize GoVADER sentiment provider, using rule-based fallback", log.F("error", err))
sentimentProvider = &linguistics.RuleBasedSentimentProvider{}
}
// Create linguistics factory and pass in the sentiment provider
b.linguistics = linguistics.NewLinguisticsFactory(b.dbConn, b.redisCache, 4, true, sentimentProvider)
log.LogInfo("Linguistics components initialized successfully")
return nil
}
// BuildApplication initializes all application services
func (b *ApplicationBuilder) BuildApplication() error {
log.LogInfo("Initializing application layer")
// Initialize repositories
// Note: This is a simplified wiring. In a real app, you might have more complex dependencies.
workRepo := sql.NewWorkRepository(b.dbConn)
// I need to add all the other repos here. For now, I'll just add the ones I need for the services.
translationRepo := sql.NewTranslationRepository(b.dbConn)
copyrightRepo := sql.NewCopyrightRepository(b.dbConn)
authorRepo := sql.NewAuthorRepository(b.dbConn)
collectionRepo := sql.NewCollectionRepository(b.dbConn)
commentRepo := sql.NewCommentRepository(b.dbConn)
likeRepo := sql.NewLikeRepository(b.dbConn)
bookmarkRepo := sql.NewBookmarkRepository(b.dbConn)
userRepo := sql.NewUserRepository(b.dbConn)
tagRepo := sql.NewTagRepository(b.dbConn)
categoryRepo := sql.NewCategoryRepository(b.dbConn)
// Initialize application services
workCommands := work.NewWorkCommands(workRepo, b.linguistics.GetAnalyzer())
workQueries := work.NewWorkQueries(workRepo)
translationCommands := translation.NewTranslationCommands(translationRepo)
translationQueries := translation.NewTranslationQueries(translationRepo)
authorCommands := author.NewAuthorCommands(authorRepo)
authorQueries := author.NewAuthorQueries(authorRepo)
collectionCommands := collection.NewCollectionCommands(collectionRepo)
collectionQueries := collection.NewCollectionQueries(collectionRepo)
analyticsRepo := sql.NewAnalyticsRepository(b.dbConn)
analysisRepo := linguistics.NewGORMAnalysisRepository(b.dbConn)
analyticsService := analytics.NewService(analyticsRepo, analysisRepo, translationRepo, workRepo, b.linguistics.GetSentimentProvider())
commentCommands := comment.NewCommentCommands(commentRepo, analyticsService)
commentQueries := comment.NewCommentQueries(commentRepo)
likeCommands := like.NewLikeCommands(likeRepo, analyticsService)
likeQueries := like.NewLikeQueries(likeRepo)
bookmarkCommands := bookmark.NewBookmarkCommands(bookmarkRepo, analyticsService)
bookmarkQueries := bookmark.NewBookmarkQueries(bookmarkRepo)
userQueries := user.NewUserQueries(userRepo)
tagQueries := tag.NewTagQueries(tagRepo)
categoryQueries := category.NewCategoryQueries(categoryRepo)
jwtManager := auth_platform.NewJWTManager()
authCommands := auth.NewAuthCommands(userRepo, jwtManager)
authQueries := auth.NewAuthQueries(userRepo, jwtManager)
copyrightCommands := copyright.NewCopyrightCommands(copyrightRepo)
bookRepo := sql.NewBookRepository(b.dbConn)
publisherRepo := sql.NewPublisherRepository(b.dbConn)
sourceRepo := sql.NewSourceRepository(b.dbConn)
copyrightQueries := copyright.NewCopyrightQueries(copyrightRepo, workRepo, authorRepo, bookRepo, publisherRepo, sourceRepo)
localizationService := localization.NewService(translationRepo)
searchService := app_search.NewIndexService(localizationService, b.weaviateWrapper)
b.App = &Application{
AnalyticsService: analyticsService,
WorkCommands: workCommands,
WorkQueries: workQueries,
TranslationCommands: translationCommands,
TranslationQueries: translationQueries,
AuthCommands: authCommands,
AuthQueries: authQueries,
AuthorCommands: authorCommands,
AuthorQueries: authorQueries,
CollectionCommands: collectionCommands,
CollectionQueries: collectionQueries,
CommentCommands: commentCommands,
CommentQueries: commentQueries,
CopyrightCommands: copyrightCommands,
CopyrightQueries: copyrightQueries,
LikeCommands: likeCommands,
LikeQueries: likeQueries,
BookmarkCommands: bookmarkCommands,
BookmarkQueries: bookmarkQueries,
CategoryQueries: categoryQueries,
Localization: localizationService,
Search: searchService,
UserQueries: userQueries,
TagQueries: tagQueries,
BookRepo: sql.NewBookRepository(b.dbConn),
PublisherRepo: sql.NewPublisherRepository(b.dbConn),
SourceRepo: sql.NewSourceRepository(b.dbConn),
MonetizationQueries: monetization.NewMonetizationQueries(sql.NewMonetizationRepository(b.dbConn), workRepo, authorRepo, bookRepo, publisherRepo, sourceRepo),
CopyrightRepo: copyrightRepo,
MonetizationRepo: sql.NewMonetizationRepository(b.dbConn),
}
log.LogInfo("Application layer initialized successfully")
return nil
}
// Build initializes all components in the correct order
func (b *ApplicationBuilder) Build() error {
if err := b.BuildDatabase(); err != nil { return err }
if err := b.BuildCache(); err != nil { return err }
if err := b.BuildWeaviate(); err != nil { return err }
if err := b.BuildBackgroundJobs(); err != nil { return err }
if err := b.BuildLinguistics(); err != nil { return err }
if err := b.BuildApplication(); err != nil { return err }
log.LogInfo("Application builder completed successfully")
return nil
}
// GetApplication returns the application container
func (b *ApplicationBuilder) GetApplication() *Application {
return b.App
}
// GetDB returns the database connection
func (b *ApplicationBuilder) GetDB() *gorm.DB {
return b.dbConn
}
// GetAsynq returns the Asynq client
func (b *ApplicationBuilder) GetAsynq() *asynq.Client {
return b.asynqClient
}
// GetLinguisticsFactory returns the linguistics factory
func (b *ApplicationBuilder) GetLinguisticsFactory() *linguistics.LinguisticsFactory {
return b.linguistics
}
// Close closes all resources
func (b *ApplicationBuilder) Close() error {
if b.asynqClient != nil {
b.asynqClient.Close()
}
if b.dbConn != nil {
sqlDB, err := b.dbConn.DB()
if err == nil {
sqlDB.Close()
}
}
return nil
}