From 5b1d414ba23937e7fe1eba74547a7acf33e9c5a6 Mon Sep 17 00:00:00 2001 From: Joe Robinson Date: Mon, 21 Jul 2014 11:51:34 +0100 Subject: Added some error checking --- blavote.go | 59 +++++++++++++++++++++++++++++++++++++++++++---------------- option.go | 9 +++++---- poll.go | 38 ++++++++++++++++++++++++-------------- user.go | 12 +++++++++--- 4 files changed, 81 insertions(+), 37 deletions(-) diff --git a/blavote.go b/blavote.go index 9158e1c..857c904 100644 --- a/blavote.go +++ b/blavote.go @@ -8,12 +8,13 @@ import "os" import "io/ioutil" import "strings" import "strconv" +import "errors" var version string func main() { - version = "0.31" + version = "0.4" //Command line arguments var opts struct { @@ -75,7 +76,7 @@ func main() { fmt.Println(err) } } else if (opts.Remove > 0) { - deletePoll(db, opts.Remove, opts.Username) + err = deletePoll(db, opts.Remove, opts.Username) if (err == nil) { fmt.Print("Poll removed with ID ") @@ -85,11 +86,11 @@ func main() { } } else { - fmt.Println(opts.Args.Rest) vote(db, opts.Username, opts.Args.Rest) - } + } + } func connectDb(name string) (*sqlite3.Conn, error) { @@ -125,21 +126,47 @@ func initTables(db *sqlite3.Conn) { } -func vote(db *sqlite3.Conn, nick string, options []string) { +func vote(db *sqlite3.Conn, nick string, options []string) error { - 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)" + pollId, err := strconv.Atoi(options[0]) - err := db.Exec(sql, args) + if (err != nil) { + return errors.New("Poll ID must be a number") + } - if (err != nil ) { - fmt.Print("Failed to vote: ") - fmt.Print(err) - } else { - fmt.Println("Vote added") + poll, err := getPollFromId(db, pollId) + + if (err != nil) { + return err + } + + option, err := getOptionFromText(db, options[1]) + + if (err != nil) { + return err + } + + user := getUserForName(db, nick) + + //Record the user in the DB if they aren't already + if (user.id == 0) { + user, err = createUser(db, nick, false) + + if (err != nil) { + return err } } + + 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 ) { + fmt.Print("Failed to vote: ") + return err + } else { + fmt.Println("Vote added") + return nil + } } \ No newline at end of file diff --git a/option.go b/option.go index d8cb697..e3d246f 100644 --- a/option.go +++ b/option.go @@ -1,6 +1,7 @@ package main import "github.com/mxk/go-sqlite/sqlite3" +import "errors" type Option struct { id int @@ -8,7 +9,7 @@ type Option struct { pollId int64 } -func getOptionFromText(db*sqlite3.Conn, text string) Option { +func getOptionFromText(db*sqlite3.Conn, text string) (Option, error) { args := sqlite3.NamedArgs{"$a": text} sql := "SELECT * FROM options WHERE text = $a" @@ -20,10 +21,10 @@ func getOptionFromText(db*sqlite3.Conn, text string) Option { 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 + return option, nil } - //If we get here there are no matching users - return Option{id:0, text:"", pollId:0} + //If we get here there are no matching options + return Option{id:0, text:"", pollId:0}, errors.New("No option found matching '" + text + "'") } \ No newline at end of file diff --git a/poll.go b/poll.go index 803cf6c..275aa21 100644 --- a/poll.go +++ b/poll.go @@ -1,7 +1,8 @@ package main import "github.com/mxk/go-sqlite/sqlite3" -import "fmt" +import "errors" +import "strconv" type Poll struct { id int64 @@ -14,10 +15,15 @@ func addPoll(db *sqlite3.Conn, title string, options []string, nick string) (int user := getUserForName(db, nick) if (user.id == 0) { - createUser(db, nick, false) - user = getUserForName(db, nick) + var err error + user, err = createUser(db, nick, false) + + if (err != nil) { + return 0, err + } } + args := sqlite3.NamedArgs{"$a": title, "$b": user.id} sql := "INSERT INTO polls (title, user_id) VALUES ($a, $b)" @@ -53,7 +59,7 @@ func addOption(db *sqlite3.Conn, text string, pollId int64) error { return nil } -func getPollFromTitle(db *sqlite3.Conn, title string) Poll { +func getPollFromTitle(db *sqlite3.Conn, title string) (Poll, error) { args := sqlite3.NamedArgs{"$a": title} sql := "SELECT * FROM polls WHERE title = $a" @@ -65,15 +71,15 @@ func getPollFromTitle(db *sqlite3.Conn, title string) Poll { 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 + return poll, nil } - //If we get here there are no matching users - return Poll{id:0, title:"", userId:0} + //If we get here there are no matching polls, return an error + return Poll{id:0, title:"", userId:0}, errors.New("No poll found with title '" + title +"'") } -func getPollFromId(db *sqlite3.Conn, id int) Poll { +func getPollFromId(db *sqlite3.Conn, id int) (Poll, error) { args := sqlite3.NamedArgs{"$a": id} sql := "SELECT * FROM polls WHERE id = $a" @@ -85,18 +91,23 @@ func getPollFromId(db *sqlite3.Conn, id int) Poll { 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 + return poll, nil } - //If we get here there are no matching users - return Poll{id:0, title:"", userId:0} + //If we get here there are no matching polls, return an error + return Poll{id:0, title:"", userId:0}, errors.New("No poll found with ID " + strconv.Itoa(id)) } func deletePoll(db *sqlite3.Conn, id int, nick string) error { user := getUserForName(db, nick) - poll := getPollFromId(db, id) + + poll, err := getPollFromId(db, id) + + if (err != nil) { + return err + } if (user.isAdmin || poll.userId == user.id) { @@ -109,8 +120,7 @@ func deletePoll(db *sqlite3.Conn, id int, nick string) error { return nil } } else { - fmt.Println("denied") - return nil + return errors.New("You do not have permission to delete this poll") } diff --git a/user.go b/user.go index b686f74..f2a5afd 100644 --- a/user.go +++ b/user.go @@ -8,14 +8,20 @@ type User struct { isAdmin bool } -func createUser(db *sqlite3.Conn, name string, isAdmin bool) { +func createUser(db *sqlite3.Conn, name string, isAdmin bool) (User, error) { + + user := getUserForName(db, name) //Check if a user with this name already exists - if (getUserForName(db, name).id == 0) { + if (user.id == 0) { args := sqlite3.NamedArgs{"$a": name, "$b": isAdmin} sql := "INSERT INTO users (name, admin) VALUES ($a, $b)" - db.Exec(sql, args) + err := db.Exec(sql, args) + + return User{id:db.LastInsertId(), name:name, isAdmin:isAdmin}, err + } else { + return user, nil } } -- cgit v1.2.3