mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 05:11:34 +00:00
This change introduces a major architectural refactoring of the application, with a focus on improving testability, decoupling, and observability. The following domains have been successfully refactored: - `localization`: Wrote a full suite of unit tests and added logging. - `auth`: Introduced a `JWTManager` interface, wrote comprehensive unit tests, and added logging. - `copyright`: Separated integration tests, wrote a full suite of unit tests, and added logging. - `monetization`: Wrote a full suite of unit tests and added logging. - `search`: Refactored the Weaviate client usage by creating a wrapper to improve testability, and achieved 100% test coverage. For each of these domains, 100% test coverage has been achieved for the refactored code. The refactoring of the `work` domain is currently in progress. Unit tests have been written for the commands and queries, but there is a persistent build issue with the query tests that needs to be resolved. The error indicates that the query methods are undefined, despite appearing to be correctly defined and called.
45 lines
1.3 KiB
Go
45 lines
1.3 KiB
Go
package search
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"tercul/internal/app/localization"
|
|
"tercul/internal/domain"
|
|
"tercul/internal/platform/log"
|
|
"tercul/internal/platform/search"
|
|
)
|
|
|
|
// IndexService pushes localized snapshots into Weaviate for search
|
|
type IndexService interface {
|
|
IndexWork(ctx context.Context, work domain.Work) error
|
|
}
|
|
|
|
type indexService struct {
|
|
localization localization.Service
|
|
weaviate search.WeaviateWrapper
|
|
}
|
|
|
|
func NewIndexService(localization localization.Service, weaviate search.WeaviateWrapper) IndexService {
|
|
return &indexService{localization: localization, weaviate: weaviate}
|
|
}
|
|
|
|
func (s *indexService) IndexWork(ctx context.Context, work domain.Work) error {
|
|
log.LogDebug("Indexing work", log.F("work_id", work.ID))
|
|
// Choose best content snapshot for indexing
|
|
content, err := s.localization.GetWorkContent(ctx, work.ID, work.Language)
|
|
if err != nil {
|
|
log.LogError("Failed to get work content for indexing", log.F("work_id", work.ID), log.F("error", err))
|
|
return err
|
|
}
|
|
|
|
err = s.weaviate.IndexWork(ctx, &work, content)
|
|
if err != nil {
|
|
log.LogError("Failed to index work in Weaviate", log.F("work_id", work.ID), log.F("error", err))
|
|
return err
|
|
}
|
|
log.LogInfo("Successfully indexed work", log.F("work_id", work.ID))
|
|
return nil
|
|
}
|
|
|
|
func formatID(id uint) string { return fmt.Sprintf("%d", id) }
|