-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfirestore.go
136 lines (120 loc) · 3.09 KB
/
firestore.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
package firestore
import (
"context"
"cloud.google.com/go/firestore"
firebase "firebase.google.com/go"
"github.com/kyleshepherd/discord-tk-bot/internal/storage"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
type (
KillStore struct {
client *firestore.Client
}
)
func NewKillStore(ctx context.Context, projectID string, credentialsFilePath string) (*KillStore, error) {
conf := &firebase.Config{ProjectID: projectID}
var app *firebase.App
if credentialsFilePath != "" {
var err error
opt := option.WithCredentialsFile(credentialsFilePath)
app, err = firebase.NewApp(ctx, conf, opt)
if err != nil {
return nil, err
}
} else {
var err error
app, err = firebase.NewApp(ctx, conf)
if err != nil {
return nil, err
}
}
client, err := app.Firestore(ctx)
if err != nil {
return nil, err
}
return &KillStore{
client: client,
}, nil
}
func (s *KillStore) GetKillByID(ctx context.Context, id string) (*storage.Kill, error) {
dsnap, err := s.client.Collection("kills").Doc(id).Get(ctx)
if err != nil && status.Code(err) == codes.NotFound {
return nil, &storage.ErrNotFound{Key: id}
}
if err != nil {
return nil, err
}
var k storage.Kill
err = dsnap.DataTo(&k)
if err != nil {
return nil, err
}
return &k, nil
}
func (s *KillStore) CreateKill(ctx context.Context, kill *storage.Kill) (*storage.Kill, error) {
ref := s.client.Collection("kills").NewDoc()
kill.ID = ref.ID
_, err := ref.Set(ctx, kill)
if err != nil {
return nil, err
}
return kill, nil
}
func (s *KillStore) DeleteKill(ctx context.Context, id string) error {
_, err := s.client.Collection("kills").Doc(id).Delete(ctx)
if err != nil {
return err
}
return nil
}
func (s *KillStore) DeleteKillsForServer(ctx context.Context, serverId string) error {
iter := s.client.Collection("kills").Where("serverId", "==", serverId).Documents(ctx)
defer iter.Stop()
for {
doc, err := iter.Next()
if err == iterator.Done {
break
}
if err != nil {
return err
}
_, err = doc.Ref.Delete(ctx)
if err != nil {
return err
}
}
return nil
}
func (s *KillStore) ListKillsForServer(ctx context.Context, serverId string) ([]*storage.Kill, error) {
iter := s.client.Collection("kills").Where("serverId", "==", serverId).OrderBy("date", firestore.Desc).Documents(ctx)
return iterateKills(iter)
}
func (s *KillStore) ListPlayerKillsForServer(ctx context.Context, serverId string, killerId string) ([]*storage.Kill, error) {
iter := s.client.Collection("kills").Where("serverId", "==", serverId).Where("killer", "==", killerId).OrderBy("date", firestore.Desc).Documents(ctx)
return iterateKills(iter)
}
func iterateKills(iter *firestore.DocumentIterator) ([]*storage.Kill, error) {
kills := []*storage.Kill{}
defer iter.Stop()
for {
doc, err := iter.Next()
if err == iterator.Done {
break
}
if err != nil {
return nil, err
}
var k *storage.Kill
if err := doc.DataTo(&k); err != nil {
return nil, err
}
kills = append(kills, k)
}
return kills, nil
}
func (s *KillStore) Close() {
s.client.Close()
}