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

main
Matt Foxx 3 weeks ago committed by GitHub
parent 620e3b65cb
commit c77481fd59
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -42,6 +42,10 @@ func OptsFromRequest(r *http.Request) db.GetItemsOpts {
month, _ := strconv.Atoi(monthStr)
yearStr := r.URL.Query().Get("year")
year, _ := strconv.Atoi(yearStr)
fromStr := r.URL.Query().Get("from")
from, _ := strconv.Atoi(fromStr)
toStr := r.URL.Query().Get("to")
to, _ := strconv.Atoi(toStr)
artistIdStr := r.URL.Query().Get("artist_id")
artistId, _ := strconv.Atoi(artistIdStr)
@ -67,8 +71,8 @@ func OptsFromRequest(r *http.Request) db.GetItemsOpts {
period = db.PeriodDay
}
l.Debug().Msgf("OptsFromRequest: Parsed options: limit=%d, page=%d, week=%d, month=%d, year=%d, artist_id=%d, album_id=%d, track_id=%d, period=%s",
limit, page, week, month, year, artistId, albumId, trackId, period)
l.Debug().Msgf("OptsFromRequest: Parsed options: limit=%d, page=%d, week=%d, month=%d, year=%d, from=%d, to=%d, artist_id=%d, album_id=%d, track_id=%d, period=%s",
limit, page, week, month, year, from, to, artistId, albumId, trackId, period)
return db.GetItemsOpts{
Limit: limit,
@ -77,6 +81,8 @@ func OptsFromRequest(r *http.Request) db.GetItemsOpts {
Week: week,
Month: month,
Year: year,
From: from,
To: to,
ArtistID: artistId,
AlbumID: albumId,
TrackID: trackId,

@ -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

@ -17,15 +17,24 @@ 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)
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
}

Loading…
Cancel
Save