diff options
author | Joe Robinson <joe@mumsnet.com> | 2014-07-20 16:13:16 +0100 |
---|---|---|
committer | Joe Robinson <joe@mumsnet.com> | 2014-07-20 16:13:16 +0100 |
commit | 9c64140e68d236b6b8bbe90157b60cb9eb35506d (patch) | |
tree | 0c0c9832f3219452c63dafaee36d1100f76163c5 /poll.go | |
parent | 2b2d6732707ff24a50f672466f8b9fbb40f90653 (diff) |
Added poll removal option
Diffstat (limited to 'poll.go')
-rw-r--r-- | poll.go | 37 |
1 files changed, 35 insertions, 2 deletions
@@ -5,7 +5,7 @@ import "github.com/mxk/go-sqlite/sqlite3" type Poll struct { id int title string - userId int + userId int64 } func addPoll(db *sqlite3.Conn, title string, options []string, nick string) (int64, error) { @@ -63,7 +63,7 @@ func getPollFromTitle(db *sqlite3.Conn, title string) Poll { 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)} + poll := Poll{id:rowid, title:row["title"].(string), userId:row["user_id"].(int64)} return poll } @@ -72,3 +72,36 @@ func getPollFromTitle(db *sqlite3.Conn, title string) Poll { } +func getPollFromId(db *sqlite3.Conn, id int) Poll { + + args := sqlite3.NamedArgs{"$a": id} + sql := "SELECT * FROM polls 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 + + poll := Poll{id:rowid, title:row["title"].(string), userId:row["user_id"].(int64)} + return poll + } + + //If we get here there are no matching users + return Poll{id:0, title:"", userId:0} + +} + +func deletePoll(db *sqlite3.Conn, id int) error { + + poll := getPollFromId(db, id) + + if (poll.id == 0) { + return nil + } else { + sql := "DELETE FROM polls WHERE id = $a" + args := sqlite3.NamedArgs{"$a": id} + db.Exec(sql, args) + return nil + } +}
\ No newline at end of file |