From 397992465eb0eae75c05779bda1d662129657386 Mon Sep 17 00:00:00 2001 From: Joe Robinson Date: Mon, 21 Jul 2014 12:16:04 +0100 Subject: Only allow users to vote on a poll once --- blavote.go | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) (limited to 'blavote.go') diff --git a/blavote.go b/blavote.go index 857c904..fb4444b 100644 --- a/blavote.go +++ b/blavote.go @@ -14,7 +14,7 @@ var version string func main() { - version = "0.4" + version = "0.5" //Command line arguments var opts struct { @@ -86,9 +86,11 @@ func main() { } } else { - vote(db, opts.Username, opts.Args.Rest) - + err = vote(db, opts.Username, opts.Args.Rest) + if (err != nil) { + fmt.Println(err) + } } } @@ -157,16 +159,21 @@ func vote(db *sqlite3.Conn, nick string, options []string) error { } } - args := sqlite3.NamedArgs{"$a": user.id, "$b": poll.id, "$c": option.id} - sql := "INSERT INTO votes (user_id, poll_id, option_id) VALUES ($a, $b, $c)" + if (!hasUserVotedInPoll(db, poll, user)) { - err = db.Exec(sql, args) + args := sqlite3.NamedArgs{"$a": user.id, "$b": poll.id, "$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 ) { + return err + } else { + fmt.Println("Vote added") + return nil + } - if (err != nil ) { - fmt.Print("Failed to vote: ") - return err } else { - fmt.Println("Vote added") - return nil + return errors.New("You have already voted in this poll") } -} \ No newline at end of file +} -- cgit v1.2.3