diff options
author | Joe Robinson <joe@mumsnet.com> | 2014-07-18 17:29:32 +0100 |
---|---|---|
committer | Joe Robinson <joe@mumsnet.com> | 2014-07-18 17:29:32 +0100 |
commit | 0913485975baf0a5db4939ab3112ed7b8883d0f1 (patch) | |
tree | 126ca3da189cd0e3ec2cd477ec53fab8a3ea0528 | |
parent | df0a8c9ef163016cf6a6530fb6ac6eddd6b691c5 (diff) |
Added functions to add new polls
-rw-r--r-- | blavote.go | 18 | ||||
-rw-r--r-- | poll.go | 73 |
2 files changed, 82 insertions, 9 deletions
@@ -19,8 +19,10 @@ func main() { Version bool `short:"v" long:"version" description:"Show program version"` Add bool `short:"a" long:"add" description:"Add a new poll"` New bool `short:"n" long:"new" description:"Add a new poll"` + Username string `short:"u" long:"username" description:"Username of user adding poll"` Args struct { Title string + PollId int Rest []string } `positional-args:"yes"` } @@ -53,11 +55,14 @@ func main() { if (opts.Version) { fmt.Println("v" + version) } else if (opts.Add || opts.New) { - addPoll(db, opts.Args.Title, opts.Args.Rest) + err := addPoll(db, opts.Args.Title, opts.Args.Rest, opts.Username) + if (err == nil) { + fmt.Println("Poll added") + } else { + fmt.Println(err) + } } - createUser(db, "l_bratch", true) - getUserForId(db, 1) } @@ -88,14 +93,9 @@ func initTables(db *sqlite3.Conn) { db.Exec("insert into info (key, value) values('version', '0.01')") 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 polls(id integer primary key autoincrement, title 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)") } - -func addPoll(db *sqlite3.Conn, title string, options []string) { - - -}
\ No newline at end of file @@ -0,0 +1,73 @@ +package main + +import "github.com/mxk/go-sqlite/sqlite3" + +type Poll struct { + id int + title string + userId int +} + +func addPoll(db *sqlite3.Conn, title string, options []string, nick string) error { + + user := getUserForName(db, nick) + + if (user.id == 0) { + createUser(db, nick, false) + user = getUserForName(db, nick) + } + + args := sqlite3.NamedArgs{"$a": title, "$b": user.id} + sql := "INSERT INTO polls (title, user_id) VALUES ($a, $b)" + + err := db.Exec(sql, args) + + if (err != nil ) { + return err + } + + pollId := db.LastInsertId() + for _,option := range options { + addOption(db, option, pollId) + if (err != nil ) { + return err + } + } + + return nil + +} + +func addOption(db *sqlite3.Conn, text string, pollId int64) error { + + args := sqlite3.NamedArgs{"$a": text, "$b": pollId} + sql := "INSERT INTO options (text, poll_id) VALUES ($a, $b)" + + err := db.Exec(sql, args) + + if (err != nil ) { + return err + } + + return nil +} + +func getPollFromTitle(db *sqlite3.Conn, title string) Poll { + + args := sqlite3.NamedArgs{"$a": title} + sql := "SELECT * FROM polls WHERE title = $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 + + poll := Poll{id:rowid, title:row["title"].(string), userId:row["user_id"].(int)} + return poll + } + + //If we get here there are no matching users + return Poll{id:0, title:"", userId:0} + +}
\ No newline at end of file |