feat: Rewrite to use the Diyanet API
This commit is contained in:
+71
-19
@@ -1,9 +1,12 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
@@ -22,8 +25,13 @@ type httpError struct {
|
||||
}
|
||||
|
||||
type Services struct {
|
||||
TimesProvider prayer.TimesProvider
|
||||
LocationTimesProvider prayer.LocationTimesProvider
|
||||
Provider DiyanetProvider
|
||||
}
|
||||
|
||||
type DiyanetProvider 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)
|
||||
}
|
||||
|
||||
func New(services Services) *fiber.App {
|
||||
@@ -55,33 +63,77 @@ func New(services Services) *fiber.App {
|
||||
})
|
||||
|
||||
app.Get("/api/v1/diyanet/prayertimes", func(ctx *fiber.Ctx) error {
|
||||
var times []prayer.Times
|
||||
var err error
|
||||
|
||||
locationID := ctx.Query("location_id")
|
||||
var loc struct {
|
||||
Latitude float64 `query:"latitude"`
|
||||
Longitude float64 `query:"longitude"`
|
||||
var query struct {
|
||||
LocationID string `query:"location_id"`
|
||||
Latitude string `query:"latitude"`
|
||||
Longitude string `query:"longitude"`
|
||||
}
|
||||
if err := ctx.QueryParser(&query); err != nil {
|
||||
return fmt.Errorf("failed to bind prayer times query parameters: %w", errors.Join(fiber.ErrBadRequest, err))
|
||||
}
|
||||
|
||||
if locationID != "" {
|
||||
times, err = services.TimesProvider.Get(ctx.Context(), locationID)
|
||||
} else if err := ctx.QueryParser(&loc); err == nil {
|
||||
times, err = services.LocationTimesProvider.GetByCoords(ctx.Context(), prayer.Coordinates{
|
||||
Latitude: loc.Latitude,
|
||||
Longitude: loc.Longitude,
|
||||
locationID := strings.TrimSpace(query.LocationID)
|
||||
latitude := strings.TrimSpace(query.Latitude)
|
||||
longitude := strings.TrimSpace(query.Longitude)
|
||||
|
||||
var (
|
||||
times []prayer.Times
|
||||
err error
|
||||
)
|
||||
|
||||
switch {
|
||||
case locationID != "":
|
||||
times, err = services.Provider.Get(ctx.Context(), locationID)
|
||||
case latitude != "" && longitude != "":
|
||||
lat, latErr := strconv.ParseFloat(latitude, 64)
|
||||
if latErr != nil {
|
||||
return fmt.Errorf("failed to parse latitude query parameter: %w", errors.Join(fiber.ErrBadRequest, latErr))
|
||||
}
|
||||
lng, lngErr := strconv.ParseFloat(longitude, 64)
|
||||
if lngErr != nil {
|
||||
return fmt.Errorf("failed to parse longitude query parameter: %w", errors.Join(fiber.ErrBadRequest, lngErr))
|
||||
}
|
||||
|
||||
times, err = services.Provider.GetByCoords(ctx.Context(), prayer.Coordinates{
|
||||
Latitude: lat,
|
||||
Longitude: lng,
|
||||
})
|
||||
} else {
|
||||
return fmt.Errorf("%w: missing location id or coordinates", fiber.ErrBadRequest)
|
||||
default:
|
||||
return fmt.Errorf("failed to validate prayer times query parameters: %w", fiber.ErrBadRequest)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("failed to fetch prayer times: %w", err)
|
||||
}
|
||||
|
||||
ctx.Response().Header.Add(fiber.HeaderCacheControl, "max-age=86400")
|
||||
return ctx.JSON(fiber.Map{
|
||||
"prayertimes": times,
|
||||
})
|
||||
})
|
||||
|
||||
return ctx.JSON(times)
|
||||
app.Get("/api/v1/diyanet/location", func(ctx *fiber.Ctx) error {
|
||||
var query struct {
|
||||
Text string `query:"query"`
|
||||
}
|
||||
if err := ctx.QueryParser(&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 == "" {
|
||||
return fmt.Errorf("failed to validate location query parameters: %w", fiber.ErrBadRequest)
|
||||
}
|
||||
|
||||
locations, err := services.Provider.SearchLocations(ctx.Context(), query.Text)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to search locations: %w", err)
|
||||
}
|
||||
|
||||
ctx.Response().Header.Add(fiber.HeaderCacheControl, "max-age=86400")
|
||||
return ctx.JSON(fiber.Map{
|
||||
"locations": locations,
|
||||
})
|
||||
})
|
||||
|
||||
app.Use("/", filesystem.New(filesystem.Config{
|
||||
|
||||
Reference in New Issue
Block a user