tercul-backend/internal/enrichment/author_enricher.go
Damir Mukimov d50722dad5
Some checks failed
Test / Integration Tests (push) Successful in 4s
Build / Build Binary (push) Failing after 2m9s
Docker Build / Build Docker Image (push) Failing after 2m32s
Test / Unit Tests (push) Failing after 3m12s
Lint / Go Lint (push) Failing after 1m0s
Refactor ID handling to use UUIDs across the application
- Updated database models and repositories to replace uint IDs with UUIDs.
- Modified test fixtures to generate and use UUIDs for authors, translations, users, and works.
- Adjusted mock implementations to align with the new UUID structure.
- Ensured all relevant functions and methods are updated to handle UUIDs correctly.
- Added necessary imports for UUID handling in various files.
2025-12-27 00:33:34 +01:00

79 lines
2.2 KiB
Go

package enrichment
import (
"context"
"fmt"
"tercul/internal/domain"
"tercul/internal/platform/openlibrary"
)
// OpenLibraryAuthorEnricher enriches author data from the Open Library API.
type OpenLibraryAuthorEnricher struct {
client *openlibrary.Client
}
// NewOpenLibraryAuthorEnricher creates a new OpenLibraryAuthorEnricher.
func NewOpenLibraryAuthorEnricher() *OpenLibraryAuthorEnricher {
return &OpenLibraryAuthorEnricher{
client: openlibrary.NewClient(),
}
}
// Name returns the name of the enricher.
func (e *OpenLibraryAuthorEnricher) Name() string {
return "openlibrary_author_enricher"
}
// Enrich fetches data from the Open Library API and enriches the author.
func (e *OpenLibraryAuthorEnricher) Enrich(ctx context.Context, author *domain.Author) error {
if author.OpenLibraryID == "" {
// No OLID to look up.
return nil
}
olAuthor, err := e.client.GetAuthor(ctx, author.OpenLibraryID)
if err != nil {
return fmt.Errorf("failed to get author from Open Library: %w", err)
}
if olAuthor.Bio != nil {
// The bio can be a string or a struct with a 'value' field.
if bioStr, ok := olAuthor.Bio.(string); ok {
// Find or create the English translation for the bio.
e.updateBioTranslation(author, bioStr)
} else if bioMap, ok := olAuthor.Bio.(map[string]interface{}); ok {
if bioValue, ok := bioMap["value"].(string); ok {
e.updateBioTranslation(author, bioValue)
}
}
}
return nil
}
func (e *OpenLibraryAuthorEnricher) updateBioTranslation(author *domain.Author, bio string) {
// This is a simplified implementation. A real one would need to handle
// creating or updating a translation record associated with the author.
// For now, we'll just append it to the author's existing bio if it's empty.
var bioTranslation *domain.Translation
for _, t := range author.Translations {
if t.TranslatableType == "authors" && t.Language == "en" {
bioTranslation = t
break
}
}
if bioTranslation == nil {
author.Translations = append(author.Translations, &domain.Translation{
Content: bio,
Language: "en",
TranslatableID: author.ID,
TranslatableType: "authors",
})
} else {
if bioTranslation.Content == "" {
bioTranslation.Content = bio
}
}
}