From ccc04dd9ee322c4bac68da94afcb5f068c5d2521 Mon Sep 17 00:00:00 2001 From: Abdussamet Kocak Date: Wed, 5 Apr 2023 07:36:25 +0200 Subject: [PATCH] fix: Return date as a string instead of local time --- main.go | 3 +- pkg/dbtimesprovider/provider.go | 29 ++++++++------- pkg/diyanet/scraper.go | 2 +- pkg/diyanetapi/provider.go | 12 ++++-- pkg/diyanetapi/provider_test.go | 4 +- pkg/prayer/types.go | 65 +++++++++++++++++++++++++++++---- 6 files changed, 87 insertions(+), 28 deletions(-) diff --git a/main.go b/main.go index 7a17205..9a1c913 100644 --- a/main.go +++ b/main.go @@ -38,11 +38,12 @@ func newServices() (api.Services, func(), error) { return api.Services{}, nil, fmt.Errorf("failed to init db: %w", err) } - dbProvider := dbtimesprovider.New(db, diyanetAPIProvider) if err := dbtimesprovider.Migrate(db.Db.(*sql.DB)); err != nil { return api.Services{}, nil, fmt.Errorf("failed to migrate database: %w", err) } + dbProvider := dbtimesprovider.New(db, diyanetAPIProvider) + return api.Services{ TimesProvider: dbProvider, LocationTimesProvider: diyanetAPIProvider, diff --git a/pkg/dbtimesprovider/provider.go b/pkg/dbtimesprovider/provider.go index 76d0066..43520ca 100644 --- a/pkg/dbtimesprovider/provider.go +++ b/pkg/dbtimesprovider/provider.go @@ -42,18 +42,19 @@ func (p Provider) Name() string { func (p Provider) Get(ctx context.Context, location string) ([]prayer.Times, error) { times, err := p.loadTimes(ctx, location) - if err == nil && len(times) != 0 { - return times, nil - } else if err != nil { + if err != nil { return nil, fmt.Errorf("failed to load prayer times from db: %w", err) } + if len(times) > 0 { + return times, nil + } times, err = p.provider.Get(ctx, location) if err != nil { return nil, fmt.Errorf("failed to get prayer times: %w", err) } - if len(times) > 1 { + if len(times) > 0 { if err := p.saveTimes(ctx, location, times); err != nil { return nil, fmt.Errorf("failed to save times to db: %w", err) } @@ -110,15 +111,15 @@ func Migrate(con *sql.DB) error { } type prayerTimesRow struct { - ProviderID int64 `db:"provider_id"` - LocationID string `db:"location_id"` - Date time.Time `db:"date"` - Fajr string `db:"fajr"` - Sunrise string `db:"sunrise"` - Dhuhr string `db:"dhuhr"` - Asr string `db:"asr"` - Maghrib string `db:"maghrib"` - Isha string `db:"isha"` + ProviderID int64 `db:"provider_id"` + LocationID string `db:"location_id"` + Date string `db:"date"` + Fajr string `db:"fajr"` + Sunrise string `db:"sunrise"` + Dhuhr string `db:"dhuhr"` + Asr string `db:"asr"` + Maghrib string `db:"maghrib"` + Isha string `db:"isha"` } func (r prayerTimesRow) toDomain() prayer.Times { @@ -166,7 +167,7 @@ func (p Provider) saveTimes(ctx context.Context, locationID string, times []pray func (p Provider) loadTimes(ctx context.Context, locationID string) ([]prayer.Times, error) { now := p.clockFunc() - today := now.Truncate(time.Hour * 24) + today := now.UTC().Truncate(time.Hour * 24) q := p.db. From(goqu.T("prayer_times").As("pt")). diff --git a/pkg/diyanet/scraper.go b/pkg/diyanet/scraper.go index a42e7c3..266f785 100644 --- a/pkg/diyanet/scraper.go +++ b/pkg/diyanet/scraper.go @@ -53,7 +53,7 @@ func (d Provider) Get(ctx context.Context, location string) ([]prayer.Times, err } row := prayer.Times{ - Date: parsedDate, + Date: parsedDate.Format(time.DateOnly), Fajr: el.Find("td:nth-of-type(2)").Text(), Sunrise: el.Find("td:nth-of-type(3)").Text(), Dhuhr: el.Find("td:nth-of-type(4)").Text(), diff --git a/pkg/diyanetapi/provider.go b/pkg/diyanetapi/provider.go index 5f5e145..beb4839 100644 --- a/pkg/diyanetapi/provider.go +++ b/pkg/diyanetapi/provider.go @@ -73,17 +73,21 @@ func (d Provider) parseResponse(res *req.Response) ([]prayer.Times, error) { return nil, fmt.Errorf("received error: %s", res.String()) } + if len(response.ResultObject.PrayerTimes) == 0 { + return nil, nil + } + var times []prayer.Times const format = "15:04" - now := time.Now() + today := time.Now().UTC().Truncate(time.Hour * 24) for _, pt := range response.ResultObject.PrayerTimes { - then := pt.Date.UTC().Truncate(time.Hour * 24) - if then.Before(now) { + then := prayer.Date(pt.Date).Time() + if then.Before(today) { continue } times = append(times, prayer.Times{ - Date: then, + Date: pt.Date.Format(time.DateOnly), Fajr: pt.Fajr.Format(format), Sunrise: pt.Sunrise.Format(format), Dhuhr: pt.Dhuhr.Format(format), diff --git a/pkg/diyanetapi/provider_test.go b/pkg/diyanetapi/provider_test.go index ad9be48..1bb13b5 100644 --- a/pkg/diyanetapi/provider_test.go +++ b/pkg/diyanetapi/provider_test.go @@ -30,6 +30,8 @@ func TestDiyanetAPI_GetByCoords(t *testing.T) { times, err := p.Get(context.Background(), "11104") assert.NoError(t, err) assert.NotEmpty(t, times) - t.Logf("%#+v", times[0]) + for _, time := range times { + t.Log(time) + } }) } diff --git a/pkg/prayer/types.go b/pkg/prayer/types.go index 0581f57..4337873 100644 --- a/pkg/prayer/types.go +++ b/pkg/prayer/types.go @@ -2,6 +2,8 @@ package prayer import ( "context" + "database/sql/driver" + "encoding/json" "errors" "time" ) @@ -23,12 +25,61 @@ type LocationTimesProvider interface { Name() string } +type Date time.Time + +func (d Date) String() string { + return time.Time(d).Format(time.DateOnly) +} + +func (d *Date) Scan(src any) error { + switch v := src.(type) { + case []byte: + return json.Unmarshal(v, d) + case string: + return json.Unmarshal([]byte(v), d) + case time.Time: + *d = Date(v) + return nil + } + return nil +} + +func (d *Date) Value() (driver.Value, error) { + return json.Marshal(d) +} + +func (d *Date) UnmarshalJSON(bytes []byte) error { + var t time.Time + if err := json.Unmarshal(bytes, &t); err != nil { + return err + } + _, offset := t.Zone() + t = t.Add(time.Duration(offset * int(time.Second))) + t = t.UTC() + *d = Date(t) + + return nil +} + +func (d Date) MarshalJSON() ([]byte, error) { + t := time.Time(d) + return json.Marshal(t.Format(time.DateOnly)) +} + +func (d Date) Time() time.Time { + t := time.Time(d) + _, offset := t.Zone() + t = t.Add(time.Duration(offset * int(time.Second))) + t = t.UTC() + return t +} + type Times struct { - Date time.Time `json:"date"` - Fajr string `json:"fajr"` - Sunrise string `json:"sunrise"` - Dhuhr string `json:"dhuhr"` - Asr string `json:"asr"` - Maghrib string `json:"maghrib"` - Isha string `json:"isha"` + Date string `json:"date"` + Fajr string `json:"fajr"` + Sunrise string `json:"sunrise"` + Dhuhr string `json:"dhuhr"` + Asr string `json:"asr"` + Maghrib string `json:"maghrib"` + Isha string `json:"isha"` }