aboutsummaryrefslogtreecommitdiffstats
path: root/data.go
diff options
context:
space:
mode:
Diffstat (limited to 'data.go')
-rw-r--r--data.go59
1 files changed, 57 insertions, 2 deletions
diff --git a/data.go b/data.go
index 9e5f5d5..3ece80f 100644
--- a/data.go
+++ b/data.go
@@ -40,11 +40,17 @@ type Session struct {
type DataStore interface {
PutSession(*Session) error
GetSession(key string) (*Session, error)
- GetUser(lfmName string) (int, string, error)
PutScrobbles([]Scrobble) error
RecentScrobbles(userId int) []*Scrobble
GetSongId(artist, album, name string) (int, error)
InsertSong(s *Scrobble) (int, error)
+
+ GetScrobblingUser(lfmName string) (int, string, error)
+ GetUser(u *User) error
+ InsertUser(u *User) error
+ SaveUser(u *User) error
+
+ InsertUserSession(s *UserSession) error
Api
}
@@ -71,7 +77,7 @@ func (store *SqlStore) GetSession(key string) (*Session, error) {
return s, err
}
-func (store *SqlStore) GetUser(lfmName string) (int, string, error) {
+func (store *SqlStore) GetScrobblingUser(lfmName string) (int, string, error) {
var password string
var userId int
row := store.QueryRow("SELECT user_id, lfm_password FROM users WHERE lfm_name = $1",
@@ -153,3 +159,52 @@ func (store *SqlStore) InsertSong(s *Scrobble) (int, error) {
err := row.Scan(&id)
return id, err
}
+
+func (store *SqlStore) InsertUser(user *User) error {
+ query := `
+ INSERT into users (type, op_id, name, email)
+ VALUES ($1, $2, $3, $4) RETURNING user_id`
+ row := store.QueryRow(query, user.Type, user.OpId, user.Name, user.Email)
+ return row.Scan(&user.Id)
+}
+
+func (store *SqlStore) GetUser(user *User) error {
+ var query string
+ var row *sql.Row
+ if user.Id == 0 {
+ query = `
+ SELECT user_id, name, email, lfm_name
+ FROM users WHERE type=$1 AND op_id=$2`
+ row = store.QueryRow(query, user.Type, user.OpId)
+ } else {
+ query = `
+ SELECT user_id, name, email, lfm_name
+ FROM users WHERE user_id=$1`
+ row = store.QueryRow(query, user.Id)
+ }
+ return row.Scan(&user.Id, &user.Name, &user.Email, &user.LfmName)
+}
+
+func (store *SqlStore) SaveUser(user *User) error {
+ var err error
+ var query string
+ if user.LfmPassword != "" {
+ query = `
+ UPDATE users SET name=$1, email=$2, lfm_name=$3, lfm_password=$4
+ WHERE user_id=$5`
+ _, err = store.Exec(query, user.Name, user.Email, user.LfmName,
+ user.LfmPassword, user.Id)
+ } else {
+ query = `
+ UPDATE users SET name=$1, email=$2, lfm_name=$3 WHERE user_id=$5`
+ _, err = store.Exec(query, user.Name, user.Email, user.LfmName, user.Id)
+
+ }
+ return err
+}
+
+func (store *SqlStore) InsertUserSession(s *UserSession) error {
+ query := `INSERT into user_sessions values ($1, $2)`
+ _, err := store.Exec(query, &s.Id, &s.UserId)
+ return err
+}