forked from microsoft/coyote-samples
/
CartActor.cs
74 lines (62 loc) · 2.24 KB
/
CartActor.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Coyote;
using Microsoft.Coyote.Actors;
using static System.Console;
namespace Actors
{
[OnEventDoAction(typeof(AddItemEvent), nameof(OnAddItemEvent))]
[OnEventDoAction(typeof(RemoveItemEvent), nameof(OnRemoveItemEvent))]
[OnEventDoAction(typeof(GetTotalRequestEvent), nameof(OnGetTotal))]
public class CartActor : Actor
{
private Dictionary<string, decimal> items = new Dictionary<string, decimal>();
protected void OnAddItemEvent(Event e)
{
// Note: Not a fan of this parameter "Event e" and having to cast "e"
// the appropriate event type.
// I think "OnAddItemEvent(AddItemEvent e)" would be better?
if ( !(e is AddItemEvent item) ) return;
if( this.items.ContainsKey(item.Sku) )
{
this.Assert(true, "The item cannot be added again.");
}
this.items.Add(item.Sku, item.Price);
}
protected void OnRemoveItemEvent(Event e)
{
if( !(e is RemoveItemEvent item) ) return;
if( this.items.TryGetValue(item.Sku, out var price) )
{
this.items.Remove(item.Sku);
}
else
{
this.Assert(true, "Cannot remove an item from the cart that never existed.");
}
}
//protected void OnGetTotal(GetTotalRequestEvent e) -- doesn't work. :(
protected void OnGetTotal(Event e)
{
if( !(e is GetTotalRequestEvent) ) return;
var total = this.items.Values.Sum();
//var response = new GetTotalResponseEvent()
// {
// Total = total
// };
//this.SendEvent(caller, response);
// Note: Feels like a hack to use the CurrentEventGroup as a mechanism for
// returning a simple value from Coyote.
if( this.CurrentEventGroup is AwaitableEventGroup<decimal> awaiter )
{
awaiter.TrySetResult(total);
}
else
{
this.Assert(true, $"CurrentEventGroup should be set to '{nameof(AwaitableEventGroup<decimal>)}'");
}
}
}
}