-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.go
236 lines (194 loc) · 5.36 KB
/
database.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
package main
import (
"database/sql"
"log"
"time"
_ "github.com/mattn/go-sqlite3"
)
var db *sql.DB
func loadDatabase() {
conn, err := sql.Open("sqlite3", config.Database)
checkErr(err)
db = conn
createDatabase()
}
func createDatabase() {
log.Println("Creating database...")
sqlStmt1 := `CREATE TABLE IF NOT EXISTS kudos (id integer primary key, userFrom text, kudo text, message text, value INTEGER DEFAULT 1, date text);`
_, err1 := db.Exec(sqlStmt1)
checkErr(err1)
sqlStmt2 := `CREATE TABLE IF NOT EXISTS kudos_receiver (id integer primary key, userTo text, kudos_id INTEGER);`
_, err2 := db.Exec(sqlStmt2)
checkErr(err2)
}
// Remove data migration in the future.
func migrateKudos() {
stmt1 := `ALTER TABLE kudos ADD COLUMN message text`
db.Exec(stmt1)
rows, err := db.Query("SELECT id, kudo FROM kudos ORDER BY id ASC")
checkErr(err)
defer rows.Close()
stmt2, err2 := db.Prepare("INSERT INTO kudos_receiver (userTo, kudos_id) VALUES (?, ?)")
checkErr(err2)
stmt3, err3 := db.Prepare("UPDATE kudos SET message = ? WHERE id = ?")
checkErr(err3)
var kudosList []Kudo
for rows.Next() {
var kudo Kudo
err = rows.Scan(&kudo.ID, &kudo.Text)
checkErr(err)
kudosList = append(kudosList, kudo)
}
rows.Close()
for _, element := range kudosList {
parsed := parseKudoCommand(element.Text)
log.Printf("Kudo #%d\n", element.ID)
stmt3.Exec(parsed.Text, element.ID)
for _, member := range parsed.Members {
log.Printf("Receiver #%s\n", member.ID)
stmt2.Exec(member.ID, element.ID)
}
log.Println("________")
}
}
func dbSaveKudo(kudo *Kudo) {
if kudo.ID != 0 {
return
}
stmt, err := db.Prepare("INSERT INTO kudos (userFrom, kudo, message, value, date) VALUES (?, ?, ?, ?, ?)")
checkErr(err)
res, err := stmt.Exec(kudo.MemberFrom.ID, kudo.Original, kudo.Text, kudo.Value, kudo.Date.Format(time.RFC3339))
checkErr(err)
id, err := res.LastInsertId()
checkErr(err)
stmt2, err := db.Prepare("INSERT INTO kudos_receiver (userTo, kudos_id) VALUES (?, ?)")
checkErr(err)
for _, member := range kudo.Recipients {
stmt2.Exec(member.ID, id)
}
kudo.ID = id
}
func loadKudos() {
rows, err := db.Query("SELECT id, userFrom, message, value, date FROM kudos ORDER BY id DESC LIMIT 9")
checkErr(err)
defer rows.Close()
for rows.Next() {
var kudo Kudo
var memberFrom, date string
err = rows.Scan(&kudo.ID, &memberFrom, &kudo.Text, &kudo.Value, &date)
checkErr(err)
kudo.MemberFrom, _ = findMemberByID(memberFrom)
kudo.Recipients = loadKudosRecipients(kudo.ID)
kudo.Color = randomColor()
kudo.Date, _ = time.Parse(time.RFC3339, date)
kudos = append(kudos, kudo)
}
}
func loadKudosRecipients(kudoId int64) []Member {
stmt, err := db.Prepare("SELECT userTo FROM kudos_receiver WHERE kudos_id = ?")
checkErr(err)
memberRows, err := stmt.Query(kudoId)
checkErr(err)
defer memberRows.Close()
var members []Member
for memberRows.Next() {
var memberToId string
err = memberRows.Scan(&memberToId)
checkErr(err)
memberTo, err := findMemberByID(memberToId)
if err != nil {
log.Printf("Invalid user provided: #%s\n", memberToId)
} else {
members = append(members, memberTo)
}
}
return members
}
func loadKudosGaveList() []KudosStats {
rows, err := db.Query(`
SELECT userFrom, count(id) as pts FROM kudos
WHERE kudos.date > date('now','-1 year')
GROUP by userFrom HAVING count(id) > 0 ORDER BY pts DESC
`)
checkErr(err)
defer rows.Close()
var statsList []KudosStats
i := 1
var max float32 = 0
for rows.Next() {
var memberFrom string
var pts int
err = rows.Scan(&memberFrom, &pts)
checkErr(err)
if i == 1 {
max = float32(pts)
}
statsList = append(statsList, generateStatsResult(memberFrom, pts, i, max))
i = i + 1
}
return statsList
}
func loadKudosReceivedList() []KudosStats {
rows, err := db.Query(`
SELECT userTo, SUM(value) as pts FROM kudos ku
INNER JOIN kudos_receiver kr ON kr.kudos_id = ku.id
WHERE ku.date > date('now','-1 year')
GROUP by userTo HAVING SUM(value) > 0 ORDER BY pts DESC
`)
checkErr(err)
defer rows.Close()
var statsList []KudosStats
i := 1
var max float32 = 0
for rows.Next() {
var memberFrom string
var pts int
err = rows.Scan(&memberFrom, &pts)
checkErr(err)
if i == 1 {
max = float32(pts)
}
statsList = append(statsList, generateStatsResult(memberFrom, pts, i, max))
i = i + 1
}
return statsList
}
func loadKudosReceivedByUser(userId string) []Kudo {
stmt, err := db.Prepare(`SELECT ku.id, ku.userFrom, ku.message, ku.value, ku.date
FROM kudos ku
JOIN kudos_receiver ON ku.id = kudos_receiver.kudos_id
WHERE userTo = ?
ORDER BY ku.date DESC
`)
checkErr(err)
rows, err := stmt.Query(userId)
defer rows.Close()
var kudos []Kudo
for rows.Next() {
var kudo Kudo
var memberFrom, date string
err = rows.Scan(&kudo.ID, &memberFrom, &kudo.Text, &kudo.Value, &date)
kudo.MemberFrom, _ = findMemberByID(memberFrom)
kudo.Date, _ = time.Parse(time.RFC3339, date)
kudos = append(kudos, kudo)
}
return kudos
}
func generateStatsResult(memberFrom string, pts int, i int, max float32) KudosStats {
prc := float32(pts * 85) / max
if prc < 0 {
prc = 0
}
var kudoStats KudosStats
kudoStats.Member, _ = findMemberByID(memberFrom)
kudoStats.Pts = pts
kudoStats.Position = i
kudoStats.HasCrown = i == 1
kudoStats.Prc = prc
return kudoStats
}
func checkErr(err error) {
if err != nil {
panic(err)
}
}