-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_layer.go
125 lines (96 loc) · 2.72 KB
/
data_layer.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
package main
import (
"context"
"github.com/jackc/pgx/v4"
)
type dataLayer interface {
addUser(username string, guildID string) error
getTopTwentyUsers(guildID string) ([]user, error)
checkUserExists(username string, guildID string) (int, error)
addWin(username string, guildID string) error
updateGameStats(username string, points string, guildID string) error
}
type postgresDataLayer struct {
dbConn *pgx.Conn
}
func (db *postgresDataLayer) addUser(username string, guildID string) error {
_, err := db.dbConn.Exec(context.Background(), "INSERT INTO users (username, guild_id) VALUES ($1, $2)", username, guildID)
return err
}
func (db *postgresDataLayer) getTopTwentyUsers(guildID string) ([]user, error) {
rows, err := db.dbConn.Query(
context.Background(),
`SELECT
CAST(RANK() OVER (ORDER BY games_won DESC) AS TEXT), username, CAST(games_won AS TEXT) ,
CAST(points AS TEXT), CAST(games AS TEXT), points_per_game
FROM users
WHERE guild_id = ($1)
LIMIT 20`,
guildID,
)
defer rows.Close()
if err != nil {
return nil, err
}
users := make([]user, 0, 20)
for i := 0; rows.Next(); i++ {
user := user{}
err = rows.Scan(
&user.rank,
&user.username,
&user.victories,
&user.points,
&user.games,
&user.pointsPerGame,
)
if err != nil {
return nil, err
}
users = append(users, user)
}
if rows.Err() != nil {
return nil, rows.Err()
}
return users, nil
}
func (db *postgresDataLayer) checkUserExists(username string, guildID string) (int, error) {
row := db.dbConn.QueryRow(context.Background(), "SELECT COUNT(*) FROM users WHERE username = ($1) AND guild_id = ($2);", username, guildID)
var recordExists int
err := row.Scan(&recordExists)
if err != nil {
return -1, err
}
return recordExists, nil
}
func (db *postgresDataLayer) addWin(username string, guildID string) error {
_, err := db.dbConn.Exec(context.Background(), "UPDATE users SET games_won = games_won + 1 WHERE username = ($1) AND guild_id = ($2)", username, guildID)
return err
}
func (db *postgresDataLayer) updateGameStats(username string, points string, guildID string) error {
tx, err := db.dbConn.Begin(context.Background())
if err != nil {
return err
}
defer tx.Rollback(context.Background())
_, err = tx.Exec(
context.Background(),
`UPDATE users
SET
points = points + ($1),
games = games + 1
WHERE username = ($2) AND guild_id = ($3)`,
points, username, guildID,
)
if err != nil {
return err
}
_, err = tx.Exec(context.Background(), "UPDATE users SET points_per_game = CAST(points as real) / games WHERE username = ($1) AND guild_id = ($2)", username, guildID)
if err != nil {
return err
}
err = tx.Commit(context.Background())
if err != nil {
return err
}
return nil
}