feat: Add location search

This commit is contained in:
2026-02-21 01:50:59 +03:00
parent 26af8af8a9
commit a53b200c21
5 changed files with 327 additions and 105 deletions
+30 -4
View File
@@ -36,6 +36,7 @@ type PrayerProvider interface {
type LocationProvider interface {
SearchLocations(ctx context.Context, query string) ([]prayer.Location, error)
SearchLocationsByCoords(ctx context.Context, coords prayer.Coordinates) ([]prayer.Location, error)
}
func New(services Services) *fiber.App {
@@ -117,18 +118,43 @@ func New(services Services) *fiber.App {
app.Get("/api/v1/diyanet/location", func(ctx fiber.Ctx) error {
var query struct {
Text string `query:"query"`
Text string `query:"query"`
Latitude string `query:"latitude"`
Longitude string `query:"longitude"`
}
if err := ctx.Bind().Query(&query); err != nil {
return fmt.Errorf("failed to bind location query parameters: %w", errors.Join(fiber.ErrBadRequest, err))
}
query.Text = strings.TrimSpace(query.Text)
if query.Text == "" {
query.Latitude = strings.TrimSpace(query.Latitude)
query.Longitude = strings.TrimSpace(query.Longitude)
var (
locations []prayer.Location
err error
)
switch {
case query.Text != "" && query.Latitude == "" && query.Longitude == "":
locations, err = services.LocationProvider.SearchLocations(ctx.Context(), query.Text)
case query.Text == "" && query.Latitude != "" && query.Longitude != "":
lat, latErr := strconv.ParseFloat(query.Latitude, 64)
if latErr != nil {
return fmt.Errorf("failed to parse latitude query parameter: %w", errors.Join(fiber.ErrBadRequest, latErr))
}
lng, lngErr := strconv.ParseFloat(query.Longitude, 64)
if lngErr != nil {
return fmt.Errorf("failed to parse longitude query parameter: %w", errors.Join(fiber.ErrBadRequest, lngErr))
}
locations, err = services.LocationProvider.SearchLocationsByCoords(ctx.Context(), prayer.Coordinates{
Latitude: lat,
Longitude: lng,
})
default:
return fmt.Errorf("failed to validate location query parameters: %w", fiber.ErrBadRequest)
}
locations, err := services.LocationProvider.SearchLocations(ctx.Context(), query.Text)
if err != nil {
return fmt.Errorf("failed to search locations: %w", err)
}