feat: Add unix timestamp date range parameters for fetching paginated listens (#98)

This commit is contained in:
Matt Foxx 2025-11-20 11:43:09 -05:00 committed by GitHub
parent 620e3b65cb
commit c77481fd59
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 27 additions and 10 deletions

View file

@ -122,6 +122,8 @@ type GetItemsOpts struct {
Week int // 1-52
Month int // 1-12
Year int
From int // unix timestamp
To int // unix timestamp
// Used only for getting top tracks
ArtistID int

View file

@ -17,14 +17,23 @@ import (
func (d *Psql) GetListensPaginated(ctx context.Context, opts db.GetItemsOpts) (*db.PaginatedResponse[*models.Listen], error) {
l := logger.FromContext(ctx)
offset := (opts.Page - 1) * opts.Limit
t1, t2, err := utils.DateRange(opts.Week, opts.Month, opts.Year)
if err != nil {
return nil, fmt.Errorf("GetListensPaginated: %w", err)
}
if opts.Month == 0 && opts.Year == 0 {
// use period, not date range
t2 = time.Now()
t1 = db.StartTimeFromPeriod(opts.Period)
var t1 time.Time
var t2 time.Time
if opts.From != 0 && opts.To != 0 {
t1 = time.Unix(int64(opts.From), 0)
t2 = time.Unix(int64(opts.To), 0)
} else {
t1R, t2R, err := utils.DateRange(opts.Week, opts.Month, opts.Year)
if err != nil {
return nil, fmt.Errorf("GetListensPaginated: %w", err)
}
t1 = t1R
t2 = t2R
if opts.Month == 0 && opts.Year == 0 {
// use period, not date range
t2 = time.Now()
t1 = db.StartTimeFromPeriod(opts.Period)
}
}
if opts.Limit == 0 {
opts.Limit = DefaultItemsPerPage