mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 04:01:34 +00:00
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.
77 lines
1.6 KiB
Go
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
|
|
} |