mirror of
https://github.com/gabehf/Koito.git
synced 2026-03-15 02:15:55 -07:00
feat: Add unix timestamp date range parameters for fetching paginated listens (#98)
This commit is contained in:
parent
620e3b65cb
commit
c77481fd59
3 changed files with 27 additions and 10 deletions
|
|
@ -42,6 +42,10 @@ func OptsFromRequest(r *http.Request) db.GetItemsOpts {
|
||||||
month, _ := strconv.Atoi(monthStr)
|
month, _ := strconv.Atoi(monthStr)
|
||||||
yearStr := r.URL.Query().Get("year")
|
yearStr := r.URL.Query().Get("year")
|
||||||
year, _ := strconv.Atoi(yearStr)
|
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")
|
artistIdStr := r.URL.Query().Get("artist_id")
|
||||||
artistId, _ := strconv.Atoi(artistIdStr)
|
artistId, _ := strconv.Atoi(artistIdStr)
|
||||||
|
|
@ -67,8 +71,8 @@ func OptsFromRequest(r *http.Request) db.GetItemsOpts {
|
||||||
period = db.PeriodDay
|
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",
|
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, artistId, albumId, trackId, period)
|
limit, page, week, month, year, from, to, artistId, albumId, trackId, period)
|
||||||
|
|
||||||
return db.GetItemsOpts{
|
return db.GetItemsOpts{
|
||||||
Limit: limit,
|
Limit: limit,
|
||||||
|
|
@ -77,6 +81,8 @@ func OptsFromRequest(r *http.Request) db.GetItemsOpts {
|
||||||
Week: week,
|
Week: week,
|
||||||
Month: month,
|
Month: month,
|
||||||
Year: year,
|
Year: year,
|
||||||
|
From: from,
|
||||||
|
To: to,
|
||||||
ArtistID: artistId,
|
ArtistID: artistId,
|
||||||
AlbumID: albumId,
|
AlbumID: albumId,
|
||||||
TrackID: trackId,
|
TrackID: trackId,
|
||||||
|
|
|
||||||
|
|
@ -122,6 +122,8 @@ type GetItemsOpts struct {
|
||||||
Week int // 1-52
|
Week int // 1-52
|
||||||
Month int // 1-12
|
Month int // 1-12
|
||||||
Year int
|
Year int
|
||||||
|
From int // unix timestamp
|
||||||
|
To int // unix timestamp
|
||||||
|
|
||||||
// Used only for getting top tracks
|
// Used only for getting top tracks
|
||||||
ArtistID int
|
ArtistID int
|
||||||
|
|
|
||||||
|
|
@ -17,14 +17,23 @@ import (
|
||||||
func (d *Psql) GetListensPaginated(ctx context.Context, opts db.GetItemsOpts) (*db.PaginatedResponse[*models.Listen], error) {
|
func (d *Psql) GetListensPaginated(ctx context.Context, opts db.GetItemsOpts) (*db.PaginatedResponse[*models.Listen], error) {
|
||||||
l := logger.FromContext(ctx)
|
l := logger.FromContext(ctx)
|
||||||
offset := (opts.Page - 1) * opts.Limit
|
offset := (opts.Page - 1) * opts.Limit
|
||||||
t1, t2, err := utils.DateRange(opts.Week, opts.Month, opts.Year)
|
var t1 time.Time
|
||||||
if err != nil {
|
var t2 time.Time
|
||||||
return nil, fmt.Errorf("GetListensPaginated: %w", err)
|
if opts.From != 0 && opts.To != 0 {
|
||||||
}
|
t1 = time.Unix(int64(opts.From), 0)
|
||||||
if opts.Month == 0 && opts.Year == 0 {
|
t2 = time.Unix(int64(opts.To), 0)
|
||||||
// use period, not date range
|
} else {
|
||||||
t2 = time.Now()
|
t1R, t2R, err := utils.DateRange(opts.Week, opts.Month, opts.Year)
|
||||||
t1 = db.StartTimeFromPeriod(opts.Period)
|
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 {
|
if opts.Limit == 0 {
|
||||||
opts.Limit = DefaultItemsPerPage
|
opts.Limit = DefaultItemsPerPage
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue