🚨 go fmt; go mod tidy
This commit is contained in:
parent
fb2a29be51
commit
b0657a3fb2
20 changed files with 715 additions and 722 deletions
17
store/db.go
17
store/db.go
|
|
@ -1,8 +1,8 @@
|
|||
package store
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
"database/sql"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
const file = "./govote.db"
|
||||
|
|
@ -10,12 +10,11 @@ const file = "./govote.db"
|
|||
var db *sql.DB
|
||||
|
||||
func init() {
|
||||
var err error
|
||||
if db, err = sql.Open("sqlite3", file); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
var err error
|
||||
if db, err = sql.Open("sqlite3", file); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
initCreateTables(db)
|
||||
initStmts(db)
|
||||
initCreateTables(db)
|
||||
initStmts(db)
|
||||
}
|
||||
|
||||
|
|
|
|||
166
store/handler.go
166
store/handler.go
|
|
@ -12,103 +12,103 @@ import (
|
|||
)
|
||||
|
||||
func NewVoting(
|
||||
id string,
|
||||
r string,
|
||||
d time.Time,
|
||||
q quorum.Quorum,
|
||||
t threshold.Threshold,
|
||||
e []string,
|
||||
a bool,
|
||||
id string,
|
||||
r string,
|
||||
d time.Time,
|
||||
q quorum.Quorum,
|
||||
t threshold.Threshold,
|
||||
e []string,
|
||||
a bool,
|
||||
) error {
|
||||
electors := strings.Join(e, " ")
|
||||
if _, err := votingInsert.Exec(id, r, d.String(), q.String(), t.String(), electors, a); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
electors := strings.Join(e, " ")
|
||||
if _, err := votingInsert.Exec(id, r, d.String(), q.String(), t.String(), electors, a); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetVoting(id string) (*voting.Voting, error) {
|
||||
result := votingSelect.QueryRow(id)
|
||||
if result == nil {
|
||||
return nil, fmt.Errorf("not found: %s", id)
|
||||
}
|
||||
var (
|
||||
err error
|
||||
r string
|
||||
d time.Time
|
||||
q quorum.Quorum
|
||||
t threshold.Threshold
|
||||
e []string
|
||||
a bool
|
||||
dbDeadline string
|
||||
dbQuorum string
|
||||
dbThreshold string
|
||||
dbElectors string
|
||||
)
|
||||
if err := result.Scan(&r, &dbDeadline, &dbQuorum, &dbThreshold, &dbElectors, &a); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if d, err = time.Parse("2006-01-02 15:04:05 -0700 MST", dbDeadline); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := votingSelect.QueryRow(id)
|
||||
if result == nil {
|
||||
return nil, fmt.Errorf("not found: %s", id)
|
||||
}
|
||||
var (
|
||||
err error
|
||||
r string
|
||||
d time.Time
|
||||
q quorum.Quorum
|
||||
t threshold.Threshold
|
||||
e []string
|
||||
a bool
|
||||
dbDeadline string
|
||||
dbQuorum string
|
||||
dbThreshold string
|
||||
dbElectors string
|
||||
)
|
||||
if err := result.Scan(&r, &dbDeadline, &dbQuorum, &dbThreshold, &dbElectors, &a); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if q, err = quorum.FromString(dbQuorum); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if t, err = threshold.FromString(dbThreshold); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, _e := range strings.Split(dbElectors, " ") {
|
||||
if _e != "" {
|
||||
e = append(e, _e)
|
||||
}
|
||||
}
|
||||
if d, err = time.Parse("2006-01-02 15:04:05 -0700 MST", dbDeadline); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
votes, err := getVotes(id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
v := voting.NewVotingWithVotes(id, r, d, q, t, e, a, votes)
|
||||
return v, nil
|
||||
if q, err = quorum.FromString(dbQuorum); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if t, err = threshold.FromString(dbThreshold); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, _e := range strings.Split(dbElectors, " ") {
|
||||
if _e != "" {
|
||||
e = append(e, _e)
|
||||
}
|
||||
}
|
||||
|
||||
votes, err := getVotes(id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
v := voting.NewVotingWithVotes(id, r, d, q, t, e, a, votes)
|
||||
return v, nil
|
||||
}
|
||||
|
||||
func PlaceVote(id, votingID, elector string, choice vote.Choice) error {
|
||||
if _, err := voteInsert.Exec(id, votingID, elector, choice.String()); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
if _, err := voteInsert.Exec(id, votingID, elector, choice.String()); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func getVotes(id string) ([]vote.Vote, error) {
|
||||
result, err := voteSelect.Query(id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var (
|
||||
e string
|
||||
c vote.Choice
|
||||
ts time.Time
|
||||
dbChoice string
|
||||
dbTimestamp string
|
||||
votes = []vote.Vote{}
|
||||
)
|
||||
for result.Next() {
|
||||
if err = result.Scan(&e, &dbChoice, &dbTimestamp); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result, err := voteSelect.Query(id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var (
|
||||
e string
|
||||
c vote.Choice
|
||||
ts time.Time
|
||||
dbChoice string
|
||||
dbTimestamp string
|
||||
votes = []vote.Vote{}
|
||||
)
|
||||
for result.Next() {
|
||||
if err = result.Scan(&e, &dbChoice, &dbTimestamp); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if c, err = vote.ChoiceFromString(dbChoice); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if c, err = vote.ChoiceFromString(dbChoice); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if ts, err = time.Parse("2006-01-02 15:04:05", dbTimestamp); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if ts, err = time.Parse("2006-01-02 15:04:05", dbTimestamp); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
v := vote.NewVoteWithTimestamp(e, c, ts)
|
||||
votes = append(votes, v)
|
||||
}
|
||||
v := vote.NewVoteWithTimestamp(e, c, ts)
|
||||
votes = append(votes, v)
|
||||
}
|
||||
|
||||
return votes, nil
|
||||
return votes, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,16 +3,16 @@ package store
|
|||
import "database/sql"
|
||||
|
||||
var (
|
||||
votingInsert *sql.Stmt
|
||||
votingSelect *sql.Stmt
|
||||
voteEligible *sql.Stmt
|
||||
voteInsert *sql.Stmt
|
||||
voteSelect *sql.Stmt
|
||||
votingInsert *sql.Stmt
|
||||
votingSelect *sql.Stmt
|
||||
voteEligible *sql.Stmt
|
||||
voteInsert *sql.Stmt
|
||||
voteSelect *sql.Stmt
|
||||
)
|
||||
|
||||
func initCreateTables(db *sql.DB) {
|
||||
var err error
|
||||
createTables := `
|
||||
var err error
|
||||
createTables := `
|
||||
CREATE TABLE IF NOT EXISTS voting (
|
||||
id TEXT PRIMARY KEY,
|
||||
referendum TEXT NOT NULL,
|
||||
|
|
@ -36,23 +36,23 @@ func initCreateTables(db *sql.DB) {
|
|||
CREATE INDEX IF NOT EXISTS vote_voting ON vote ( voting );
|
||||
`
|
||||
|
||||
if _, err = db.Exec(createTables); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if _, err = db.Exec(createTables); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func initStmts(db *sql.DB) {
|
||||
initStmtVotingInsert(db)
|
||||
initStmtVotingSelect(db)
|
||||
initStmtVotingInsert(db)
|
||||
initStmtVotingSelect(db)
|
||||
|
||||
initStmtVoteEligible(db)
|
||||
initStmtVoteInsert(db)
|
||||
initStmtVoteSelect(db)
|
||||
initStmtVoteEligible(db)
|
||||
initStmtVoteInsert(db)
|
||||
initStmtVoteSelect(db)
|
||||
}
|
||||
|
||||
func initStmtVotingInsert(db *sql.DB) {
|
||||
var err error
|
||||
if votingInsert, err = db.Prepare(`
|
||||
var err error
|
||||
if votingInsert, err = db.Prepare(`
|
||||
INSERT INTO voting (
|
||||
id,
|
||||
referendum,
|
||||
|
|
@ -63,13 +63,13 @@ func initStmtVotingInsert(db *sql.DB) {
|
|||
anonymous
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?);
|
||||
`); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func initStmtVotingSelect(db *sql.DB) {
|
||||
var err error
|
||||
if votingSelect, err = db.Prepare(`
|
||||
var err error
|
||||
if votingSelect, err = db.Prepare(`
|
||||
SELECT
|
||||
referendum,
|
||||
deadline,
|
||||
|
|
@ -80,13 +80,13 @@ func initStmtVotingSelect(db *sql.DB) {
|
|||
FROM voting
|
||||
WHERE id = ?;
|
||||
`); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func initStmtVoteEligible(db *sql.DB) {
|
||||
var err error
|
||||
if voteEligible, err = db.Prepare(`
|
||||
var err error
|
||||
if voteEligible, err = db.Prepare(`
|
||||
SELECT
|
||||
id,
|
||||
referendum,
|
||||
|
|
@ -104,26 +104,26 @@ func initStmtVoteEligible(db *sql.DB) {
|
|||
)
|
||||
LIMIT 1;
|
||||
`); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func initStmtVoteInsert(db *sql.DB) {
|
||||
var err error
|
||||
if voteInsert, err = db.Prepare(`
|
||||
var err error
|
||||
if voteInsert, err = db.Prepare(`
|
||||
INSERT INTO vote (
|
||||
id, voting, elector, choice
|
||||
) VALUES (
|
||||
?, ?, ?, ?
|
||||
);
|
||||
`); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func initStmtVoteSelect(db *sql.DB) {
|
||||
var err error
|
||||
if voteSelect, err = db.Prepare(`
|
||||
var err error
|
||||
if voteSelect, err = db.Prepare(`
|
||||
SELECT
|
||||
elector,
|
||||
choice,
|
||||
|
|
@ -131,6 +131,6 @@ func initStmtVoteSelect(db *sql.DB) {
|
|||
FROM vote
|
||||
WHERE voting = ?;
|
||||
`); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue