fathom/pkg/datastore/users.go

50 lines
1.0 KiB
Go
Raw Normal View History

2017-01-25 16:11:35 +00:00
package datastore
import (
2018-04-25 11:45:21 +00:00
"database/sql"
"github.com/usefathom/fathom/pkg/models"
2017-01-25 16:11:35 +00:00
)
// GetUser retrieves user from datastore by its ID
2018-04-25 11:45:21 +00:00
func GetUser(ID int64) (*models.User, error) {
u := &models.User{}
err := dbx.Get(u, dbx.Rebind("SELECT * FROM users WHERE id = ? LIMIT 1"), ID)
if err != nil {
2018-04-25 11:45:21 +00:00
if err == sql.ErrNoRows {
return nil, ErrNoResults
}
return nil, err
}
2018-04-25 11:45:21 +00:00
return u, err
2017-01-25 16:11:35 +00:00
}
// GetUserByEmail retrieves user from datastore by its email
2017-01-25 16:11:35 +00:00
func GetUserByEmail(email string) (*models.User, error) {
2018-04-25 11:45:21 +00:00
u := &models.User{}
err := dbx.Get(u, dbx.Rebind("SELECT * FROM users WHERE email = ? LIMIT 1"), email)
if err != nil {
2018-04-25 11:45:21 +00:00
if err == sql.ErrNoRows {
return nil, ErrNoResults
}
return nil, err
}
2018-04-25 11:45:21 +00:00
return u, err
2017-01-25 16:11:35 +00:00
}
// SaveUser inserts the user model in the connected database
func SaveUser(u *models.User) error {
var sql = dbx.Rebind("INSERT INTO users(email, password) VALUES(?, ?)")
result, err := dbx.Exec(sql, u.Email, u.Password)
if err != nil {
return err
}
u.ID, _ = result.LastInsertId()
return nil
}