1
1
using Azure . Data . Tables ;
2
2
using Nameless . Orleans . Client . Contracts ;
3
+ using Nameless . Orleans . Core ;
3
4
using Nameless . Orleans . Grains . Abstractions ;
4
5
using Orleans . Configuration ;
5
6
@@ -11,12 +12,12 @@ public static void Main(string[] args) {
11
12
12
13
builder . Host . UseOrleansClient ( ( _ , client ) => {
13
14
client . UseAzureStorageClustering ( options => {
14
- options . TableServiceClient = new TableServiceClient ( "UseDevelopmentStorage=true;" ) ;
15
+ options . TableServiceClient = new TableServiceClient ( Constants . ConnectionString ) ;
15
16
} ) ;
16
17
17
18
client . Configure < ClusterOptions > ( options => {
18
- options . ClusterId = "Nameless_Orleans_Cluster" ;
19
- options . ServiceId = "Nameless_Orleans_Service" ;
19
+ options . ClusterId = Constants . ClusterId ;
20
+ options . ServiceId = Constants . ServiceId ;
20
21
} ) ;
21
22
22
23
client . UseTransactions ( ) ;
@@ -31,7 +32,10 @@ public static void Main(string[] args) {
31
32
32
33
await atmGrain . InitializeAsync ( createAtm . OpeningBalance ) ;
33
34
34
- return TypedResults . Created ( $ "/atm/{ atmId } ", new { AtmId = atmId } ) ;
35
+ return TypedResults . Created ( $ "/atm/{ atmId } ", new {
36
+ AtmId = atmId ,
37
+ createAtm . OpeningBalance
38
+ } ) ;
35
39
} ) ;
36
40
37
41
// Retrieves the ATM current balance
@@ -49,11 +53,18 @@ await transactionClient.RunTransaction(TransactionOption.Create, async () => {
49
53
} ) ;
50
54
51
55
// Withdraw from ATM
52
- app . MapPost ( "/atm/{atmId}/withdraw" , async ( Guid atmId , AtmWithdraw atmWithdraw , IClusterClient clusterClient ) => {
56
+ app . MapPost ( "/atm/{atmId}/withdraw" , async ( Guid atmId ,
57
+ AtmWithdraw atmWithdraw ,
58
+ IClusterClient clusterClient ,
59
+ ITransactionClient transactionClient ) => {
53
60
var atmGrain = clusterClient . GetGrain < IAtmGrain > ( atmId ) ;
54
61
var accountGrain = clusterClient . GetGrain < IAccountGrain > ( atmWithdraw . AccountId ) ;
55
62
56
- await atmGrain . WithdrawAsync ( atmWithdraw . AccountId , atmWithdraw . Amount ) ;
63
+ await transactionClient . RunTransaction ( TransactionOption . Create , async ( ) => {
64
+ await atmGrain . WithdrawAsync ( atmWithdraw . AccountId , atmWithdraw . Amount ) ;
65
+ await accountGrain . DebitAsync ( atmWithdraw . Amount ) ;
66
+ } ) ;
67
+
57
68
var currentAtmBalance = await atmGrain . GetCurrentBalanceAsync ( ) ;
58
69
var currentAccountBalance = await accountGrain . GetCurrentBalanceAsync ( ) ;
59
70
@@ -140,6 +151,30 @@ await transactionClient.RunTransaction(TransactionOption.Create, async () => {
140
151
} ) ;
141
152
} ) ;
142
153
154
+ // Get customer net worth
155
+ app . MapGet ( "/customer/{customerId}/net_worth" , async ( Guid customerId , IClusterClient clusterClient ) => {
156
+ var customerGrain = clusterClient . GetGrain < ICustomerGrain > ( customerId ) ;
157
+
158
+ var netWorth = await customerGrain . GetNetWorthAsync ( customerId ) ;
159
+
160
+ return TypedResults . Ok ( new {
161
+ CustomerId = customerId ,
162
+ NetWorth = netWorth
163
+ } ) ;
164
+ } ) ;
165
+
166
+ // Get customer net worth
167
+ app . MapPost ( "/customer/{customerId:guid}/add_account" , async ( Guid customerId , CustomerAccount customerAccount , IClusterClient clusterClient ) => {
168
+ var customerGrain = clusterClient . GetGrain < ICustomerGrain > ( customerId ) ;
169
+
170
+ await customerGrain . AddAccountAsync ( customerAccount . AccountId ) ;
171
+
172
+ return TypedResults . Ok ( new {
173
+ CustomerId = customerId ,
174
+ customerAccount . AccountId
175
+ } ) ;
176
+ } ) ;
177
+
143
178
app . Run ( ) ;
144
179
}
145
180
}
0 commit comments