-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathstats_repository.go
43 lines (35 loc) · 1.1 KB
/
stats_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
package margelet
import (
"fmt"
"strconv"
"strings"
"gopkg.in/redis.v3"
)
// StatsRepository - public interface for session repository
type StatsRepository interface {
Incr(chatID int64, userID int, name string)
Get(chatID int64, userID int, name string) int
}
type statsRepository struct {
key string
redis *redis.Client
}
func newStatsRepository(prefix string, redis *redis.Client) *statsRepository {
key := strings.Join([]string{prefix, "margelet_sessions"}, "-")
return &statsRepository{key, redis}
}
// Inc - adds user's answer to existing session
func (stats *statsRepository) Incr(chatID int64, userID int, name string) {
key := stats.keyFor(chatID, userID, name)
stats.redis.Incr(key)
}
// Get - adds user's answer to existing session
func (stats *statsRepository) Get(chatID int64, userID int, name string) int {
key := stats.keyFor(chatID, userID, name)
value, _ := stats.redis.Get(key).Result()
v, _ := strconv.Atoi(value)
return v
}
func (stats *statsRepository) keyFor(chatID int64, userID int, name string) string {
return fmt.Sprintf("%s_%d_%d_%s", stats.key, chatID, userID, name)
}