From 0d7a802091a40a7304a0c3d1113c0cafd6556365 Mon Sep 17 00:00:00 2001 From: Joe Robinson Date: Fri, 25 Jul 2014 13:06:21 +0100 Subject: Added function to list recent polls --- poll.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'poll.go') 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 -- cgit v1.2.3