aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThibaut Horel <thibaut.horel@gmail.com>2017-06-06 19:20:09 -0400
committerThibaut Horel <thibaut.horel@gmail.com>2017-06-06 19:20:09 -0400
commit53841da448baa28067bab046198757954cebc4a1 (patch)
tree514750df12d2391b77d55b124a8a044b0d4998b1
parent81522cbae16c6aadb1b789f3f875dc50f10cc005 (diff)
downloadlastfm-api-53841da448baa28067bab046198757954cebc4a1.tar.gz
Use user_id instead of lfm_name as much as possible
-rw-r--r--apiv1.go5
-rw-r--r--data.go38
-rw-r--r--schema.sql4
-rw-r--r--web.go8
4 files changed, 27 insertions, 28 deletions
diff --git a/apiv1.go b/apiv1.go
index 892c06b..592e7de 100644
--- a/apiv1.go
+++ b/apiv1.go
@@ -36,14 +36,14 @@ func (app *App) mainHandler(w http.ResponseWriter, r *http.Request) {
user := r.FormValue("u")
auth := r.FormValue("a")
- password, err := app.GetPassword(user)
+ userId, password, err := app.GetUser(user)
if (md5hex(password+timestamp) != auth) || err != nil {
fmt.Fprintln(w, "BADAUTH")
return
}
s := &Session{
- User: user,
+ UserId: userId,
Client: r.FormValue("c"),
ClientVersion: r.FormValue("v"),
Key: randomToken(16),
@@ -162,6 +162,7 @@ func parseScrobbles(values url.Values, session *Session) ([]Scrobble, int) {
ignored++
} else {
scrobble.Session = session.Key
+ scrobble.UserId = session.UserId
scrobbles = append(scrobbles, scrobble)
}
}
diff --git a/data.go b/data.go
index 318e43e..30f8a38 100644
--- a/data.go
+++ b/data.go
@@ -21,7 +21,7 @@ type Scrobble struct {
Mbid string `xml:"-" json:"-"`
MbidComp string
Session string `xml:"-" json:"-"`
- User string
+ UserId int
Image string
}
@@ -29,6 +29,7 @@ type Session struct {
XMLName xml.Name `json:"-" xml:"session"`
User string `json:"name" xml:"name"`
Key string `json:"key" xml:"key"`
+ UserId int
Client string
ClientVersion string
Protocol string
@@ -39,9 +40,9 @@ type Session struct {
type DataStore interface {
PutSession(*Session) error
GetSession(key string) (*Session, error)
- GetPassword(userName string) (string, error)
+ GetUser(lfmName string) (int, string, error)
PutScrobbles([]Scrobble) error
- RecentScrobbles(lfmName string) []*Scrobble
+ RecentScrobbles(userId int) []*Scrobble
Api
}
@@ -51,29 +52,30 @@ type SqlStore struct {
func (store *SqlStore) PutSession(s *Session) error {
query := `
- INSERT INTO scrobbling_sessions (lfm_name, session_key, client, client_version, protocol)
+ INSERT INTO scrobbling_sessions (user_id, session_key, client, client_version, protocol)
VALUES ($1, $2, $3, $4, $5)`
- _, err := store.Exec(query, s.User, s.Key, s.Client, s.ClientVersion, s.Protocol)
+ _, err := store.Exec(query, s.UserId, s.Key, s.Client, s.ClientVersion, s.Protocol)
return err
}
func (store *SqlStore) GetSession(key string) (*Session, error) {
query := `
- SELECT lfm_name, session_key, client, client_version, protocol, created
+ SELECT user_id, session_key, client, client_version, protocol, created
FROM scrobbling_sessions WHERE session_key = $1`
row := store.QueryRow(query, key)
- s := &Session{}
- err := row.Scan(&s.User, &s.Key, &s.Client, &s.ClientVersion, &s.Protocol,
+ s := new(Session)
+ err := row.Scan(&s.UserId, &s.Key, &s.Client, &s.ClientVersion, &s.Protocol,
&s.Created)
return s, err
}
-func (store *SqlStore) GetPassword(name string) (string, error) {
+func (store *SqlStore) GetUser(lfmName string) (int, string, error) {
var password string
- row := store.QueryRow("SELECT lfm_password FROM users WHERE lfm_name = $1",
- name)
- err := row.Scan(&password)
- return password, err
+ var userId int
+ row := store.QueryRow("SELECT user_id, lfm_password FROM users WHERE lfm_name = $1",
+ lfmName)
+ err := row.Scan(&userId, &password)
+ return userId, password, err
}
func (store *SqlStore) PutScrobbles(scrobbles []Scrobble) error {
@@ -84,7 +86,7 @@ func (store *SqlStore) PutScrobbles(scrobbles []Scrobble) error {
query := `
INSERT INTO scrobbles
(artist, albumartist, trackname, album, tracknumber, duration, time,
- chosen, mbid, mbid_computed, session_key, lfm_name)
+ chosen, mbid, mbid_computed, session_key, user_id)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)`
st, err := tx.Prepare(query)
if err != nil {
@@ -93,7 +95,7 @@ func (store *SqlStore) PutScrobbles(scrobbles []Scrobble) error {
for _, s := range scrobbles {
_, err = st.Exec(s.Artist, s.AlbumArtist, s.TrackName, s.Album,
s.TrackNumber, s.Duration, s.Time, s.Chosen, s.Mbid, s.MbidComp,
- s.Session, s.User)
+ s.Session, s.UserId)
if err != nil {
tx.Rollback()
return err
@@ -102,14 +104,14 @@ func (store *SqlStore) PutScrobbles(scrobbles []Scrobble) error {
return tx.Commit()
}
-func (store *SqlStore) RecentScrobbles(lfmName string) []*Scrobble {
+func (store *SqlStore) RecentScrobbles(userId int) []*Scrobble {
scrobbles := make([]*Scrobble, 0, 10)
query := `
SELECT artist, album, trackname, time, image
FROM scrobbles
LEFT JOIN songs ON COALESCE(scrobbles.mbid, scrobbles.mbid_computed) = songs.mbid
- WHERE lfm_name=$1 ORDER BY time DESC LIMIT 10`
- rows, err := store.Query(query, lfmName)
+ WHERE user_id=$1 ORDER BY time DESC LIMIT 10`
+ rows, err := store.Query(query, userId)
if err != nil {
log.Println(err)
}
diff --git a/schema.sql b/schema.sql
index 475c46d..f875137 100644
--- a/schema.sql
+++ b/schema.sql
@@ -11,7 +11,7 @@ CREATE TABLE IF NOT EXISTS users (
CREATE TABLE IF NOT EXISTS scrobbling_sessions (
session_key text PRIMARY KEY,
- lfm_name text REFERENCES users(lfm_name),
+ user_id int REFERENCES users,
client text,
client_version text,
protocol text,
@@ -29,7 +29,7 @@ CREATE TABLE IF NOT EXISTS scrobbles (
mbid text,
mbid_computed text,
session_key text REFERENCES scrobbling_sessions,
- lfm_name text REFERENCES users(lfm_name)
+ user_id int REFERENCES users
);
CREATE TABLE IF NOT EXISTS user_sessions (
diff --git a/web.go b/web.go
index c9d27b1..032e04a 100644
--- a/web.go
+++ b/web.go
@@ -20,7 +20,7 @@ type UserInfo struct {
type UserSession struct {
Id string
- UserId int64
+ UserId int
UserName string
}
@@ -43,11 +43,7 @@ func (app *App) root(w http.ResponseWriter, r *http.Request) {
return
}
- var lfmName string
- row := app.DB.QueryRow("SELECT lfm_name FROM users WHERE user_id=$1",
- se.UserId)
- row.Scan(&lfmName)
- scrobbles := app.RecentScrobbles(lfmName)
+ scrobbles := app.RecentScrobbles(se.UserId)
app.Template.ExecuteTemplate(w, "index.tmpl", struct {
Session *UserSession