diff options
author | Joe Robinson <joe@mumsnet.com> | 2014-07-18 16:26:58 +0100 |
---|---|---|
committer | Joe Robinson <joe@mumsnet.com> | 2014-07-18 16:26:58 +0100 |
commit | 0d0048d532e6b738653d3c4cd6bde36dcb1d6728 (patch) | |
tree | 32bd7a103b6f6b1fe24cc8067c618ab5d8a0dd9c | |
parent | a971bc1040a236b0ae54d65c7e21565a638ffc49 (diff) |
Added initial user management functions
-rw-r--r-- | blavote.go | 12 | ||||
-rw-r--r-- | user.go | 42 |
2 files changed, 50 insertions, 4 deletions
@@ -1,5 +1,6 @@ package main +// import "blavote" import "fmt" import "github.com/mxk/go-sqlite/sqlite3" import "github.com/jessevdk/go-flags" @@ -55,6 +56,9 @@ func main() { addPoll(db, opts.Args.Title, opts.Args.Rest) } + createUser(db, "l_bratch", true) + getUserForId(db, 1) + } func connectDb(name string) (*sqlite3.Conn, error) { @@ -83,10 +87,10 @@ func initTables(db *sqlite3.Conn) { db.Exec("create table info(id int, key text, value text)") db.Exec("insert into info (key, value) values('version', '0.01')") - db.Exec("create table users(id int, name text, admin boolean)") - db.Exec("create table polls(id int, text text, user_id int)") - db.Exec("create table options(id int, text text, poll_id int)") - db.Exec("create table votes(id int, user_id int, poll_id int, option_id int)") + db.Exec("create table users(id integer primary key autoincrement, name text, admin boolean)") + db.Exec("create table polls(id integer primary key autoincrement, text text, user_id int)") + db.Exec("create table options(id integer primary key autoincrement, text text, poll_id int)") + db.Exec("create table votes(id integer primary key autoincrement, user_id int, poll_id int, option_id int)") } @@ -0,0 +1,42 @@ +package main + +import "fmt" +import "github.com/mxk/go-sqlite/sqlite3" + +type User struct { + id int + name string + isAdmin bool +} + +func createUser(db *sqlite3.Conn, name string, isAdmin bool) { + + args := sqlite3.NamedArgs{"$a": name, "$b": isAdmin} + sql := "INSERT INTO users (name, admin) VALUES ($a, $b)" + + db.Exec(sql, args) + + +} +func getUserForId(db *sqlite3.Conn, id int) User { + + args := sqlite3.NamedArgs{"$a": id} + sql := "SELECT * FROM users WHERE id = $a" + s, err := db.Query(sql, args) + row := make(sqlite3.RowMap) + + for ; err == nil ; err = s.Next() { + var rowid int + s.Scan(&rowid, row) // Assigns 1st column to rowid, the rest to row + fmt.Println(rowid, row) // Prints "1 map[a:1 b:demo c:<nil>]" + fmt.Println(row["name"]) + + user := User{id:rowid, name:row["name"].(string), isAdmin:row["admin"].(bool)} + return user + } + + //If we get here there are no matching users + return User{id:0, name:"", isAdmin:false} + + +}
\ No newline at end of file |