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 | |
parent | 2b2d6732707ff24a50f672466f8b9fbb40f90653 (diff) |
Added poll removal option
-rw-r--r-- | blavote.go | 13 | ||||
-rw-r--r-- | poll.go | 37 |
2 files changed, 47 insertions, 3 deletions
@@ -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) @@ -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 |