-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbookings.go
245 lines (203 loc) · 5.21 KB
/
bookings.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package main
import (
"encoding/json"
"io/ioutil"
//"encoding/json"
"fmt"
"github.com/stefanhans/gcp-serverless-b8e-play/Play/firebase/commandline/types"
"google.golang.org/api/iterator"
"log"
"time"
)
func AddBooking(arguments []string) {
_ = arguments
if client == nil {
fmt.Printf("not connected - use %q\n", "connect")
return
}
collection = "bookings"
doc := client.Collection(collection).NewDoc()
wr, err := doc.Create(ctx, types.Booking{
DocId: doc.ID,
User: "Alice",
Vehicle: "Tesla Deluxe",
VehicleType: "eCar",
VehicleStatus: "",
ParkingLot: "",
From: time.Now(),
To: time.Now().Add(time.Hour * 2),
Status: "request",
StatusTime: time.Now(),
})
if err != nil {
fmt.Printf("failed to create document: %v\n", err)
}
fmt.Println(wr.UpdateTime)
//jsonRequest, err := json.MarshalIndent(item, "", " ")
//if err != nil {
// fmt.Printf("failed to marshall 'jsonRequest': %v\n", err)
// return
//}
//fmt.Printf("jsonRequest: %s\n", jsonRequest)
//
//// Declared an empty map interface
//var result map[string]interface{}
//
//// Unmarshal or Decode the JSON to the interface.
//json.Unmarshal([]byte(string(jsonRequest)), &result)
//
//// [START fs_add_data_1]
//_, _, err = client.Collection(collection).Add(ctx, result)
//if err != nil {
// log.Fatalf("Failed adding document to collection %q: %v", collection, err)
//}
//
//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())
//}
}
func StoreBookings(arguments []string) {
_ = arguments
if client == nil {
fmt.Printf("not connected - use %q\n", "connect")
return
}
collection = "bookings"
jsonBookings, err := ioutil.ReadFile("data/bookings.json")
if err != nil {
fmt.Printf("could not read file %q: %v\n", "data/bookings.json", err)
return
}
fmt.Printf(string(jsonBookings))
// Declared an empty map interface
var bookings []types.Booking
// Unmarshal or Decode the JSON to the interface.
err = json.Unmarshal([]byte(jsonBookings), &bookings)
if err != nil {
fmt.Printf("failed to unmarshal 'bookings': %v\n", err)
}
fmt.Printf("bookings: %v\n", bookings)
for i, booking := range bookings {
doc := client.Collection(collection).NewDoc()
booking.DocId = doc.ID
wr, err := doc.Create(ctx, booking)
if err != nil {
fmt.Printf("failed to create document #%v: %v\n", i, err)
}
fmt.Println(wr.UpdateTime)
}
//jsonRequest, err := json.MarshalIndent(item, "", " ")
//if err != nil {
// fmt.Printf("failed to marshall 'jsonRequest': %v\n", err)
// return
//}
//fmt.Printf("jsonRequest: %s\n", jsonRequest)
//
//// Declared an empty map interface
//var result map[string]interface{}
//
//// Unmarshal or Decode the JSON to the interface.
//json.Unmarshal([]byte(string(jsonRequest)), &result)
//
//// [START fs_add_data_1]
//_, _, err = client.Collection(collection).Add(ctx, result)
//if err != nil {
// log.Fatalf("Failed adding document to collection %q: %v", collection, err)
//}
//
//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())
//}
}
func ClearBookings(arguments []string) {
_ = arguments
if client == nil {
fmt.Printf("not connected - use %q\n", "connect")
return
}
collection = "bookings"
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 GetBookings(arguments []string) {
_ = arguments
if client == nil {
fmt.Printf("not connected - use %q\n", "connect")
return
}
collection = "bookings"
bookings := make([]types.Booking, 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.Booking
if err := doc.DataTo(&data); err != nil {
fmt.Printf("failed to convert data: %v\n", err)
}
bookings = append(bookings, data)
}
fmt.Printf("bookings: \n%v\n", bookings)
jsonBookings, err := json.MarshalIndent(bookings, "", " ")
if err != nil {
fmt.Printf("failed to marshall 'bookingRequest': %v\n", err)
return
}
fmt.Printf("bookingRequest: %s\n", jsonBookings)
}