-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGetItem.cs
112 lines (98 loc) · 4.07 KB
/
GetItem.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
using AdaptiveExpressions.Properties;
using Microsoft.Bot.Builder.Dialogs;
using Newtonsoft.Json;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos;
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
namespace Iciclecreek.Bot.Builder.Dialogs.Database.Cosmos
{
/// <summary>
/// Create cosmos db item in container
/// </summary>
[Description("Get item in a cosmos container")]
public class GetItem : Dialog
{
[JsonProperty("$kind")]
public const string Kind = "Iciclecreek.Cosmos.GetItem";
[JsonConstructor]
public GetItem([CallerFilePath] string callerPath = "", [CallerLineNumber] int callerLine = 0)
{
this.RegisterSourceLocation(callerPath, callerLine);
}
/// <summary>
/// Gets or sets the disabled state for the action.
/// </summary>
[JsonProperty("disabled")]
[Description("Disable action")]
public BoolExpression Disabled { get; set; }
/// <summary>
/// Gets or sets the ConnectionString for querying the database.
/// </summary>
[JsonProperty("connectionString")]
[Description("Connection string for cosmosdb.")]
[Required]
public StringExpression ConnectionString { get; set; }
/// <summary>
/// database name
/// </summary>
[JsonProperty("database")]
[Description("Database name.")]
[Required]
public StringExpression Database { get; set; }
/// <summary>
/// Container name
/// </summary>
[JsonProperty("container")]
[Description("Name of the Container.")]
[Required]
public StringExpression Container { get; set; }
/// <summary>
/// Query.
/// </summary>
[JsonProperty("itemId")]
[Description("Item id of the item to get.")]
[Required]
public StringExpression ItemId { get; set; }
/// <summary>
/// PArtitionKey
/// </summary>
[JsonProperty("partitionKey")]
[Description("PartitionKey of the item to get.")]
public StringExpression PartitionKey { get; set; }
/// <summary>
/// Gets or sets the property path to store the query result in.
/// </summary>
/// <value>
/// The property path to store the dialog result in.
/// </value>
[JsonProperty("resultProperty")]
[Description("Property to put the result of this operation into.")]
public StringExpression ResultProperty { get; set; }
public override async Task<DialogTurnResult> BeginDialogAsync(DialogContext dc, object options = null, CancellationToken cancellationToken = default(CancellationToken))
{
if (this.Disabled != null && this.Disabled.GetValue(dc.State) == true)
{
return await dc.EndDialogAsync(cancellationToken: cancellationToken).ConfigureAwait(false);
}
var connectionString = ConnectionString.GetValue(dc.State);
var databaseName = Database.GetValue(dc.State);
var containerName = Container.GetValue(dc.State);
var itemId = ItemId.GetValue(dc.State);
var partitionKeyValue = PartitionKey.GetValue(dc.State);
PartitionKey partitionKey = new PartitionKey(partitionKeyValue);
var client = CosmosClientCache.GetClient(connectionString);
var database = client.GetDatabase(databaseName);
var container = database.GetContainer(containerName);
var result = await container.ReadItemAsync<object>(id: itemId, partitionKey: partitionKey, cancellationToken: cancellationToken).ConfigureAwait(false);
if (this.ResultProperty != null)
{
dc.State.SetValue(this.ResultProperty.GetValue(dc.State), result.Resource);
}
return await dc.EndDialogAsync(result: result.Resource, cancellationToken: cancellationToken).ConfigureAwait(false);
}
}
}