diff options
-rw-r--r-- | blavote.go | 34 | ||||
-rw-r--r-- | option.go | 57 | ||||
-rw-r--r-- | user.go | 2 |
3 files changed, 85 insertions, 8 deletions
@@ -14,7 +14,7 @@ var version string func main() { - version = "0.5" + version = "0.6" //Command line arguments var opts struct { @@ -23,6 +23,7 @@ func main() { New string `short:"n" long:"new" description:"Title for a new poll"` Username string `short:"u" long:"username" description:"Username of user adding poll"` Remove int `short:"r" long:"remove" description:"ID of a poll to delete"` + Info int `short:"i" long:"info" description:"Get info and vote stats for a given poll ID"` Args struct { Rest []string @@ -84,15 +85,21 @@ func main() { } else { fmt.Println(err) } - + } else if (opts.Info > 0) { + err = pollInfo(db, opts.Info) } else { - err = vote(db, opts.Username, opts.Args.Rest) + if (len(opts.Args.Rest) > 1) { + err = vote(db, opts.Username, opts.Args.Rest) + + if (err != nil) { + fmt.Println(err) + } + } else { - if (err != nil) { - fmt.Println(err) } } + } func connectDb(name string) (*sqlite3.Conn, error) { @@ -177,3 +184,20 @@ func vote(db *sqlite3.Conn, nick string, options []string) error { return errors.New("You have already voted in this poll") } } + +func pollInfo(db *sqlite3.Conn, id int) error { + + poll , err := getPollFromId(db, id) + fmt.Print(poll.title + " - ") + options, err := getOptionsForPoll(db, poll) + + for _, option := range options { + option, err = getVotesForOption(db, option) + fmt.Print(option.text + " (") + fmt.Print(option.numVotes) + fmt.Print(") ") + } + fmt.Println() + + return err +} @@ -4,9 +4,10 @@ import "github.com/mxk/go-sqlite/sqlite3" import "errors" type Option struct { - id int + id int64 text string pollId int64 + numVotes int64 } func getOptionFromText(db*sqlite3.Conn, text string) (Option, error) { @@ -17,7 +18,7 @@ func getOptionFromText(db*sqlite3.Conn, text string) (Option, error) { row := make(sqlite3.RowMap) for ; err == nil ; err = s.Next() { - var rowid int + var rowid int64 s.Scan(&rowid, row) // Assigns 1st column to rowid, the rest to row option := Option{id:rowid, text:row["text"].(string), pollId:row["poll_id"].(int64)} @@ -27,4 +28,56 @@ func getOptionFromText(db*sqlite3.Conn, text string) (Option, error) { //If we get here there are no matching options return Option{id:0, text:"", pollId:0}, errors.New("No option found matching '" + text + "'") +} + +func getVotesForOption(db*sqlite3.Conn, option Option) (Option, error) { + args := sqlite3.NamedArgs{"$a": option.id, "$b": option.pollId} + sql := "SELECT count(*) FROM votes WHERE option_id = $a AND poll_id = $b" + s, err := db.Query(sql, args) + + for ; err == nil ; err = s.Next() { + var votes int64 + s.Scan(&votes) // Assigns 1st column to rowid, the rest to row + + option = Option{id:option.id, text:option.text, pollId:option.pollId, numVotes:votes} + return option, nil + } + + //If we get here there are no matching options + return Option{id:0, text:"", pollId:0}, errors.New("No option found matching '" + option.text + "'") +} + +func getOptionsForPoll(db*sqlite3.Conn, poll Poll) ([]Option, error) { + args := sqlite3.NamedArgs{"$a": poll.id} + sql := "SELECT count(*) FROM options WHERE poll_id = $a" + s, err := db.Query(sql, args) + + var numOptions int64 + + //Get the number of votes first so we can make the right size array + for ; err == nil ; err = s.Next() { + s.Scan(&numOptions) // Assigns 1st column to rowid, the rest to row + } + + var options = make([]Option, numOptions) + + sql = "SELECT * FROM options WHERE poll_id = $a" + s, err = db.Query(sql, args) + row := make(sqlite3.RowMap) + id := 0 + + for ; err == nil ; err = s.Next() { + var rowid int64 + s.Scan(&rowid, row) // Assigns 1st column to rowid, the rest to row + + option := Option{id:rowid, text:row["text"].(string), pollId:row["poll_id"].(int64)} + options[id] = option + id++ + } + + return options, err + + + //If we get here there are no matching options + // return Option{id:0, text:"", pollId:0}, errors.New("No option found matching '" + option.text + "'") }
\ No newline at end of file @@ -64,4 +64,4 @@ func getUserForName(db *sqlite3.Conn, name string) User { //If we get here there are no matching users return User{id:0, name:"", isAdmin:false} -}
\ No newline at end of file +} |