mirror of
https://github.com/gabehf/Koito.git
synced 2026-03-13 09:30:27 -07:00
feat: add first listened to dates for media items (#92)
This commit is contained in:
parent
300bac0e19
commit
800c77d05e
12 changed files with 134 additions and 2 deletions
|
|
@ -98,8 +98,14 @@ func (d *Psql) GetAlbum(ctx context.Context, opts db.GetAlbumOpts) (*models.Albu
|
|||
return nil, fmt.Errorf("GetAlbum: CountTimeListenedToItem: %w", err)
|
||||
}
|
||||
|
||||
firstListen, err := d.q.GetFirstListenFromRelease(ctx, ret.ID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("GetAlbum: GetFirstListenFromRelease: %w", err)
|
||||
}
|
||||
|
||||
ret.ListenCount = count
|
||||
ret.TimeListened = seconds
|
||||
ret.FirstListen = firstListen.ListenedAt.Unix()
|
||||
|
||||
return ret, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -41,6 +41,10 @@ func (d *Psql) GetArtist(ctx context.Context, opts db.GetArtistOpts) (*models.Ar
|
|||
if err != nil {
|
||||
return nil, fmt.Errorf("GetArtist: CountTimeListenedToItem: %w", err)
|
||||
}
|
||||
firstListen, err := d.q.GetFirstListenFromArtist(ctx, row.ID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("GetAlbum: GetFirstListenFromArtist: %w", err)
|
||||
}
|
||||
return &models.Artist{
|
||||
ID: row.ID,
|
||||
MbzID: row.MusicBrainzID,
|
||||
|
|
@ -49,6 +53,7 @@ func (d *Psql) GetArtist(ctx context.Context, opts db.GetArtistOpts) (*models.Ar
|
|||
Image: row.Image,
|
||||
ListenCount: count,
|
||||
TimeListened: seconds,
|
||||
FirstListen: firstListen.ListenedAt.Unix(),
|
||||
}, nil
|
||||
} else if opts.MusicBrainzID != uuid.Nil {
|
||||
l.Debug().Msgf("Fetching artist from DB with MusicBrainz ID %s", opts.MusicBrainzID)
|
||||
|
|
@ -71,14 +76,19 @@ func (d *Psql) GetArtist(ctx context.Context, opts db.GetArtistOpts) (*models.Ar
|
|||
if err != nil {
|
||||
return nil, fmt.Errorf("GetArtist: CountTimeListenedToItem: %w", err)
|
||||
}
|
||||
firstListen, err := d.q.GetFirstListenFromArtist(ctx, row.ID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("GetAlbum: GetFirstListenFromArtist: %w", err)
|
||||
}
|
||||
return &models.Artist{
|
||||
ID: row.ID,
|
||||
MbzID: row.MusicBrainzID,
|
||||
Name: row.Name,
|
||||
Aliases: row.Aliases,
|
||||
Image: row.Image,
|
||||
TimeListened: seconds,
|
||||
ListenCount: count,
|
||||
TimeListened: seconds,
|
||||
FirstListen: firstListen.ListenedAt.Unix(),
|
||||
}, nil
|
||||
} else if opts.Name != "" {
|
||||
l.Debug().Msgf("Fetching artist from DB with name '%s'", opts.Name)
|
||||
|
|
@ -101,6 +111,10 @@ func (d *Psql) GetArtist(ctx context.Context, opts db.GetArtistOpts) (*models.Ar
|
|||
if err != nil {
|
||||
return nil, fmt.Errorf("GetArtist: CountTimeListenedToItem: %w", err)
|
||||
}
|
||||
firstListen, err := d.q.GetFirstListenFromArtist(ctx, row.ID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("GetAlbum: GetFirstListenFromArtist: %w", err)
|
||||
}
|
||||
return &models.Artist{
|
||||
ID: row.ID,
|
||||
MbzID: row.MusicBrainzID,
|
||||
|
|
@ -109,6 +123,7 @@ func (d *Psql) GetArtist(ctx context.Context, opts db.GetArtistOpts) (*models.Ar
|
|||
Image: row.Image,
|
||||
ListenCount: count,
|
||||
TimeListened: seconds,
|
||||
FirstListen: firstListen.ListenedAt.Unix(),
|
||||
}, nil
|
||||
} else {
|
||||
return nil, errors.New("insufficient information to get artist")
|
||||
|
|
|
|||
|
|
@ -89,8 +89,14 @@ func (d *Psql) GetTrack(ctx context.Context, opts db.GetTrackOpts) (*models.Trac
|
|||
return nil, fmt.Errorf("GetTrack: CountTimeListenedToItem: %w", err)
|
||||
}
|
||||
|
||||
firstListen, err := d.q.GetFirstListenFromTrack(ctx, track.ID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("GetAlbum: GetFirstListenFromRelease: %w", err)
|
||||
}
|
||||
|
||||
track.ListenCount = count
|
||||
track.TimeListened = seconds
|
||||
track.FirstListen = firstListen.ListenedAt.Unix()
|
||||
|
||||
return &track, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ type Album struct {
|
|||
VariousArtists bool `json:"is_various_artists"`
|
||||
ListenCount int64 `json:"listen_count"`
|
||||
TimeListened int64 `json:"time_listened"`
|
||||
FirstListen int64 `json:"first_listen"`
|
||||
}
|
||||
|
||||
// type SimpleAlbum struct {
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ type Artist struct {
|
|||
Image *uuid.UUID `json:"image"`
|
||||
ListenCount int64 `json:"listen_count"`
|
||||
TimeListened int64 `json:"time_listened"`
|
||||
FirstListen int64 `json:"first_listen"`
|
||||
IsPrimary bool `json:"is_primary,omitempty"`
|
||||
}
|
||||
|
||||
|
|
@ -27,5 +28,6 @@ type ArtistWithFullAliases struct {
|
|||
ImageSource string `json:"image_source,omitempty"`
|
||||
ListenCount int64 `json:"listen_count"`
|
||||
TimeListened int64 `json:"time_listened"`
|
||||
FirstListen int64 `json:"first_listen"`
|
||||
IsPrimary bool `json:"is_primary,omitempty"`
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,4 +12,5 @@ type Track struct {
|
|||
Image *uuid.UUID `json:"image"`
|
||||
AlbumID int32 `json:"album_id"`
|
||||
TimeListened int64 `json:"time_listened"`
|
||||
FirstListen int64 `json:"first_listen"`
|
||||
}
|
||||
|
|
|
|||
|
|
@ -190,6 +190,73 @@ func (q *Queries) DeleteListen(ctx context.Context, arg DeleteListenParams) erro
|
|||
return err
|
||||
}
|
||||
|
||||
const getFirstListenFromArtist = `-- name: GetFirstListenFromArtist :one
|
||||
SELECT
|
||||
l.track_id, l.listened_at, l.client, l.user_id
|
||||
FROM listens l
|
||||
JOIN tracks_with_title t ON l.track_id = t.id
|
||||
JOIN artist_tracks at ON t.id = at.track_id
|
||||
WHERE at.artist_id = $1
|
||||
ORDER BY l.listened_at ASC
|
||||
LIMIT 1
|
||||
`
|
||||
|
||||
func (q *Queries) GetFirstListenFromArtist(ctx context.Context, artistID int32) (Listen, error) {
|
||||
row := q.db.QueryRow(ctx, getFirstListenFromArtist, artistID)
|
||||
var i Listen
|
||||
err := row.Scan(
|
||||
&i.TrackID,
|
||||
&i.ListenedAt,
|
||||
&i.Client,
|
||||
&i.UserID,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getFirstListenFromRelease = `-- name: GetFirstListenFromRelease :one
|
||||
SELECT
|
||||
l.track_id, l.listened_at, l.client, l.user_id
|
||||
FROM listens l
|
||||
JOIN tracks t ON l.track_id = t.id
|
||||
WHERE t.release_id = $1
|
||||
ORDER BY l.listened_at ASC
|
||||
LIMIT 1
|
||||
`
|
||||
|
||||
func (q *Queries) GetFirstListenFromRelease(ctx context.Context, releaseID int32) (Listen, error) {
|
||||
row := q.db.QueryRow(ctx, getFirstListenFromRelease, releaseID)
|
||||
var i Listen
|
||||
err := row.Scan(
|
||||
&i.TrackID,
|
||||
&i.ListenedAt,
|
||||
&i.Client,
|
||||
&i.UserID,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getFirstListenFromTrack = `-- name: GetFirstListenFromTrack :one
|
||||
SELECT
|
||||
l.track_id, l.listened_at, l.client, l.user_id
|
||||
FROM listens l
|
||||
JOIN tracks t ON l.track_id = t.id
|
||||
WHERE t.id = $1
|
||||
ORDER BY l.listened_at ASC
|
||||
LIMIT 1
|
||||
`
|
||||
|
||||
func (q *Queries) GetFirstListenFromTrack(ctx context.Context, id int32) (Listen, error) {
|
||||
row := q.db.QueryRow(ctx, getFirstListenFromTrack, id)
|
||||
var i Listen
|
||||
err := row.Scan(
|
||||
&i.TrackID,
|
||||
&i.ListenedAt,
|
||||
&i.Client,
|
||||
&i.UserID,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getLastListensFromArtistPaginated = `-- name: GetLastListensFromArtistPaginated :many
|
||||
SELECT
|
||||
l.track_id, l.listened_at, l.client, l.user_id,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue