-
Notifications
You must be signed in to change notification settings - Fork 0
/
users.go
214 lines (168 loc) · 4.08 KB
/
users.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package main
import (
"encoding/json"
"fmt"
"github.com/stefanhans/gcp-serverless-b8e-play/Play/firebase/commandline/types"
"google.golang.org/api/iterator"
"io/ioutil"
"log"
)
func AddUser(arguments []string) {
_ = arguments
if client == nil {
fmt.Printf("not connected - use %q\n", "connect")
return
}
collection = "users"
doc := client.Collection(collection).NewDoc()
wr, err := doc.Create(ctx, types.User{
DocId: doc.ID,
Name: "Tesla Deluxe",
Type: "eCar",
Status: "ready",
Description: "just a test description",
})
if err != nil {
fmt.Printf("failed to create document: %v\n", err)
}
fmt.Println(wr.UpdateTime)
}
func GetUser(arguments []string) {
if len(arguments) == 0 {
fmt.Printf("argument 'doc-id' missing\n")
return
}
docId := arguments[0]
if client == nil {
fmt.Printf("not connected - use %q\n", "connect")
return
}
collection = "users"
q := client.Collection(collection).
Where("doc-id", "==", docId).
Limit(1)
iter := q.Documents(ctx)
doc, err := iter.Next()
if err != nil {
log.Fatalf("Failed to iterate: %v", err)
}
fmt.Println(doc.Data())
var user types.User
if err := doc.DataTo(&user); err != nil {
fmt.Printf("failed to convert user: %v\n", err)
}
fmt.Printf("user: \n%v\n", user)
jsonUsers, err := json.MarshalIndent(user, "", " ")
if err != nil {
fmt.Printf("failed to marshall 'userRequest': %v\n", err)
return
}
fmt.Printf("userRequest: %s\n", jsonUsers)
}
func StoreUsers(arguments []string) {
_ = arguments
if client == nil {
fmt.Printf("not connected - use %q\n", "connect")
return
}
collection = "users"
jsonUsers, err := ioutil.ReadFile("data/users.json")
if err != nil {
fmt.Printf("could not read file %q: %v\n", "data/users.json", err)
return
}
fmt.Printf(string(jsonUsers))
// Declared an empty map interface
var users []types.User
// Unmarshal or Decode the JSON to the interface.
err = json.Unmarshal([]byte(jsonUsers), &users)
if err != nil {
fmt.Printf("failed to unmarshal 'users': %v\n", err)
}
fmt.Printf("users: %v\n", users)
for i, user := range users {
doc := client.Collection(collection).NewDoc()
user.DocId = doc.ID
wr, err := doc.Create(ctx, user)
if err != nil {
fmt.Printf("failed to create document #%v: %v\n", i, err)
}
fmt.Println(wr.UpdateTime)
}
}
func ClearUsers(arguments []string) {
_ = arguments
if client == nil {
fmt.Printf("not connected - use %q\n", "connect")
return
}
collection = "users"
ref := client.Collection(collection)
batchSize := 20
for {
// Get a batch of documents
iter := ref.Limit(batchSize).Documents(ctx)
numDeleted := 0
// Iterate through the documents, adding
// a delete operation for each one to a
// WriteBatch.
batch := client.Batch()
for {
doc, err := iter.Next()
if err == iterator.Done {
break
}
if err != nil {
fmt.Printf("failed next iteration: %v\n", err)
return
}
batch.Delete(doc.Ref)
numDeleted++
}
// If there are no documents to delete,
// the process is over.
if numDeleted == 0 {
fmt.Printf("all %s deleted\n", collection)
return
}
result, err := batch.Commit(ctx)
if err != nil {
fmt.Printf("failed to commit batch for deletion: %v\n", err)
return
}
_ = result
//fmt.Printf("Commit Result: %v\n", result)
}
}
func GetUsers(arguments []string) {
_ = arguments
if client == nil {
fmt.Printf("not connected - use %q\n", "connect")
return
}
collection = "users"
users := make([]types.User, 0)
iter := client.Collection(collection).Documents(ctx)
for {
doc, err := iter.Next()
if err == iterator.Done {
break
}
if err != nil {
log.Fatalf("Failed to iterate: %v", err)
}
fmt.Println(doc.Data())
var data types.User
if err := doc.DataTo(&data); err != nil {
fmt.Printf("failed to convert data: %v\n", err)
}
users = append(users, data)
}
fmt.Printf("users: \n%v\n", users)
jsonUsers, err := json.MarshalIndent(users, "", " ")
if err != nil {
fmt.Printf("failed to marshall 'userRequest': %v\n", err)
return
}
fmt.Printf("userRequest: %s\n", jsonUsers)
}