From 3345ce304365a577aa7d3f86e1b136b906610f1f Mon Sep 17 00:00:00 2001 From: Thibaut Horel Date: Fri, 28 Dec 2018 20:12:34 -0500 Subject: First working commit --- store.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'store.go') diff --git a/store.go b/store.go index 1c8348f..9723b66 100644 --- a/store.go +++ b/store.go @@ -20,6 +20,7 @@ type User struct { type Store interface { GetSession(id string) (*Session, bool) + NewSession(userId int64) *Session GetUser(name string) (*User, bool) } @@ -42,7 +43,7 @@ func (store *PgStore) GetSession(id string) (*Session, bool) { return s, true } s = new(Session) - row := store.QueryRow("SELECT id, user_id FROM session WHERE id = $1", id) + row := store.QueryRow("SELECT id, user_id FROM sessions WHERE id = $1", id) if err := row.Scan(&s.Id, &s.UserId); err != nil { return nil, false } @@ -50,9 +51,17 @@ func (store *PgStore) GetSession(id string) (*Session, bool) { return s, true } +func (store *PgStore) NewSession(userId int64) *Session { + var id string + store.QueryRow("INSERT INTO sessions(user_id) VALUES ($1) RETURNING id", userId).Scan(&id) + s := &Session{id, userId} + store.sessionCache[s.Id] = s + return s +} + func (store *PgStore) GetUser(name string) (*User, bool) { u := &User{Name: name} - row := store.QueryRow("SELECT id, password FROM user WHERE name = $1", name) + row := store.QueryRow("SELECT id, password FROM users WHERE name = $1", name) if err := row.Scan(&u.Id, &u.Password); err != nil { return nil, false } -- cgit v1.2.3-70-g09d2