tercul-backend/internal/application/services/analytics_service.go
google-labs-jules[bot] 52101fbeda Refactor: Expose Analytics Service via GraphQL
This commit refactors the analytics service to align with the new DDD architecture and exposes it through the GraphQL API.

Key changes:
- A new `AnalyticsService` has been created in `internal/application/services` to encapsulate analytics-related business logic.
- The GraphQL resolver has been updated to use the new `AnalyticsService`, providing a clean and maintainable API.
- The old analytics service and its related files have been removed, reducing code duplication and confusion.
- The `bookmark`, `like`, and `work` services have been refactored to remove their dependencies on the old analytics repository.
- Unit tests have been added for the new `AnalyticsService`, and existing tests have been updated to reflect the refactoring.
2025-10-03 04:10:16 +00:00

77 lines
1.6 KiB
Go

package services
import (
"context"
"tercul/internal/domain"
)
type AnalyticsService interface {
GetAnalytics(ctx context.Context) (*Analytics, error)
}
type analyticsService struct {
workRepo domain.WorkRepository
translationRepo domain.TranslationRepository
authorRepo domain.AuthorRepository
userRepo domain.UserRepository
likeRepo domain.LikeRepository
}
func NewAnalyticsService(
workRepo domain.WorkRepository,
translationRepo domain.TranslationRepository,
authorRepo domain.AuthorRepository,
userRepo domain.UserRepository,
likeRepo domain.LikeRepository,
) AnalyticsService {
return &analyticsService{
workRepo: workRepo,
translationRepo: translationRepo,
authorRepo: authorRepo,
userRepo: userRepo,
likeRepo: likeRepo,
}
}
type Analytics struct {
TotalWorks int64
TotalTranslations int64
TotalAuthors int64
TotalUsers int64
TotalLikes int64
}
func (s *analyticsService) GetAnalytics(ctx context.Context) (*Analytics, error) {
totalWorks, err := s.workRepo.Count(ctx)
if err != nil {
return nil, err
}
totalTranslations, err := s.translationRepo.Count(ctx)
if err != nil {
return nil, err
}
totalAuthors, err := s.authorRepo.Count(ctx)
if err != nil {
return nil, err
}
totalUsers, err := s.userRepo.Count(ctx)
if err != nil {
return nil, err
}
totalLikes, err := s.likeRepo.Count(ctx)
if err != nil {
return nil, err
}
return &Analytics{
TotalWorks: totalWorks,
TotalTranslations: totalTranslations,
TotalAuthors: totalAuthors,
TotalUsers: totalUsers,
TotalLikes: totalLikes,
}, nil
}