final
This commit is contained in:
118
internal/usecase/process_persons.go
Normal file
118
internal/usecase/process_persons.go
Normal file
@@ -0,0 +1,118 @@
|
||||
package usecase
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/paramah/ai_devs4/s01e02/internal/domain"
|
||||
)
|
||||
|
||||
// ProcessPersonsUseCase handles the processing of persons
|
||||
type ProcessPersonsUseCase struct {
|
||||
personRepo domain.PersonRepository
|
||||
apiClient domain.APIClient
|
||||
apiKey string
|
||||
outputDir string
|
||||
}
|
||||
|
||||
// NewProcessPersonsUseCase creates a new use case instance
|
||||
func NewProcessPersonsUseCase(
|
||||
personRepo domain.PersonRepository,
|
||||
apiClient domain.APIClient,
|
||||
apiKey string,
|
||||
outputDir string,
|
||||
) *ProcessPersonsUseCase {
|
||||
return &ProcessPersonsUseCase{
|
||||
personRepo: personRepo,
|
||||
apiClient: apiClient,
|
||||
apiKey: apiKey,
|
||||
outputDir: outputDir,
|
||||
}
|
||||
}
|
||||
|
||||
// Execute processes all persons from the input file
|
||||
func (uc *ProcessPersonsUseCase) Execute(ctx context.Context, inputFile string) error {
|
||||
// Load persons from file
|
||||
log.Printf("Loading persons from: %s", inputFile)
|
||||
persons, err := uc.personRepo.LoadPersons(ctx, inputFile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("loading persons: %w", err)
|
||||
}
|
||||
log.Printf("Loaded %d persons", len(persons))
|
||||
|
||||
// Process each person
|
||||
for i, person := range persons {
|
||||
log.Printf("\n[%d/%d] Processing: %s %s", i+1, len(persons), person.Name, person.Surname)
|
||||
|
||||
// Get location information
|
||||
if err := uc.processLocation(ctx, person); err != nil {
|
||||
log.Printf("Error getting location for %s %s: %v", person.Name, person.Surname, err)
|
||||
continue
|
||||
}
|
||||
|
||||
// Get access level information
|
||||
if err := uc.processAccessLevel(ctx, person); err != nil {
|
||||
log.Printf("Error getting access level for %s %s: %v", person.Name, person.Surname, err)
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
log.Printf("\nProcessing completed!")
|
||||
return nil
|
||||
}
|
||||
|
||||
// processLocation gets and saves location information for a person
|
||||
func (uc *ProcessPersonsUseCase) processLocation(ctx context.Context, person domain.Person) error {
|
||||
req := domain.LocationRequest{
|
||||
APIKey: uc.apiKey,
|
||||
Name: person.Name,
|
||||
Surname: person.Surname,
|
||||
}
|
||||
|
||||
log.Printf(" → Getting location...")
|
||||
response, err := uc.apiClient.GetLocation(ctx, req)
|
||||
if err != nil {
|
||||
return fmt.Errorf("getting location: %w", err)
|
||||
}
|
||||
|
||||
// Save to file
|
||||
fileName := fmt.Sprintf("%s_%s.json", person.Name, person.Surname)
|
||||
filePath := filepath.Join(uc.outputDir, "locations", fileName)
|
||||
|
||||
if err := os.WriteFile(filePath, response, 0644); err != nil {
|
||||
return fmt.Errorf("saving location response: %w", err)
|
||||
}
|
||||
|
||||
log.Printf(" ✓ Location saved to: %s", filePath)
|
||||
return nil
|
||||
}
|
||||
|
||||
// processAccessLevel gets and saves access level information for a person
|
||||
func (uc *ProcessPersonsUseCase) processAccessLevel(ctx context.Context, person domain.Person) error {
|
||||
req := domain.AccessLevelRequest{
|
||||
APIKey: uc.apiKey,
|
||||
Name: person.Name,
|
||||
Surname: person.Surname,
|
||||
BirthYear: person.Born,
|
||||
}
|
||||
|
||||
log.Printf(" → Getting access level...")
|
||||
response, err := uc.apiClient.GetAccessLevel(ctx, req)
|
||||
if err != nil {
|
||||
return fmt.Errorf("getting access level: %w", err)
|
||||
}
|
||||
|
||||
// Save to file
|
||||
fileName := fmt.Sprintf("%s_%s.json", person.Name, person.Surname)
|
||||
filePath := filepath.Join(uc.outputDir, "accesslevel", fileName)
|
||||
|
||||
if err := os.WriteFile(filePath, response, 0644); err != nil {
|
||||
return fmt.Errorf("saving access level response: %w", err)
|
||||
}
|
||||
|
||||
log.Printf(" ✓ Access level saved to: %s", filePath)
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user