From 9c64140e68d236b6b8bbe90157b60cb9eb35506d Mon Sep 17 00:00:00 2001 From: Joe Robinson Date: Sun, 20 Jul 2014 16:13:16 +0100 Subject: Added poll removal option --- blavote.go | 13 ++++++++++++- poll.go | 37 +++++++++++++++++++++++++++++++++++-- 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/blavote.go b/blavote.go index a27742a..48e1ae2 100644 --- a/blavote.go +++ b/blavote.go @@ -13,7 +13,7 @@ var version string func main() { - version = "0.1" + version = "0.2" //Command line arguments var opts struct { @@ -21,6 +21,7 @@ func main() { Add string `short:"a" long:"add" description:"Title for a new poll"` 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"` Args struct { Rest []string @@ -73,6 +74,16 @@ func main() { } else { fmt.Println(err) } + } else if (opts.Remove > 0) { + deletePoll(db, opts.Remove) + + if (err == nil) { + fmt.Print("Poll removed with ID ") + fmt.Println(opts.Remove) + } else { + fmt.Println(err) + } + } else { fmt.Println(opts.Args.Rest) vote(db, opts.Username, opts.Args.Rest) diff --git a/poll.go b/poll.go index 83d1281..9ca9aeb 100644 --- a/poll.go +++ b/poll.go @@ -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 -- cgit v1.2.3