-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Program.cs
263 lines (241 loc) · 9.95 KB
/
Program.cs
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// Copyright(c) 2017 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.
using CommandLine;
using Google.Cloud.Firestore;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Threading;
using System.Linq;
namespace GoogleCloudSamples
{
public class GetData
{
public static string Usage = @"Usage:
C:\> dotnet run command YOUR_PROJECT_ID
Where command is one of
retrieve-create-examples
get-doc-as-map
get-doc-as-entity
get-multiple-docs
get-all-docs
add-subcollection
get-collections
";
private static async Task RetrieveCreateExamples(string project)
{
FirestoreDb db = FirestoreDb.Create(project);
// [START fs_retrieve_create_examples]
CollectionReference citiesRef = db.Collection("cities");
await citiesRef.Document("SF").SetAsync(new Dictionary<string, object>(){
{ "Name", "San Francisco" },
{ "State", "CA" },
{ "Country", "USA" },
{ "Capital", false },
{ "Population", 860000 }
});
await citiesRef.Document("LA").SetAsync(new Dictionary<string, object>(){
{ "Name", "Los Angeles" },
{ "State", "CA" },
{ "Country", "USA" },
{ "Capital", false },
{ "Population", 3900000 }
});
await citiesRef.Document("DC").SetAsync(new Dictionary<string, object>(){
{ "Name", "Washington D.C." },
{ "State", null },
{ "Country", "USA" },
{ "Capital", true },
{ "Population", 680000 }
});
await citiesRef.Document("TOK").SetAsync(new Dictionary<string, object>(){
{ "Name", "Tokyo" },
{ "State", null },
{ "Country", "Japan" },
{ "Capital", true },
{ "Population", 9000000 }
});
await citiesRef.Document("BJ").SetAsync(new Dictionary<string, object>(){
{ "Name", "Beijing" },
{ "State", null },
{ "Country", "China" },
{ "Capital", true },
{ "Population", 21500000 }
});
Console.WriteLine("Added example cities data to the cities collection.");
// [END fs_retrieve_create_examples]
}
private static async Task GetDocAsMap(string project)
{
FirestoreDb db = FirestoreDb.Create(project);
// [START fs_get_doc_as_map]
DocumentReference docRef = db.Collection("cities").Document("SF");
DocumentSnapshot snapshot = await docRef.GetSnapshotAsync();
if (snapshot.Exists)
{
Console.WriteLine("Document data for {0} document:", snapshot.Id);
Dictionary<string, object> city = snapshot.ToDictionary();
foreach (KeyValuePair<string, object> pair in city)
{
Console.WriteLine("{0}: {1}", pair.Key, pair.Value);
}
}
else
{
Console.WriteLine("Document {0} does not exist!", snapshot.Id);
}
// [END fs_get_doc_as_map]
}
// A custom City class used by the GetDocAsEntity function.
[FirestoreData]
public class City
{
[FirestoreProperty]
public string Name { get; set; }
[FirestoreProperty]
public string State { get; set; }
[FirestoreProperty]
public string Country { get; set; }
[FirestoreProperty]
public bool Capital { get; set; }
[FirestoreProperty]
public long Population { get; set; }
}
private static async Task GetDocAsEntity(string project)
{
FirestoreDb db = FirestoreDb.Create(project);
// [START fs_get_doc_as_entity]
DocumentReference docRef = db.Collection("cities").Document("BJ");
DocumentSnapshot snapshot = await docRef.GetSnapshotAsync();
if (snapshot.Exists)
{
Console.WriteLine("Document data for {0} document:", snapshot.Id);
City city = snapshot.ConvertTo<City>();
Console.WriteLine("Name: {0}", city.Name);
Console.WriteLine("State: {0}", city.State);
Console.WriteLine("Country: {0}", city.Country);
Console.WriteLine("Capital: {0}", city.Capital);
Console.WriteLine("Population: {0}", city.Population);
}
else
{
Console.WriteLine("Document {0} does not exist!", snapshot.Id);
}
// [END fs_get_doc_as_entity]
}
private static async Task GetMultipleDocs(string project)
{
FirestoreDb db = FirestoreDb.Create(project);
// [START fs_get_multiple_docs]
Query capitalQuery = db.Collection("cities").WhereEqualTo("Capital", true);
QuerySnapshot capitalQuerySnapshot = await capitalQuery.GetSnapshotAsync();
foreach (DocumentSnapshot documentSnapshot in capitalQuerySnapshot.Documents)
{
Console.WriteLine("Document data for {0} document:", documentSnapshot.Id);
Dictionary<string, object> city = documentSnapshot.ToDictionary();
foreach (KeyValuePair<string, object> pair in city)
{
Console.WriteLine("{0}: {1}", pair.Key, pair.Value);
}
Console.WriteLine("");
}
// [END fs_get_multiple_docs]
}
private static async Task GetAllDocs(string project)
{
FirestoreDb db = FirestoreDb.Create(project);
// [START fs_get_all_docs]
Query allCitiesQuery = db.Collection("cities");
QuerySnapshot allCitiesQuerySnapshot = await allCitiesQuery.GetSnapshotAsync();
foreach (DocumentSnapshot documentSnapshot in allCitiesQuerySnapshot.Documents)
{
Console.WriteLine("Document data for {0} document:", documentSnapshot.Id);
Dictionary<string, object> city = documentSnapshot.ToDictionary();
foreach (KeyValuePair<string, object> pair in city)
{
Console.WriteLine("{0}: {1}", pair.Key, pair.Value);
}
Console.WriteLine("");
}
// [END fs_get_all_docs]
}
private static async Task AddSubcollection(string project)
{
FirestoreDb db = FirestoreDb.Create(project);
// [START fs_add_subcollection]
DocumentReference cityRef = db.Collection("cities").Document("SF");
CollectionReference subcollectionRef = cityRef.Collection("neighborhoods");
Dictionary<string, object> data = new Dictionary<string, object>
{
{ "name", "Marina" },
};
await subcollectionRef.Document("Marina").SetAsync(data);
// [END fs_add_subcollection]
Console.WriteLine("Added data to the Marina document in the neighborhoods subcollection in the SF document in the cities collection.");
}
private static async Task GetCollections(string project)
{
FirestoreDb db = FirestoreDb.Create(project);
// [START fs_get_collections]
DocumentReference cityRef = db.Collection("cities").Document("SF");
IAsyncEnumerable<CollectionReference> subcollections = cityRef.ListCollectionsAsync();
IAsyncEnumerator<CollectionReference> subcollectionsEnumerator = subcollections.GetAsyncEnumerator(default);
while (await subcollectionsEnumerator.MoveNextAsync())
{
CollectionReference subcollectionRef = subcollectionsEnumerator.Current;
Console.WriteLine("Found subcollection with ID: {0}", subcollectionRef.Id);
}
// [END fs_get_collections]
}
public static void Main(string[] args)
{
if (args.Length < 2)
{
Console.Write(Usage);
return;
}
string command = args[0].ToLower();
string project = string.Join(" ",
new ArraySegment<string>(args, 1, args.Length - 1));
switch (command)
{
case "retrieve-create-examples":
RetrieveCreateExamples(project).Wait();
break;
case "get-doc-as-map":
GetDocAsMap(project).Wait();
break;
case "get-doc-as-entity":
GetDocAsEntity(project).Wait();
break;
case "get-multiple-docs":
GetMultipleDocs(project).Wait();
break;
case "get-all-docs":
GetAllDocs(project).Wait();
break;
case "add-subcollection":
AddSubcollection(project).Wait();
break;
case "get-collections":
GetCollections(project).Wait();
break;
default:
Console.Write(Usage);
return;
}
}
}
}