-
Notifications
You must be signed in to change notification settings - Fork 3
/
db_scores.go
271 lines (221 loc) · 6.83 KB
/
db_scores.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package database
import (
"database/sql"
"os"
"time"
"github.com/nsec/askgod/api"
"github.com/nsec/askgod/internal/utils"
)
// GetTeamPoints returns the current total for the team
func (db *DB) GetTeamPoints(teamid int64) (int64, error) {
total := int64(0)
// Get the total
err := db.QueryRow("SELECT COALESCE(SUM(score.value), 0) AS points FROM score WHERE teamid=$1", teamid).Scan(&total)
if err != nil {
return -1, err
}
return total, nil
}
// GetTeamFlags retrieves all the score entries for the team
func (db *DB) GetTeamFlags(teamid int64) ([]api.Flag, error) {
// Return a list of score entries
resp := []api.Flag{}
// Query all the scores from the database
rows, err := db.Query("SELECT score.flagid, flag.description, score.value, score.notes, score.submit_time, flag.return_string FROM score LEFT JOIN flag ON flag.id=score.flagid WHERE score.teamid=$1 ORDER BY score.submit_time ASC;", teamid)
if err != nil {
return nil, err
}
defer rows.Close()
// Iterate through the results
for rows.Next() {
row := api.Flag{}
err := rows.Scan(&row.ID, &row.Description, &row.Value, &row.Notes, &row.SubmitTime, &row.ReturnString)
if err != nil {
return nil, err
}
resp = append(resp, row)
}
// Check for any error that might have happened
err = rows.Err()
if err != nil {
return nil, err
}
return resp, nil
}
// GetTeamFlag retrieves a single score entry for the team
func (db *DB) GetTeamFlag(teamid int64, id int64) (*api.Flag, error) {
// Return a list of score entries
resp := api.Flag{}
// Query all the scores from the database
err := db.QueryRow("SELECT score.flagid, flag.description, score.value, score.notes, score.submit_time, flag.return_string FROM score LEFT JOIN flag ON flag.id=score.flagid WHERE score.teamid=$1 AND score.flagid=$2 ORDER BY score.submit_time ASC;", teamid, id).Scan(
&resp.ID, &resp.Description, &resp.Value, &resp.Notes, &resp.SubmitTime, &resp.ReturnString)
if err != nil {
return nil, err
}
return &resp, nil
}
// UpdateTeamFlag updates a single score entry for the team
func (db *DB) UpdateTeamFlag(teamid int64, id int64, flag api.FlagPut) error {
// Update the database entry
result, err := db.Exec("UPDATE score SET notes=$1 WHERE teamid=$2 AND flagid=$3;",
flag.Notes, teamid, id)
if err != nil {
return err
}
// Check that a change indeed happened
count, err := result.RowsAffected()
if err != nil {
return err
}
if count == 0 {
return sql.ErrNoRows
}
return nil
}
// SubmitTeamFlag validates a submitted flag and adds it to the database
func (db *DB) SubmitTeamFlag(teamid int64, flag api.FlagPost) (*api.Flag, *api.AdminFlag, error) {
// Query the database entry
row := api.AdminFlag{}
tags := ""
err := db.QueryRow("SELECT id, flag, value, return_string, description, tags FROM flag WHERE LOWER(flag)=LOWER($1);", flag.Flag).Scan(
&row.ID, &row.Flag, &row.Value, &row.ReturnString, &row.Description, &tags)
if err != nil {
return nil, nil, err
}
row.Tags, err = utils.ParseTags(tags)
if err != nil {
return nil, nil, err
}
// Check if already submitted
id := int64(-1)
err = db.QueryRow("SELECT id FROM score WHERE teamid=$1 AND flagid=$2;", teamid, row.ID).Scan(&id)
if err == nil {
return nil, &row, os.ErrExist
} else if err != sql.ErrNoRows {
return nil, nil, err
}
// Add the flag
id = -1
err = db.QueryRow("INSERT INTO score (teamid, flagid, value, notes, submit_time) VALUES ($1, $2, $3, $4, $5) RETURNING id;",
teamid, row.ID, row.Value, flag.Notes, time.Now()).Scan(&id)
if err != nil {
return nil, nil, err
}
// Query the new entry
result := api.Flag{}
err = db.QueryRow("SELECT score.flagid, flag.description, score.value, score.notes, score.submit_time, flag.return_string FROM score LEFT JOIN flag ON flag.id=score.flagid WHERE score.id=$1;", id).Scan(
&result.ID, &result.Description, &result.Value, &result.Notes, &result.SubmitTime, &result.ReturnString)
if err != nil {
return nil, nil, err
}
return &result, &row, nil
}
// GetScores retrieves all the score entries from the database
func (db *DB) GetScores() ([]api.AdminScore, error) {
// Return a list of score entries
resp := []api.AdminScore{}
// Query all the scores from the database
rows, err := db.Query("SELECT id, teamid, flagid, value, notes, submit_time FROM score ORDER BY id ASC;")
if err != nil {
return nil, err
}
defer rows.Close()
// Iterate through the results
for rows.Next() {
row := api.AdminScore{}
err := rows.Scan(&row.ID, &row.TeamID, &row.FlagID, &row.Value, &row.Notes, &row.SubmitTime)
if err != nil {
return nil, err
}
resp = append(resp, row)
}
// Check for any error that might have happened
err = rows.Err()
if err != nil {
return nil, err
}
return resp, nil
}
// GetScore retrieves a single score entry from the database
func (db *DB) GetScore(id int64) (*api.AdminScore, error) {
// Query the database entry
row := api.AdminScore{}
err := db.QueryRow("SELECT id, teamid, flagid, value, notes, submit_time FROM score WHERE id=$1;", id).Scan(
&row.ID, &row.TeamID, &row.FlagID, &row.Value, &row.Notes, &row.SubmitTime)
if err != nil {
return nil, err
}
return &row, nil
}
// CreateScore adds a new score entry to the database
func (db *DB) CreateScore(score api.AdminScorePost) (int64, error) {
id := int64(-1)
// Create the database entry
err := db.QueryRow("INSERT INTO score (teamid, flagid, value, notes, submit_time) VALUES ($1, $2, $3, $4, $5) RETURNING id",
score.TeamID, score.FlagID, score.Value, score.Notes, time.Now()).Scan(&id)
if err != nil {
return -1, err
}
return id, nil
}
// UpdateScore updates an existing score entry
func (db *DB) UpdateScore(id int64, score api.AdminScorePut) error {
// Update the database entry
result, err := db.Exec("UPDATE score SET value=$1, notes=$2 WHERE id=$3;",
score.Value, score.Notes, id)
if err != nil {
return err
}
// Check that a change indeed happened
count, err := result.RowsAffected()
if err != nil {
return err
}
if count == 0 {
return sql.ErrNoRows
}
return nil
}
// DeleteScore deletes a single score entry from the database
func (db *DB) DeleteScore(id int64) error {
// Delete the database entry
result, err := db.Exec("DELETE FROM score WHERE id=$1;", id)
if err != nil {
return err
}
// Check that a change indeed happened
count, err := result.RowsAffected()
if err != nil {
return err
}
if count == 0 {
return sql.ErrNoRows
}
return nil
}
// ClearScores wipes all score entries from the database
func (db *DB) ClearScores() error {
// Start a transaction
tx, err := db.Begin()
if err != nil {
return err
}
// Wipe the table
_, err = tx.Exec("DELETE FROM score;")
if err != nil {
tx.Rollback()
return err
}
// Reset the sequence
_, err = tx.Exec("ALTER SEQUENCE score_id_seq RESTART;")
if err != nil {
tx.Rollback()
return err
}
// Commit
err = tx.Commit()
if err != nil {
return err
}
return nil
}