summaryrefslogtreecommitdiff
path: root/models.go
diff options
context:
space:
mode:
authorJan Dittberner <jandd@cacert.org>2019-07-31 14:14:21 +0200
committerJan Dittberner <jandd@cacert.org>2019-07-31 14:14:21 +0200
commit2d7f0cc0e309046607c7bb15954fb258c37efab7 (patch)
tree03a77da9e17f96c29e51e2f04fa67b7b9cde075c /models.go
parent870e3ab1d2deed3a601bd6ba96856c3a00d89eb3 (diff)
downloadcacert-boardvoting-2d7f0cc0e309046607c7bb15954fb258c37efab7.tar.gz
cacert-boardvoting-2d7f0cc0e309046607c7bb15954fb258c37efab7.tar.xz
cacert-boardvoting-2d7f0cc0e309046607c7bb15954fb258c37efab7.zip
Modernize Go code
- switch from go-logging to logrus - handle all errors - use gomail's NewDialer instead of deprecated NewPlainDialer
Diffstat (limited to 'models.go')
-rw-r--r--models.go63
1 files changed, 33 insertions, 30 deletions
diff --git a/models.go b/models.go
index 8c2f968..a2740ad 100644
--- a/models.go
+++ b/models.go
@@ -3,10 +3,12 @@ package main
import (
"database/sql"
"fmt"
- dbmig "git.cacert.org/cacert-boardvoting/db"
"github.com/jmoiron/sqlx"
"github.com/rubenv/sql-migrate"
"time"
+
+ migrations "git.cacert.org/cacert-boardvoting/db"
+ log "github.com/sirupsen/logrus"
)
type sqlKey int
@@ -250,16 +252,16 @@ func NewDB(database *sql.DB) *dbHandler {
var stmt *sqlx.Stmt
stmt, err := handler.db.Preparex(sqlStatement)
if err != nil {
- log.Critical("ERROR parsing statement %s: %s", sqlStatement, err)
+ log.Errorf("error parsing statement %s: %s", sqlStatement, err)
failedStatements = append(failedStatements, sqlStatement)
}
- stmt.Close()
+ _ = stmt.Close()
}
if len(failedStatements) > 0 {
log.Panicf("%d statements failed to prepare", len(failedStatements))
}
- _, err := migrate.Exec(database, "sqlite3", dbmig.Migrations(), migrate.Up)
+ _, err := migrate.Exec(database, "sqlite3", migrations.Migrations(), migrate.Up)
if err != nil {
log.Panicf("running database migration failed: %v", err)
}
@@ -288,7 +290,7 @@ func (d *dbHandler) getPreparedStatement(statementKey sqlKey) *sqlx.Stmt {
func (v *Vote) Save() (err error) {
insertVoteStmt := db.getPreparedNamedStatement(sqlCreateVote)
- defer insertVoteStmt.Close()
+ defer func() { _ = insertVoteStmt.Close() }()
if _, err = insertVoteStmt.Exec(v); err != nil {
log.Errorf("saving vote failed: %v", err)
@@ -296,7 +298,7 @@ func (v *Vote) Save() (err error) {
}
getVoteStmt := db.getPreparedStatement(sqlLoadVote)
- defer getVoteStmt.Close()
+ defer func() { _ = getVoteStmt.Close() }()
if err = getVoteStmt.Get(v, v.DecisionId, v.VoterId); err != nil {
log.Errorf("getting inserted vote failed: %v", err)
@@ -353,13 +355,14 @@ type DecisionForDisplay struct {
func FindDecisionForDisplayByTag(tag string) (decision *DecisionForDisplay, err error) {
decisionStmt := db.getPreparedStatement(sqlLoadDecisionByTag)
- defer decisionStmt.Close()
+ defer func() { _ = decisionStmt.Close() }()
decision = &DecisionForDisplay{}
if err = decisionStmt.Get(decision, tag); err != nil {
if err == sql.ErrNoRows {
decision = nil
err = nil
+ return
} else {
log.Errorf("getting motion %s failed: %v", tag, err)
return
@@ -381,7 +384,7 @@ func FindDecisionsForDisplayOnPage(page int64, unvoted bool, voter *Voter) (deci
} else {
decisionsStmt = db.getPreparedStatement(sqlLoadDecisions)
}
- defer decisionsStmt.Close()
+ defer func() { _ = decisionsStmt.Close() }()
var rows *sqlx.Rows
if unvoted && voter != nil {
@@ -393,7 +396,7 @@ func FindDecisionsForDisplayOnPage(page int64, unvoted bool, voter *Voter) (deci
log.Errorf("loading motions for page %d failed: %v", page, err)
return
}
- defer rows.Close()
+ defer func() { _ = rows.Close() }()
for rows.Next() {
var d DecisionForDisplay
@@ -412,14 +415,14 @@ func FindDecisionsForDisplayOnPage(page int64, unvoted bool, voter *Voter) (deci
func (d *Decision) VoteSums() (sums *VoteSums, err error) {
votesStmt := db.getPreparedStatement(sqlLoadVoteCountsForDecision)
- defer votesStmt.Close()
+ defer func() { _ = votesStmt.Close() }()
voteRows, err := votesStmt.Queryx(d.Id)
if err != nil {
log.Errorf("fetching vote sums for motion %s failed: %v", d.Tag, err)
return
}
- defer voteRows.Close()
+ defer func() { _ = voteRows.Close() }()
sums = &VoteSums{}
for voteRows.Next() {
@@ -443,7 +446,7 @@ func (d *Decision) VoteSums() (sums *VoteSums, err error) {
func (d *DecisionForDisplay) LoadVotes() (err error) {
votesStmt := db.getPreparedStatement(sqlLoadVotesForDecision)
- defer votesStmt.Close()
+ defer func() { _ = votesStmt.Close() }()
err = votesStmt.Select(&d.Votes, d.Id)
if err != nil {
@@ -456,7 +459,7 @@ func (d *DecisionForDisplay) LoadVotes() (err error) {
func (d *Decision) OlderExists(unvoted bool, voter *Voter) (result bool, err error) {
if unvoted && voter != nil {
olderStmt := db.getPreparedStatement(sqlCountOlderThanUnvotedDecision)
- defer olderStmt.Close()
+ defer func() { _ = olderStmt.Close() }()
if err = olderStmt.Get(&result, d.Proposed, voter.Id); err != nil {
log.Errorf("finding older motions than %s failed: %v", d.Tag, err)
@@ -464,7 +467,7 @@ func (d *Decision) OlderExists(unvoted bool, voter *Voter) (result bool, err err
}
} else {
olderStmt := db.getPreparedStatement(sqlCountOlderThanDecision)
- defer olderStmt.Close()
+ defer func() { _ = olderStmt.Close() }()
if err = olderStmt.Get(&result, d.Proposed); err != nil {
log.Errorf("finding older motions than %s failed: %v", d.Tag, err)
@@ -477,7 +480,7 @@ func (d *Decision) OlderExists(unvoted bool, voter *Voter) (result bool, err err
func (d *Decision) Create() (err error) {
insertDecisionStmt := db.getPreparedNamedStatement(sqlCreateDecision)
- defer insertDecisionStmt.Close()
+ defer func() { _ = insertDecisionStmt.Close() }()
result, err := insertDecisionStmt.Exec(d)
if err != nil {
@@ -493,7 +496,7 @@ func (d *Decision) Create() (err error) {
rescheduleChannel <- JobIdCloseDecisions
getDecisionStmt := db.getPreparedStatement(sqlLoadDecisionById)
- defer getDecisionStmt.Close()
+ defer func() { _ = getDecisionStmt.Close() }()
err = getDecisionStmt.Get(d, lastInsertId)
if err != nil {
@@ -506,7 +509,7 @@ func (d *Decision) Create() (err error) {
func (d *Decision) LoadWithId() (err error) {
getDecisionStmt := db.getPreparedStatement(sqlLoadDecisionById)
- defer getDecisionStmt.Close()
+ defer func() { _ = getDecisionStmt.Close() }()
err = getDecisionStmt.Get(d, d.Id)
if err != nil {
@@ -519,7 +522,7 @@ func (d *Decision) LoadWithId() (err error) {
func (d *Decision) Update() (err error) {
updateDecisionStmt := db.getPreparedNamedStatement(sqlUpdateDecision)
- defer updateDecisionStmt.Close()
+ defer func() { _ = updateDecisionStmt.Close() }()
result, err := updateDecisionStmt.Exec(d)
if err != nil {
@@ -541,7 +544,7 @@ func (d *Decision) Update() (err error) {
func (d *Decision) UpdateStatus() (err error) {
updateStatusStmt := db.getPreparedNamedStatement(sqlUpdateDecisionStatus)
- defer updateStatusStmt.Close()
+ defer func() { _ = updateStatusStmt.Close() }()
result, err := updateStatusStmt.Exec(d)
if err != nil {
@@ -567,7 +570,7 @@ func (d *Decision) String() string {
func FindVoterByAddress(emailAddress string) (voter *Voter, err error) {
findVoterStmt := db.getPreparedStatement(sqlLoadEnabledVoterByEmail)
- defer findVoterStmt.Close()
+ defer func() { _ = findVoterStmt.Close() }()
voter = &Voter{}
if err = findVoterStmt.Get(voter, emailAddress); err != nil {
@@ -595,7 +598,7 @@ func (d *Decision) Close() error {
d.Status, reasoning = voteSums.CalculateResult(quorum, majority)
closeDecisionStmt := db.getPreparedNamedStatement(sqlUpdateDecisionStatus)
- defer closeDecisionStmt.Close()
+ defer func() { _ = closeDecisionStmt.Close() }()
result, err := closeDecisionStmt.Exec(d)
if err != nil {
@@ -618,7 +621,7 @@ func (d *Decision) Close() error {
func CloseDecisions() (err error) {
getClosableDecisionsStmt := db.getPreparedNamedStatement(sqlSelectClosableDecisions)
- defer getClosableDecisionsStmt.Close()
+ defer func() { _ = getClosableDecisionsStmt.Close() }()
decisions := make([]*Decision, 0)
rows, err := getClosableDecisionsStmt.Queryx(struct{ Now time.Time }{time.Now().UTC()})
@@ -626,7 +629,7 @@ func CloseDecisions() (err error) {
log.Errorf("fetching closable decisions failed: %v", err)
return
}
- defer rows.Close()
+ defer func() { _ = rows.Close() }()
for rows.Next() {
decision := &Decision{}
if err = rows.StructScan(decision); err != nil {
@@ -635,10 +638,10 @@ func CloseDecisions() (err error) {
}
decisions = append(decisions, decision)
}
- rows.Close()
+ defer func() { _ = rows.Close() }()
for _, decision := range decisions {
- log.Debugf("found closable decision %s", decision.Tag)
+ log.Infof("found closable decision %s", decision.Tag)
if err = decision.Close(); err != nil {
log.Errorf("closing decision %s failed: %s", decision.Tag, err)
return
@@ -650,7 +653,7 @@ func CloseDecisions() (err error) {
func GetNextPendingDecisionDue() (due *time.Time, err error) {
getNextPendingDecisionDueStmt := db.getPreparedStatement(sqlGetNextPendingDecisionDue)
- defer getNextPendingDecisionDueStmt.Close()
+ defer func() { _ = getNextPendingDecisionDueStmt.Close() }()
row := getNextPendingDecisionDueStmt.QueryRow()
@@ -669,7 +672,7 @@ func GetNextPendingDecisionDue() (due *time.Time, err error) {
func GetReminderVoters() (voters *[]Voter, err error) {
getReminderVotersStmt := db.getPreparedStatement(sqlGetReminderVoters)
- defer getReminderVotersStmt.Close()
+ defer func() { _ = getReminderVotersStmt.Close() }()
voterSlice := make([]Voter, 0)
@@ -684,7 +687,7 @@ func GetReminderVoters() (voters *[]Voter, err error) {
func FindUnvotedDecisionsForVoter(voter *Voter) (decisions *[]Decision, err error) {
findUnvotedDecisionsForVoterStmt := db.getPreparedStatement(sqlFindUnvotedDecisionsForVoter)
- defer findUnvotedDecisionsForVoterStmt.Close()
+ defer func() { _ = findUnvotedDecisionsForVoterStmt.Close() }()
decisionsSlice := make([]Decision, 0)
@@ -699,7 +702,7 @@ func FindUnvotedDecisionsForVoter(voter *Voter) (decisions *[]Decision, err erro
func GetVoterById(id int64) (voter *Voter, err error) {
getVoterByIdStmt := db.getPreparedStatement(sqlGetEnabledVoterById)
- defer getVoterByIdStmt.Close()
+ defer func() { _ = getVoterByIdStmt.Close() }()
voter = &Voter{}
if err = getVoterByIdStmt.Get(voter, id); err != nil {
@@ -712,7 +715,7 @@ func GetVoterById(id int64) (voter *Voter, err error) {
func GetVotersForProxy(proxy *Voter) (voters *[]Voter, err error) {
getVotersForProxyStmt := db.getPreparedStatement(sqlGetVotersForProxy)
- defer getVotersForProxyStmt.Close()
+ defer func() { _ = getVotersForProxyStmt.Close() }()
votersSlice := make([]Voter, 0)