-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlite_karma_repo.go
173 lines (153 loc) · 3.36 KB
/
sqlite_karma_repo.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
package main
import (
"database/sql"
"io/ioutil"
"path"
"runtime"
"strings"
"time"
)
const InsertQuery = `
INSERT INTO karma
(giver, receiver, count, channel)
VALUES
(?, ?, ?, ?)
`
const RankingQuery = `
SELECT {kind} as user, sum(count) as count
FROM karma
WHERE ? < date and date <= ?
GROUP BY {kind}
ORDER BY sum(count) desc
`
const UserPutQuery = `
INSERT OR REPLACE INTO user VALUES (?, ?, ?, ?, ?)
`
const UserFindByIdQuery = `SELECT id, name, display_name, team_id, image_url FROM user where id = ?`
const UserFindByNameQuery = `SELECT id, name, display_name, team_id, image_url FROM user where name = ?`
type SQLiteKarmaRepo struct {
db *sql.DB
}
type SQLiteUserRepo struct {
db *sql.DB
}
func getKarmaDb (dataDir string) (*sql.DB, error) {
if dataDir == "" {
dataDir = "."
}
db, err := sql.Open("sqlite3", path.Join(dataDir, "karma.db"))
if err != nil {
return nil, err
}
_, filename, _, _ := runtime.Caller(1)
raw, err := ioutil.ReadFile(path.Join(path.Dir(filename), "create_table.sql"))
if err != nil {
return nil, err
}
query := string(raw)
_, err = db.Exec(query)
if err != nil {
return nil, err
}
return db, nil
}
func NewSQLiteKarmaRepo(dataDir string) UserKarmaRepo {
db, err := getKarmaDb(dataDir)
if err != nil {
panic(nil)
}
repo := &SQLiteKarmaRepo{db}
return repo
}
/*
Implements KarmaRepo
*/
func (s *SQLiteKarmaRepo) Save(karmaList []Karma) error {
stmt, err := s.db.Prepare(InsertQuery)
if err != nil {
return err
}
defer stmt.Close()
for _, k := range karmaList {
if k.Giver == "" {
continue
}
if k.Receiver == "" {
continue
}
_, err := stmt.Exec(k.Giver, k.Receiver, k.Count, k.Channel)
if err != nil {
return err
}
}
return nil
}
func (s *SQLiteKarmaRepo) Ranking(kind KarmaAggregateKind, from time.Time, to time.Time) (KarmaRanking, error) {
ranking := KarmaRanking{ Kind: kind}
ranking.Ranks = make([]KarmaAggregate, 0)
query := strings.ReplaceAll(RankingQuery, "{kind}", string(kind))
stmt, err := s.db.Prepare(query)
if err != nil {
return ranking, err
}
defer stmt.Close()
rows, err := stmt.Query(from, to)
if err != nil {
return ranking, err
}
var user string
var count float64
for rows.Next() {
err := rows.Scan(&user, &count)
agg := KarmaAggregate{
User: user,
Count: count,
}
if err != nil {
return ranking, err
}
ranking.Ranks = append(ranking.Ranks, agg)
}
return ranking, nil
}
/*
Implements UserRepo
*/
func (s *SQLiteKarmaRepo) Put(user User) error {
stmt, err := s.db.Prepare(UserPutQuery)
if err != nil {
return err
}
defer stmt.Close()
_, err = stmt.Exec(user.Id, user.Name, user.DisplayName, user.TeamId, user.ImageURL)
if err != nil {
return err
}
return nil
}
func (s *SQLiteKarmaRepo) GetById(id string) (user User, err error) {
stmt, err := s.db.Prepare(UserFindByIdQuery)
if err != nil {
return
}
defer stmt.Close()
row := stmt.QueryRow(id)
err = row.Scan(&user.Id, &user.Name, &user.DisplayName, &user.TeamId, &user.ImageURL)
if err != nil {
return
}
return user, nil
}
func (s *SQLiteKarmaRepo) GetByName(name string) (user User, err error) {
stmt, err := s.db.Prepare(UserFindByNameQuery)
if err != nil {
return
}
defer stmt.Close()
row := stmt.QueryRow(name)
err = row.Scan(&user.Id, &user.Name, &user.DisplayName, &user.TeamId, &user.ImageURL)
if err != nil {
return
}
return user, nil
}