feat: Add location search
This commit is contained in:
@@ -12,7 +12,6 @@ import (
|
||||
type APIProvider interface {
|
||||
Get(ctx context.Context, locationID string) ([]prayer.Times, error)
|
||||
GetByCoords(ctx context.Context, coords prayer.Coordinates) ([]prayer.Times, error)
|
||||
SearchLocations(ctx context.Context, query string) ([]prayer.Location, error)
|
||||
}
|
||||
|
||||
type FallbackProvider interface {
|
||||
@@ -29,18 +28,11 @@ func New(api APIProvider, fallback FallbackProvider) Provider {
|
||||
return Provider{
|
||||
api: api,
|
||||
fallback: fallback,
|
||||
timeout: time.Second,
|
||||
timeout: 2 * time.Second,
|
||||
}
|
||||
}
|
||||
|
||||
func (p Provider) SearchLocations(ctx context.Context, query string) ([]prayer.Location, error) {
|
||||
locations, err := p.api.SearchLocations(ctx, query)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to search locations from api provider: %w", err)
|
||||
}
|
||||
|
||||
return locations, nil
|
||||
}
|
||||
var ErrEmptyTimes = errors.New("diyanet did not return any prayer times")
|
||||
|
||||
func (p Provider) Get(ctx context.Context, locationID string) ([]prayer.Times, error) {
|
||||
ctxWithTimeout, cancel := context.WithTimeout(ctx, p.timeout)
|
||||
@@ -51,7 +43,7 @@ func (p Provider) Get(ctx context.Context, locationID string) ([]prayer.Times, e
|
||||
return nil, fmt.Errorf("failed to get prayer times from api provider: %w", err)
|
||||
}
|
||||
if len(times) == 0 {
|
||||
return nil, fmt.Errorf("failed to get prayer times from api provider: %w", errors.New("empty prayer times result"))
|
||||
return nil, ErrEmptyTimes
|
||||
}
|
||||
|
||||
return times, nil
|
||||
@@ -66,16 +58,14 @@ func (p Provider) GetByCoords(ctx context.Context, coords prayer.Coordinates) ([
|
||||
return times, nil
|
||||
}
|
||||
|
||||
fallbackTimes, fallbackErr := p.fallback.GetByCoords(ctx, coords)
|
||||
if fallbackErr != nil {
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get prayer times from fallback provider: %w", errors.Join(err, fallbackErr))
|
||||
}
|
||||
return nil, fmt.Errorf("failed to get prayer times from fallback provider: %w", fallbackErr)
|
||||
}
|
||||
if len(fallbackTimes) == 0 {
|
||||
return nil, fmt.Errorf("failed to get prayer times from fallback provider: %w", errors.New("empty prayer times result"))
|
||||
times, err = p.fallback.GetByCoords(ctx, coords)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get prayer times from fallback provider: %w", err)
|
||||
}
|
||||
|
||||
return fallbackTimes, nil
|
||||
if len(times) == 0 {
|
||||
return nil, fmt.Errorf("fallback provider did not return any prayer times")
|
||||
}
|
||||
|
||||
return times, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user