-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleDocument.cs
More file actions
114 lines (92 loc) · 3.65 KB
/
SimpleDocument.cs
File metadata and controls
114 lines (92 loc) · 3.65 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
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace ArangoDbPoc
{
public class SimpleDocument : Flow
{
protected override async Task StartInternalFlow()
{
await NextAsync("Ready to create a document.", CreateDocument);
await NextAsync("Ready to query a document.", QueryDocument);
await NextAsync("Ready to update a document.", UpdateDocument);
await NextAsync("Ready to query a document.", QueryDocument);
await NextAsync("Ready to remove a document.", DeleteDocument);
}
private static async Task CreateDocument()
{
try
{
//Truncate. Make sure we only have 1 document at this stage.
await Arango.Collection.TruncateAsync(DatabaseName, CollectionName);
Console.WriteLine("Creating 'Ned Stark' document ...");
var ned = new Character("Ned", "Stark", true, 41, new List<string> { "A", "H", "C", "N", "P" });
await Arango.Document.CreateAsync(DatabaseName, CollectionName, ned);
Console.WriteLine("Document created.");
}
catch (Exception e)
{
Console.WriteLine("Failed to create document. " + e);
}
}
private static async Task QueryDocument()
{
try
{
Console.WriteLine("Querying 'Ned Stark' document ...");
const string name = "Ned";
FormattableString query = $"FOR c IN {CollectionName:@} FILTER c.name == {name} RETURN c";
var result = await Arango.Query.ExecuteAsync<Character>(DatabaseName, $"{query}");
foreach (var character in result)
{
string json = JsonConvert.SerializeObject(character, Formatting.Indented);
Console.WriteLine(json);
}
Console.WriteLine("Document retrieved.");
}
catch (Exception e)
{
Console.WriteLine("Error occured while retrieving document. " + e);
}
}
private static async Task UpdateDocument()
{
try
{
Console.WriteLine("Updating 'Ned Stark' document ...");
const string name = "Ned";
//Retrieve the key
var key = await GetKey($"c.name == {name}", CollectionName);
//Kill Ned Stark
Console.WriteLine("Killing 'Ned Stark' ...");
await Arango.Document.UpdateAsync(DatabaseName, CollectionName, new
{
Key = key,
Alive = false
});
Console.WriteLine("'Ned Stark' document updated.");
}
catch (Exception e)
{
Console.WriteLine("Error occured while modifying document. " + e);
}
}
private static async Task DeleteDocument()
{
try
{
Console.WriteLine("Deleting 'Ned Stark' document ...");
const string name = "Ned";
//Retrieve the key
var key = await GetKey($"c.name == {name}", CollectionName);
await Arango.Document.DeleteAsync<Character>(DatabaseName, CollectionName, key);
Console.WriteLine("'Ned Stark' document removed.");
}
catch (Exception e)
{
Console.WriteLine("Error occured while removing document. " + e);
}
}
}
}