@ -1,19 +1,21 @@
package main
package db
import (
"errors"
"time"
"TerraOceanBot/common"
)
const (
f orceRejectionVote = - 1
n uclearOptionVote = 10
F orceRejectionVote = - 1
N uclearOptionVote = 10
)
var e rrForceRejectionVoteReuse = errors . New ( "db: the user has used force rejection vote in the last month" )
var e rrVoteIsOver = errors . New ( "db: the vote cannot be changed once it's over" )
var E rrForceRejectionVoteReuse = errors . New ( "db: the user has used force rejection vote in the last month" )
var E rrVoteIsOver = errors . New ( "db: the vote cannot be changed once it's over" )
func c reateCustomVote( messageID , text string ) ( int , error ) {
func C reateCustomVote( messageID , text string ) ( int , error ) {
result , err := db . Exec ( "INSERT INTO vote(messageId, name, type, finished) VALUES(?, ?, ?, ?)" , messageID , text , "custom" , false )
if err != nil {
return 0 , err
@ -22,7 +24,7 @@ func createCustomVote(messageID, text string) (int, error) {
return int ( lastID ) , nil
}
func c reateInviteVote( messageID , username , reason string ) ( int , error ) {
func C reateInviteVote( messageID , username , reason string ) ( int , error ) {
result , err := db . Exec ( "INSERT INTO vote(messageId, name, type, finished) VALUES(?, ?, ?, ?)" , messageID , username + ":" + reason , "invite" , false )
if err != nil {
return 0 , err
@ -31,7 +33,7 @@ func createInviteVote(messageID, username, reason string) (int, error) {
return int ( lastID ) , nil
}
func g etVoteName( id int ) ( string , error ) {
func G etVoteName( id int ) ( string , error ) {
rows , err := db . Query ( "SELECT name FROM vote WHERE id=?" , id )
if err != nil {
return "" , err
@ -45,10 +47,10 @@ func getVoteName(id int) (string, error) {
}
return name , nil
}
return "" , e rrNotFound
return "" , E rrNotFound
}
func g etVoteFromMessageID( msgID string ) ( int , error ) {
func G etVoteFromMessageID( msgID string ) ( int , error ) {
rows , err := db . Query ( "SELECT id FROM vote WHERE messageId=?" , msgID )
if err != nil {
return 0 , err
@ -62,10 +64,10 @@ func getVoteFromMessageID(msgID string) (int, error) {
}
return id , nil
}
return 0 , e rrNotFound
return 0 , E rrNotFound
}
func g etMessageIDFromVote( voteID int ) ( string , error ) {
func G etMessageIDFromVote( voteID int ) ( string , error ) {
rows , err := db . Query ( "SELECT messageId FROM vote WHERE id=?" , voteID )
if err != nil {
return "" , err
@ -79,10 +81,10 @@ func getMessageIDFromVote(voteID int) (string, error) {
}
return id , nil
}
return "" , e rrNotFound
return "" , E rrNotFound
}
func g etVoteType( voteID int ) ( string , error ) {
func G etVoteType( voteID int ) ( string , error ) {
rows , err := db . Query ( "SELECT type FROM vote WHERE id=?" , voteID )
if err != nil {
return "" , err
@ -96,18 +98,18 @@ func getVoteType(voteID int) (string, error) {
}
return messageType , nil
}
return "" , e rrNotFound
return "" , E rrNotFound
}
func u pdateVote( voteID int , userID string , voteValue int ) error {
if voteValue == f orceRejectionVote {
func U pdateVote( voteID int , userID string , voteValue int ) error {
if voteValue == F orceRejectionVote {
//Check if they used it within a month.
rows , err := db . Query ( "SELECT voteId FROM choices WHERE date >= ? AND value=?" , time . Now ( ) . AddDate ( 0 , - 1 , 0 ) )
if err != nil {
return err
}
if rows . Next ( ) {
return e rrForceRejectionVoteReuse
return E rrForceRejectionVoteReuse
}
rows . Close ( )
}
@ -116,30 +118,30 @@ func updateVote(voteID int, userID string, voteValue int) error {
rows , err := db . Query ( "SELECT finished FROM vote WHERE id=? AND finished=?" , voteID , true )
defer rows . Close ( )
if rows . Next ( ) {
return e rrVoteIsOver
return E rrVoteIsOver
}
_ , err = db . Exec ( "REPLACE INTO choices (voteId, userId, date, value) VALUES (?, ?, ?, ?)" , voteID , userID , time . Now ( ) , voteValue )
return err
}
func f inishVote( voteID int ) error {
func F inishVote( voteID int ) error {
_ , err := db . Exec ( "UPDATE vote SET finished=true WHERE id=?" , voteID )
return err
}
func getAllVoteChoices( voteID int ) ( [ ] v oteChoice, error ) {
func GetAllVoteChoices( voteID int ) ( [ ] common . V oteChoice, error ) {
rows , err := db . Query ( "SELECT userId, value FROM choices WHERE voteId=?" , voteID )
if err != nil {
return [ ] v oteChoice{ } , err
return [ ] common. V oteChoice{ } , err
}
defer rows . Close ( )
array := make ( [ ] v oteChoice, 0 )
array := make ( [ ] common. V oteChoice, 0 )
for rows . Next ( ) {
var userID string
var value int
rows . Scan ( & userID , & value )
array = append ( array , v oteChoice{
array = append ( array , common. V oteChoice{
UserID : userID ,
Value : value ,
} )