feat: Add location search
This commit is contained in:
@@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/imroc/req/v3"
|
||||
@@ -67,61 +66,6 @@ func (d Provider) getLocationIDByCoords(ctx context.Context, coords prayer.Coord
|
||||
return fmt.Sprintf("%d", response.ResultObject[0].ID), nil
|
||||
}
|
||||
|
||||
func (d Provider) SearchLocations(ctx context.Context, query string) ([]prayer.Location, error) {
|
||||
query = strings.TrimSpace(query)
|
||||
if query == "" {
|
||||
return []prayer.Location{}, nil
|
||||
}
|
||||
|
||||
res, err := d.http.NewRequest().
|
||||
SetContext(ctx).
|
||||
SetQueryParam("searchText", query).
|
||||
Get("https://namazvakti.diyanet.gov.tr/api/Search/GetByName")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to search locations: %w", err)
|
||||
}
|
||||
|
||||
var response struct {
|
||||
Success bool `json:"success"`
|
||||
ResultObject struct {
|
||||
Results []struct {
|
||||
ID int `json:"cityID"`
|
||||
CityNameTR string `json:"cityNameTR"`
|
||||
StateNameTR string `json:"stateNameTR"`
|
||||
CountryNameTR string `json:"countryNameTR"`
|
||||
CityNameEN string `json:"cityNameEN"`
|
||||
StateNameEN string `json:"stateNameEN"`
|
||||
CountryNameEN string `json:"countryNameEN"`
|
||||
Latitude float64 `json:"latitude"`
|
||||
Longitude float64 `json:"longitude"`
|
||||
} `json:"results"`
|
||||
} `json:"resultObject"`
|
||||
}
|
||||
if err := res.Unmarshal(&response); err != nil {
|
||||
return nil, fmt.Errorf("failed to unmarshal search response: %w", err)
|
||||
}
|
||||
if !response.Success {
|
||||
return nil, fmt.Errorf("failed to search locations in upstream: %w", errors.New(res.String()))
|
||||
}
|
||||
|
||||
locations := make([]prayer.Location, 0, len(response.ResultObject.Results))
|
||||
for _, it := range response.ResultObject.Results {
|
||||
locations = append(locations, prayer.Location{
|
||||
ID: it.ID,
|
||||
NameTR: formatLocationName(it.CountryNameTR, it.StateNameTR, it.CityNameTR),
|
||||
NameEN: formatLocationName(it.CountryNameEN, it.StateNameEN, it.CityNameEN),
|
||||
Latitude: it.Latitude,
|
||||
Longitude: it.Longitude,
|
||||
})
|
||||
}
|
||||
|
||||
return locations, nil
|
||||
}
|
||||
|
||||
func formatLocationName(country, state, city string) string {
|
||||
return fmt.Sprintf("%s / %s / %s", strings.TrimSpace(country), strings.TrimSpace(state), strings.TrimSpace(city))
|
||||
}
|
||||
|
||||
func (d Provider) Get(ctx context.Context, locationID string) ([]prayer.Times, error) {
|
||||
res, err := d.http.NewRequest().
|
||||
SetContext(ctx).
|
||||
|
||||
Reference in New Issue
Block a user