aboutsummaryrefslogtreecommitdiffstats
path: root/data.go
diff options
context:
space:
mode:
authorGuillaume Horel <guillaume.horel@gmail.com>2017-06-25 18:42:33 -0400
committerGuillaume Horel <guillaume.horel@gmail.com>2017-06-25 18:42:33 -0400
commit682bba726dca2e33317876c459f6334ddf5f060a (patch)
treef60d5e5ba56f8d835d0ada9f4ad04b3610d850bb /data.go
parent696c3dce5b184ae8c6effec4e7024cf56bf1b1ae (diff)
downloadlastfm-api-682bba726dca2e33317876c459f6334ddf5f060a.tar.gz
add tokens table
Diffstat (limited to 'data.go')
-rw-r--r--data.go23
1 files changed, 23 insertions, 0 deletions
diff --git a/data.go b/data.go
index ba3157b..186e6a2 100644
--- a/data.go
+++ b/data.go
@@ -71,6 +71,9 @@ type DataStore interface {
PutSession(*Session) error
GetSession(key string) (*Session, error)
GetClient(key string) (*Client, error)
+ GetToken(token string) (*Token, error)
+ NewToken() (string, error)
+ PutToken(token *Token) error
PutScrobbles([]Scrobble) error
PutNowPlaying(s Scrobble) error
NowPlaying(userId int) *Scrobble
@@ -129,6 +132,26 @@ func (store *SqlStore) GetClient(key string) (*Client, error) {
return c, err
}
+func (store *SqlStore) NewToken() (string, error) {
+ token := randomToken(16)
+ query := `INSERT INTO tokens(token) VALUES($1)`
+ _, err := store.Exec(query, token)
+ return token, err
+}
+
+func (store *SqlStore) PutToken(token *Token) error {
+ query := "UPDATE tokens SET user_id=$1 WHERE token=$2"
+ _, err := store.Exec(query, token.UserId, token.Val)
+ return err
+}
+
+func (store *SqlStore) GetToken(token string) (*Token, error) {
+ row := store.QueryRow(`SELECT * FROM tokens WHERE token=$1`, token)
+ t := new(Token)
+ err := row.Scan(&t.Val, &t.Created, &t.UserId)
+ return t, err
+}
+
func (store *SqlStore) GetScrobblingUser(lfmName string) (int, string, error) {
var password string
var userId int