/
repository.go
174 lines (134 loc) · 3.23 KB
/
repository.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package ballot
import (
"database/sql"
"fmt"
"github.com/jukeizu/voting/pkg/voting"
"github.com/jukeizu/voting/pkg/voting/ballot/migrations"
"github.com/lib/pq"
"github.com/shawntoffel/gossage"
)
const (
DatabaseName = "voting_ballot"
)
type Repository interface {
Migrate() error
VoidBallotOptions(pollId string, voterId string) error
CreateBallotOptions(ballot voting.Ballot) error
VoterCount(pollId string) (int64, error)
VoterIds(pollId string) ([]string, error)
VoterBallot(pollId string, voterId string) ([]string, error)
}
type repository struct {
Db *sql.DB
}
func NewRepository(url string) (Repository, error) {
conn := fmt.Sprintf("postgresql://%s/%s?sslmode=disable", url, DatabaseName)
db, err := sql.Open("postgres", conn)
if err != nil {
return nil, err
}
r := repository{
Db: db,
}
return &r, nil
}
func (r *repository) Migrate() error {
_, err := r.Db.Exec(`CREATE DATABASE IF NOT EXISTS ` + DatabaseName)
if err != nil {
return err
}
g, err := gossage.New(r.Db)
if err != nil {
return err
}
err = g.RegisterMigrations(migrations.CreateTableBallot20190423035611{})
if err != nil {
return err
}
return g.Up()
}
func (r *repository) VoidBallotOptions(pollId string, voterId string) error {
q := `UPDATE ballot_option SET void = true, updated = now() WHERE pollId = $1 AND voterId = $2`
_, err := r.Db.Exec(q, pollId, voterId)
return err
}
func (r *repository) CreateBallotOptions(ballot voting.Ballot) error {
txn, err := r.Db.Begin()
if err != nil {
return err
}
stmt, err := txn.Prepare(pq.CopyIn("ballot_option", "pollid", "optionid", "rank", "voterid"))
if err != nil {
return err
}
for _, ballotOption := range ballot.Options {
_, err := stmt.Exec(ballot.PollId, ballotOption.OptionId, ballotOption.Rank, ballot.Voter.Id)
if err != nil {
return err
}
}
_, err = stmt.Exec()
if err != nil {
return err
}
err = stmt.Close()
if err != nil {
return err
}
err = txn.Commit()
if err != nil {
return err
}
return nil
}
func (r *repository) VoterCount(pollId string) (int64, error) {
q := `SELECT COUNT(DISTINCT voterid) FROM ballot_option WHERE pollid = $1 AND void = false`
count := int64(0)
err := r.Db.QueryRow(q, pollId).Scan(&count)
if err != nil {
return 0, err
}
return count, nil
}
func (r *repository) VoterIds(pollId string) ([]string, error) {
q := `SELECT DISTINCT voterId FROM ballot_option WHERE pollid = $1 AND void = false`
voterIds := []string{}
rows, err := r.Db.Query(q, pollId)
if err != nil {
return voterIds, err
}
defer rows.Close()
for rows.Next() {
voterId := ""
err := rows.Scan(
&voterId,
)
if err != nil {
return voterIds, err
}
voterIds = append(voterIds, voterId)
}
return voterIds, nil
}
func (r *repository) VoterBallot(pollId string, voterId string) ([]string, error) {
q := `SELECT optionId FROM ballot_option
WHERE pollid = $1 AND voterid = $2 AND void = false
ORDER by rank`
optionIds := []string{}
rows, err := r.Db.Query(q, pollId, voterId)
if err != nil {
return optionIds, err
}
defer rows.Close()
for rows.Next() {
optionId := ""
err := rows.Scan(
&optionId,
)
if err != nil {
return optionIds, err
}
optionIds = append(optionIds, optionId)
}
return optionIds, nil
}