-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlow.cs
More file actions
164 lines (137 loc) · 5.01 KB
/
Flow.cs
File metadata and controls
164 lines (137 loc) · 5.01 KB
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
using Core.Arango;
using Core.Arango.Protocol;
using Core.Arango.Serialization.Newtonsoft;
using System;
using System.Linq;
using System.Threading.Tasks;
namespace ArangoDbPoc
{
public abstract class Flow
{
protected static IArangoContext Arango;
protected const string DatabaseName = "GameOfThrones";
protected const string CollectionName = "Characters";
public async Task StartFlow()
{
Console.WriteLine();
Connect();
await CreateDatabase();
await CreateCollection();
await StartInternalFlow();
}
protected abstract Task StartInternalFlow();
protected static void Connect()
{
try
{
var serializer = new ArangoNewtonsoftSerializer(new ArangoNewtonsoftCamelCaseContractResolver());
var configuration = new ArangoConfiguration { Serializer = serializer };
Console.WriteLine("Connecting to Arango DB ...");
Arango = new ArangoContext(
"Server=http://localhost:8529;User=root;Password=openSesame;",
configuration);
Console.WriteLine("Connected.");
}
catch (Exception e)
{
Console.WriteLine("Failed to connect. " + e);
}
Console.WriteLine();
}
protected static async Task CreateDatabase()
{
try
{
var exists = await Arango.Database.ExistAsync(DatabaseName);
if (exists)
{
Console.WriteLine($"'{DatabaseName}' database already exists. No database created.");
}
else
{
Console.WriteLine("Creating 'GameOfThrones' Database ...");
await Arango.Database.CreateAsync(DatabaseName);
Console.WriteLine("Database created.");
}
}
catch (Exception e)
{
Console.WriteLine("Failed to create database. " + e);
}
Console.WriteLine();
}
protected static async Task CreateCollection()
{
try
{
var exists = await Arango.Collection.ExistAsync(DatabaseName, CollectionName);
if (exists)
{
Console.WriteLine($"'{CollectionName}' collection already exists. No collection created.");
}
else
{
Console.WriteLine($"Creating '{CollectionName}' collection ...");
await Arango.Collection.CreateAsync(DatabaseName, CollectionName, ArangoCollectionType.Document);
Console.WriteLine("Collection created.");
}
}
catch (Exception e)
{
Console.WriteLine("Failed to create collection. " + e);
}
Console.WriteLine();
}
protected static async Task<string> GetKey(FormattableString filterPart, string collectionName)
{
var idResult = await Arango.Query.ExecuteAsync<string>(
DatabaseName, $"FOR c IN {collectionName:@} FILTER {filterPart} RETURN c._key");
return idResult.Count switch
{
0 => throw new InvalidOperationException("Filter returned no result."),
> 1 => throw new InvalidOperationException("Filter returned more than one result. " +
"Please refine filter. Result of query cannot " +
"contain more than one result."),
_ => idResult.First()
};
}
protected static void Next(string message, Action action)
{
DisplayMessage(message);
var keyInfo = Console.ReadKey();
if (keyInfo.Key == ConsoleKey.Enter)
{
action();
}
else
{
Console.WriteLine();
Next("Invalid key. " + message, action);
}
Console.WriteLine();
}
protected static async Task NextAsync(string message, Func<Task> action)
{
DisplayMessage(message);
var keyInfo = Console.ReadKey();
if (keyInfo.Key == ConsoleKey.Enter)
{
await action();
}
else
{
Console.WriteLine();
await NextAsync("Invalid key. " + message, action);
}
Console.WriteLine();
}
private static void DisplayMessage(string message)
{
Console.ForegroundColor = ConsoleColor.DarkCyan;
Console.WriteLine(message);
Console.WriteLine("Press enter to continue ...");
Console.WriteLine();
Console.ResetColor();
}
}
}