summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--blavote.go24
-rw-r--r--poll.go22
2 files changed, 46 insertions, 0 deletions
diff --git a/blavote.go b/blavote.go
index 03d2f28..315b74a 100644
--- a/blavote.go
+++ b/blavote.go
@@ -24,6 +24,7 @@ func main() {
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"`
+ List bool `short:"l" long:"list" description:"List recent polls"`
Args struct {
Rest []string
@@ -87,6 +88,11 @@ func main() {
}
} else if (opts.Info > 0) {
err = pollInfo(db, opts.Info)
+ } else if (opts.List) {
+ err = listPolls(db)
+ if (err != nil) {
+ fmt.Println(err)
+ }
} else {
if (len(opts.Args.Rest) > 1) {
err = vote(db, opts.Username, opts.Args.Rest)
@@ -201,3 +207,21 @@ func pollInfo(db *sqlite3.Conn, id int) error {
return err
}
+
+func listPolls(db *sqlite3.Conn) error {
+
+ polls, err := getRecentPolls(db)
+
+ for _, poll := range polls {
+ if (poll.id != 0) {
+ fmt.Print(poll.id)
+ fmt.Print(": " + poll.title + " ")
+ } else {
+ fmt.Println()
+ return nil
+ }
+ }
+
+
+ return err
+} \ No newline at end of file
diff --git a/poll.go b/poll.go
index 0952cd7..8a204f1 100644
--- a/poll.go
+++ b/poll.go
@@ -145,3 +145,25 @@ func hasUserVotedInPoll(db *sqlite3.Conn, poll Poll, user User) bool {
//Not sure why we'd get here, but we probably don't want to record a vote if we did
return true
}
+
+func getRecentPolls(db *sqlite3.Conn) ([]Poll, error) {
+
+ sql := "SELECT * FROM polls LIMIT 5"
+ s, err := db.Query(sql)
+ row := make(sqlite3.RowMap)
+ var polls = make([]Poll, 5)
+ i := 0
+
+ for ; err == nil ; err = s.Next() {
+ var rowid int64
+ 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"].(int64)}
+ polls[i] = poll
+ i++
+ }
+
+ //If we get here there are no matching polls, return an error
+ return polls, err
+
+} \ No newline at end of file