Created
September 3, 2012 19:28
-
-
Save collinvandyck/3612649 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "database/sql" | |
| "fmt" | |
| "time" | |
| ) | |
| type User struct { | |
| Id string | |
| Email string | |
| CreatedAt time.Time | |
| UpdatedAt time.Time | |
| Name string | |
| Admin bool | |
| Active bool | |
| } | |
| func GetUserByApiKey(key string) (*User, error) { | |
| fmt.Printf("Getting user with API key %s\n", key) | |
| db, err := sql.Open("postgres", "...") | |
| if err != nil { | |
| return nil, err | |
| } | |
| defer db.Close() | |
| rows, err := db.Query("select id, email, created_at, updated_at, name, admin, active from users where apikey = $1", key) | |
| if err != nil { | |
| return nil, err | |
| } | |
| 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) | |
| fmt.Printf("Made a user: %#v\n", user) | |
| return user, nil | |
| } | |
| return nil, nil | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment