-
Notifications
You must be signed in to change notification settings - Fork 177
/
Copy pathDeleteStorageItem.cs
60 lines (51 loc) · 2.15 KB
/
DeleteStorageItem.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
using System;
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;
namespace Bot.Builder.Community.Components.Storage
{
public class DeleteStorageItem : Dialog
{
[JsonConstructor]
public DeleteStorageItem([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.DeleteStorageItem";
[JsonProperty("disabled")]
public BoolExpression Disabled { 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.");
}
// Delete item
var itemKey = this.ItemKey.GetValue(dc.State);
if (!String.IsNullOrEmpty(itemKey))
{
await storage.DeleteAsync(new string[] { itemKey }, cancellationToken).ConfigureAwait(false);
}
return await dc.EndDialogAsync(null, cancellationToken).ConfigureAwait(false);
}
}
}