diff options
| author | Guillaume Horel <guillaume.horel@gmail.com> | 2017-06-25 18:42:33 -0400 |
|---|---|---|
| committer | Guillaume Horel <guillaume.horel@gmail.com> | 2017-06-25 18:42:33 -0400 |
| commit | 682bba726dca2e33317876c459f6334ddf5f060a (patch) | |
| tree | f60d5e5ba56f8d835d0ada9f4ad04b3610d850bb | |
| parent | 696c3dce5b184ae8c6effec4e7024cf56bf1b1ae (diff) | |
| download | lastfm-api-682bba726dca2e33317876c459f6334ddf5f060a.tar.gz | |
add tokens table
| -rw-r--r-- | data.go | 23 | ||||
| -rw-r--r-- | migrations/20170625150613_token.sql | 14 |
2 files changed, 37 insertions, 0 deletions
@@ -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 diff --git a/migrations/20170625150613_token.sql b/migrations/20170625150613_token.sql new file mode 100644 index 0000000..fa56fa2 --- /dev/null +++ b/migrations/20170625150613_token.sql @@ -0,0 +1,14 @@ + +-- +goose Up +-- SQL in section 'Up' is executed when this migration is applied + +CREATE TABLE tokens( + token text PRIMARY KEY, + created timestamp WITH time zone DEFAULT now(), + user_id integer REFERENCES users(user_id) +); + + +-- +goose Down +-- SQL section 'Down' is executed when this migration is rolled back +DROP TABLE tokens; |
