Skip to content

Instantly share code, notes, and snippets.

@collinvandyck
Created September 12, 2012 19:09
Show Gist options
  • Select an option

  • Save collinvandyck/3709176 to your computer and use it in GitHub Desktop.

Select an option

Save collinvandyck/3709176 to your computer and use it in GitHub Desktop.
type User struct {
Id string `json:"id"`
Email string `json:"email,omitempty"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Name string `json:"name,omitempty"`
Admin bool `json:"admin"`
Active bool `json:"-"`
}
func GetUserByApiKey(db *sql.DB, key Credentials) (*User, error) {
rows, err := db.Query("select id, email, created_at, updated_at, name, admin, active from users where apikey = $1", string(key))
if err != nil {
return nil, err
}
defer rows.Close()
if rows.Next() {
user := new(User)
var id []byte
var email []byte
var name []byte
err = rows.Scan(&id, &email, &user.CreatedAt, &user.UpdatedAt, &name, &user.Admin, &user.Active)
if err != nil {
return nil, err
}
user.Id = string(id)
user.Email = string(email)
user.Name = string(name)
return user, nil
}
return nil, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment