34 lines
717 B
Go
34 lines
717 B
Go
package json
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/paramah/ai_devs4/s01e02/internal/domain"
|
|
)
|
|
|
|
// Repository implements domain.PersonRepository
|
|
type Repository struct{}
|
|
|
|
// NewRepository creates a new JSON repository
|
|
func NewRepository() *Repository {
|
|
return &Repository{}
|
|
}
|
|
|
|
// LoadPersons loads persons from a JSON file
|
|
func (r *Repository) LoadPersons(ctx context.Context, filePath string) ([]domain.Person, error) {
|
|
data, err := os.ReadFile(filePath)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("reading file: %w", err)
|
|
}
|
|
|
|
var persons []domain.Person
|
|
if err := json.Unmarshal(data, &persons); err != nil {
|
|
return nil, fmt.Errorf("parsing JSON: %w", err)
|
|
}
|
|
|
|
return persons, nil
|
|
}
|