-
Notifications
You must be signed in to change notification settings - Fork 2
/
repo.go
87 lines (71 loc) · 1.82 KB
/
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
package main
import (
"encoding/json"
"errors"
"fmt"
"gopkg.in/redis.v3"
)
type repo interface {
GetSudoku(string) (Sudoku, error)
RandomSudoku() (Sudoku, error)
CreateSudoku([81]int, [81]int) (Sudoku, error)
GetPuzzleCount() int
}
type redisRepo struct {
client *redis.Client
}
var puzzlesKey = "puzzles"
var listKey = "puzzleList"
func newRedisRepo(addr string) redisRepo {
fmt.Println("Connecting to redis on", addr)
client := redis.NewClient(&redis.Options{
Addr: addr,
Password: "", // no password set
DB: 0, // use default DB
})
_, err := client.Ping().Result()
if err != nil {
panic(err)
}
return redisRepo{client: client}
}
func (r redisRepo) GetPuzzleCount() int {
return int(r.client.SCard(listKey).Val())
}
func (r redisRepo) RandomSudoku() (Sudoku, error) {
id, err := r.client.SRandMember(listKey).Bytes()
if err != nil {
return Sudoku{}, errors.New("No puzzles in list")
}
return r.GetSudoku(string(id))
}
func (r redisRepo) GetSudoku(id string) (Sudoku, error) {
var sudoku Sudoku
bs, err := r.client.Get(puzzlesKey + id).Bytes()
if err != nil {
return Sudoku{}, errors.New("No puzzle found error")
}
if err = json.Unmarshal(bs, &sudoku); err != nil {
return Sudoku{}, errors.New("Puzzle unmarshal error")
}
return sudoku, nil
}
func (r redisRepo) CreateSudoku(puzzle, solution [81]int) (Sudoku, error) {
sudoku := newSudoku(puzzle, solution)
_, err := r.GetSudoku(sudoku.ID)
if err == nil {
return sudoku, errors.New("Puzzle already exists")
}
b, err := json.Marshal(sudoku)
if err != nil {
fmt.Println(err)
return sudoku, errors.New("Cannot parse params")
}
r.client.SAdd(listKey, sudoku.ID)
r.client.Set(puzzlesKey+sudoku.ID, string(b), 0)
if err != nil {
fmt.Println(err)
return sudoku, errors.New("Failed to persist puzzle")
}
return sudoku, nil
}