-
Notifications
You must be signed in to change notification settings - Fork 178
/
Copy pathWriteStorageItem.cs
80 lines (67 loc) · 2.83 KB
/
WriteStorageItem.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
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using AdaptiveExpressions.Properties;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Dialogs;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Bot.Builder.Community.Components.Storage
{
public class WriteStorageItem : Dialog
{
[JsonConstructor]
public WriteStorageItem([CallerFilePath] string sourceFilePath = "", [CallerLineNumber] int sourceLineNumber = 0)
: base()
{
// enable instances of this command as debug break point
this.RegisterSourceLocation(sourceFilePath, sourceLineNumber);
}
[JsonProperty("$kind")]
public const string Kind = "BotBuilderCommunity.WriteStorageItem";
[JsonProperty("disabled")]
public BoolExpression Disabled { get; set; }
[JsonProperty("item")]
public ValueExpression Item { get; set; }
[JsonProperty("itemKey")]
public StringExpression ItemKey { get; set; }
public async override Task<DialogTurnResult> BeginDialogAsync(DialogContext dc, object options = null, CancellationToken cancellationToken = default(CancellationToken))
{
if (options is CancellationToken)
{
throw new ArgumentException($"{nameof(options)} cannot be a cancellation token");
}
if (this.Disabled != null && this.Disabled.GetValue(dc.State) == true)
{
return await dc.EndDialogAsync(cancellationToken: cancellationToken).ConfigureAwait(false);
}
// Get current storage provider
var storage = dc.Context.TurnState.Get<IStorage>();
if (storage == null)
{
throw new Exception($"{this.Id}: storage provider could not be found in turn state.");
}
// Get item from memory
var item = this.Item.GetValue(dc.State);
if (!(item is JObject))
{
throw new Exception($"{this.Id}: \"item\" is null or not an object.");
}
// Get item key from memory
var itemKey = this.ItemKey.GetValue(dc.State);
if (String.IsNullOrEmpty(itemKey))
{
throw new Exception($"{this.Id}: \"itemKey\" is null or an empty string.");
}
// Create change list
var changes = new Dictionary<string, object>();
changes.Add(itemKey, (item as JObject).ToObject<IDictionary<string, object>>());
// Write changes
await storage.WriteAsync(changes, cancellationToken).ConfigureAwait(false);
return await dc.EndDialogAsync(null, cancellationToken).ConfigureAwait(false);
}
}
}