diff options
| author | Thibaut Horel <thibaut.horel@gmail.com> | 2017-06-14 00:45:42 -0400 |
|---|---|---|
| committer | Thibaut Horel <thibaut.horel@gmail.com> | 2017-06-14 00:57:04 -0400 |
| commit | 060b315f74f437fe36175c4ddb52379d29808d64 (patch) | |
| tree | f3e0bd83433f3d918927771e60a9369fbcc320a2 /data.go | |
| parent | 2e190b1af96b0836684339bfb4353a17cf28f0b9 (diff) | |
| download | lastfm-api-060b315f74f437fe36175c4ddb52379d29808d64.tar.gz | |
Now Playing feature
Diffstat (limited to 'data.go')
| -rw-r--r-- | data.go | 32 |
1 files changed, 32 insertions, 0 deletions
@@ -39,6 +39,8 @@ type DataStore interface { PutSession(*Session) error GetSession(key string) (*Session, error) PutScrobbles([]Scrobble) error + PutNowPlaying(s Scrobble) error + NowPlaying(userId int) *Scrobble RecentScrobbles(userId int) []*Scrobble GetSongId(artist, album, name string) (int, error) InsertSong(s *Scrobble) (int, error) @@ -111,6 +113,22 @@ func (store *SqlStore) PutScrobbles(scrobbles []Scrobble) error { return tx.Commit() } +func (store *SqlStore) PutNowPlaying(s Scrobble) error { + query := ` + INSERT INTO now_playing + (artist, album_artist, track_name, album, track_number, duration, + mbid, song_id, session_key, user_id) + VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) + ON CONFLICT (user_id) DO UPDATE + set artist=$1, album_artist=$2, track_name=$3, album=$4, track_number=$5, + duration=$6, mbid=$7, song_id=$8, session_key=$9, user_id=$10, + received=current_timestamp` + + _, err := store.Exec(query, s.Artist, s.AlbumArtist, s.TrackName, s.Album, + s.TrackNumber, s.Duration, s.Mbid, s.SongId, s.SessionKey, s.UserId) + return err +} + func (store *SqlStore) RecentScrobbles(userId int) []*Scrobble { scrobbles := make([]*Scrobble, 0, 10) query := ` @@ -134,6 +152,20 @@ func (store *SqlStore) RecentScrobbles(userId int) []*Scrobble { return scrobbles } +func (store *SqlStore) NowPlaying(userId int) *Scrobble { + scrobble := new(Scrobble) + query := ` + SELECT s.artist, s.album, s.track_name, s.received, songs.image, s.duration + FROM now_playing s + LEFT JOIN songs ON songs.song_id = s.song_id + WHERE user_id=$1` + row := store.QueryRow(query, userId) + row.Scan(&scrobble.Artist.Name, &scrobble.Album.Name, + &scrobble.TrackName.Name, &scrobble.Time, &scrobble.Image, + &scrobble.Duration) + return scrobble +} + func (store *SqlStore) GetSongId(artist, album, song string) (int, error) { query := ` SELECT song_id FROM songs |
