summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--blavote.go51
-rw-r--r--option.go29
-rw-r--r--poll.go11
3 files changed, 77 insertions, 14 deletions
diff --git a/blavote.go b/blavote.go
index 85e9c5e..a27742a 100644
--- a/blavote.go
+++ b/blavote.go
@@ -7,23 +7,24 @@ import "github.com/jessevdk/go-flags"
import "os"
import "io/ioutil"
import "strings"
+import "strconv"
var version string
func main() {
- version = "0.02"
+ version = "0.1"
//Command line arguments
var opts struct {
Version bool `short:"v" long:"version" description:"Show program version"`
- Add bool `short:"a" long:"add" description:"Add a new poll"`
- New bool `short:"n" long:"new" description:"Add a new poll"`
+ 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"`
Args struct {
- Title string
- PollId int
+
Rest []string
+
} `positional-args:"yes"`
}
@@ -54,13 +55,27 @@ func main() {
if (opts.Version) {
fmt.Println("v" + version)
- } else if (opts.Add || opts.New) {
- err := addPoll(db, opts.Args.Title, opts.Args.Rest, opts.Username)
+ } else if (opts.Add != "") {
+ fmt.Println(opts.Args.Rest)
+
+ pollId, err := addPoll(db, opts.Add, opts.Args.Rest, opts.Username)
+ if (err == nil) {
+ fmt.Print("Poll added with ID ")
+ fmt.Println(pollId)
+ } else {
+ fmt.Println(err)
+ }
+ } else if (opts.New != "") {
+ pollId, err := addPoll(db, opts.New, opts.Args.Rest, opts.Username)
if (err == nil) {
- fmt.Println("Poll added")
+ fmt.Print("Poll added with ID ")
+ fmt.Println(pollId)
} else {
fmt.Println(err)
}
+ } else {
+ fmt.Println(opts.Args.Rest)
+ vote(db, opts.Username, opts.Args.Rest)
}
@@ -97,5 +112,23 @@ func initTables(db *sqlite3.Conn) {
db.Exec("create table options(id integer primary key autoincrement, text text, poll_id int)")
db.Exec("create table votes(id integer primary key autoincrement, user_id int, poll_id int, option_id int)")
-
}
+
+func vote(db *sqlite3.Conn, nick string, options []string) {
+
+ if pollId, err := strconv.Atoi(options[0]); err == nil {
+ option := getOptionFromText(db, options[1])
+ user := getUserForName(db, nick)
+ args := sqlite3.NamedArgs{"$a": user.id, "$b": pollId, "$c": option.id}
+ sql := "INSERT INTO votes (user_id, poll_id, option_id) VALUES ($a, $b, $c)"
+
+ err := db.Exec(sql, args)
+
+ if (err != nil ) {
+ fmt.Print("Failed to vote: ")
+ fmt.Print(err)
+ } else {
+ fmt.Println("Vote added")
+ }
+ }
+} \ No newline at end of file
diff --git a/option.go b/option.go
new file mode 100644
index 0000000..d8cb697
--- /dev/null
+++ b/option.go
@@ -0,0 +1,29 @@
+package main
+
+import "github.com/mxk/go-sqlite/sqlite3"
+
+type Option struct {
+ id int
+ text string
+ pollId int64
+}
+
+func getOptionFromText(db*sqlite3.Conn, text string) Option {
+
+ args := sqlite3.NamedArgs{"$a": text}
+ sql := "SELECT * FROM options WHERE text = $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
+
+ option := Option{id:rowid, text:row["text"].(string), pollId:row["poll_id"].(int64)}
+ return option
+ }
+
+ //If we get here there are no matching users
+ return Option{id:0, text:"", pollId:0}
+
+} \ No newline at end of file
diff --git a/poll.go b/poll.go
index 9cb085e..83d1281 100644
--- a/poll.go
+++ b/poll.go
@@ -8,7 +8,7 @@ type Poll struct {
userId int
}
-func addPoll(db *sqlite3.Conn, title string, options []string, nick string) error {
+func addPoll(db *sqlite3.Conn, title string, options []string, nick string) (int64, error) {
user := getUserForName(db, nick)
@@ -23,18 +23,18 @@ func addPoll(db *sqlite3.Conn, title string, options []string, nick string) erro
err := db.Exec(sql, args)
if (err != nil ) {
- return err
+ return 0, err
}
pollId := db.LastInsertId()
for _,option := range options {
addOption(db, option, pollId)
if (err != nil ) {
- return err
+ return 0, err
}
}
- return nil
+ return pollId, nil
}
@@ -70,4 +70,5 @@ func getPollFromTitle(db *sqlite3.Conn, title string) Poll {
//If we get here there are no matching users
return Poll{id:0, title:"", userId:0}
-} \ No newline at end of file
+}
+