@@ -18,57 +18,126 @@ public static void Main(string[] args) {
18
18
options . ClusterId = "Nameless_Orleans_Cluster" ;
19
19
options . ServiceId = "Nameless_Orleans_Service" ;
20
20
} ) ;
21
+
22
+ client . UseTransactions ( ) ;
21
23
} ) ;
22
24
23
25
var app = builder . Build ( ) ;
24
26
25
- app . MapGet ( "/account/{accountId}/balance" , async ( Guid accountId , IClusterClient clusterClient ) => {
26
- var checkingAccountGrain = clusterClient . GetGrain < ICheckingAccountGrain > ( accountId ) ;
27
- var balance = await checkingAccountGrain . GetBalanceAsync ( ) ;
27
+ // Creates a new ATM
28
+ app . MapPost ( "/atm" , async ( CreateAtm createAtm , IClusterClient clusterClient ) => {
29
+ var atmId = Guid . NewGuid ( ) ;
30
+ var atmGrain = clusterClient . GetGrain < IAtmGrain > ( atmId ) ;
31
+
32
+ await atmGrain . InitializeAsync ( createAtm . OpeningBalance ) ;
28
33
29
- return TypedResults . Ok ( balance ) ;
34
+ return TypedResults . Created ( $ "/atm/ { atmId } " , new { AtmId = atmId } ) ;
30
35
} ) ;
31
36
32
- app . MapPost ( "/account" , async ( CreateAccount createAccount , IClusterClient clusterClient ) => {
37
+ // Retrieves the ATM current balance
38
+ app . MapGet ( "/atm/{atmId}/balance" , async ( Guid atmId , ITransactionClient transactionClient , IClusterClient clusterClient ) => {
39
+ var currentBalance = 0m ;
40
+ await transactionClient . RunTransaction ( TransactionOption . Create , async ( ) => {
41
+ var atmGrain = clusterClient . GetGrain < IAtmGrain > ( atmId ) ;
42
+ currentBalance = await atmGrain . GetCurrentBalanceAsync ( ) ;
43
+ } ) ;
44
+
45
+ return TypedResults . Ok ( new {
46
+ AtmId = atmId ,
47
+ CurrentBalance = currentBalance
48
+ } ) ;
49
+ } ) ;
50
+
51
+ // Withdraw from ATM
52
+ app . MapPost ( "/atm/{atmId}/withdraw" , async ( Guid atmId , AtmWithdraw atmWithdraw , IClusterClient clusterClient ) => {
53
+ var atmGrain = clusterClient . GetGrain < IAtmGrain > ( atmId ) ;
54
+ var accountGrain = clusterClient . GetGrain < IAccountGrain > ( atmWithdraw . AccountId ) ;
55
+
56
+ await atmGrain . WithdrawAsync ( atmWithdraw . AccountId , atmWithdraw . Amount ) ;
57
+ var currentAtmBalance = await atmGrain . GetCurrentBalanceAsync ( ) ;
58
+ var currentAccountBalance = await accountGrain . GetCurrentBalanceAsync ( ) ;
59
+
60
+ return TypedResults . Ok ( new {
61
+ AtmId = atmId ,
62
+ AtmCurrentBalance = currentAtmBalance ,
63
+ WithdrawAmout = atmWithdraw . Amount ,
64
+ atmWithdraw . AccountId ,
65
+ AccountCurrentBalance = currentAccountBalance
66
+ } ) ;
67
+ } ) ;
68
+
69
+ // Creates a new Account
70
+ app . MapPost ( "/account" , async ( CreateAccount createAccount , ITransactionClient transactionClient , IClusterClient clusterClient ) => {
33
71
var accountId = Guid . NewGuid ( ) ;
34
- var checkingAccountGrain = clusterClient . GetGrain < ICheckingAccountGrain > ( accountId ) ;
35
72
36
- await checkingAccountGrain . InitializeAsync ( createAccount . OpeningBalance ) ;
73
+ await transactionClient . RunTransaction ( TransactionOption . Create , async ( ) => {
74
+ var accountGrain = clusterClient . GetGrain < IAccountGrain > ( accountId ) ;
75
+
76
+ await accountGrain . InitializeAsync ( createAccount . OpeningBalance ) ;
77
+ } ) ;
37
78
38
- return TypedResults . Created ( $ "/account/{ accountId } ", new { AccountId = accountId } ) ;
79
+ return TypedResults . Created ( $ "/account/{ accountId } ", new {
80
+ AccountId = accountId ,
81
+ CurrentBalance = createAccount . OpeningBalance
82
+ } ) ;
39
83
} ) ;
40
84
41
- app . MapPost ( "/account/{accountId}/debit" , async ( Guid accountId , Debit input , IClusterClient clusterClient ) => {
42
- var checkingAccountGrain = clusterClient . GetGrain < ICheckingAccountGrain > ( accountId ) ;
85
+ // Retrieves Account current balance
86
+ app . MapGet ( "/account/{accountId}/balance" , async ( Guid accountId , ITransactionClient transactionClient , IClusterClient clusterClient ) => {
87
+ var currentBalance = 0m ;
43
88
44
- await checkingAccountGrain . DebitAsync ( input . Amount ) ;
89
+ await transactionClient . RunTransaction ( TransactionOption . Create , async ( ) => {
90
+ var accountGrain = clusterClient . GetGrain < IAccountGrain > ( accountId ) ;
91
+ currentBalance = await accountGrain . GetCurrentBalanceAsync ( ) ;
92
+ } ) ;
45
93
46
- return TypedResults . Ok ( input ) ;
94
+ return TypedResults . Ok ( new {
95
+ AccountId = accountId ,
96
+ CurrentBalance = currentBalance
97
+ } ) ;
47
98
} ) ;
48
99
49
- app . MapPost ( "/account/{accountId}/credit" , async ( Guid accountId , Credit input , IClusterClient clusterClient ) => {
50
- var checkingAccountGrain = clusterClient . GetGrain < ICheckingAccountGrain > ( accountId ) ;
100
+ // Creates a debit into an Account
101
+ app . MapPost ( "/account/{accountId}/debit" , async ( Guid accountId , Debit input , IClusterClient clusterClient ) => {
102
+ var accountGrain = clusterClient . GetGrain < IAccountGrain > ( accountId ) ;
103
+
104
+ await accountGrain . DebitAsync ( input . Amount ) ;
51
105
52
- await checkingAccountGrain . CreditAsync ( input . Amount ) ;
106
+ var currentBalance = await accountGrain . GetCurrentBalanceAsync ( ) ;
53
107
54
- return TypedResults . Ok ( input ) ;
108
+ return TypedResults . Ok ( new {
109
+ AccountId = accountId ,
110
+ Debit = input . Amount ,
111
+ CurrentBalance = currentBalance
112
+ } ) ;
55
113
} ) ;
56
114
57
- app . MapPost ( "/atm" , async ( CreateAtm createAtm , IClusterClient clusterClient ) => {
58
- var atmId = Guid . NewGuid ( ) ;
59
- var atmGrain = clusterClient . GetGrain < IAtmGrain > ( atmId ) ;
115
+ // Creates a credit into an Account
116
+ app . MapPost ( "/account/{accountId}/credit" , async ( Guid accountId , Credit input , IClusterClient clusterClient ) => {
117
+ var accountGrain = clusterClient . GetGrain < IAccountGrain > ( accountId ) ;
60
118
61
- await atmGrain . InitializeAsync ( createAtm . OpeningBalance ) ;
119
+ await accountGrain . CreditAsync ( input . Amount ) ;
62
120
63
- return TypedResults . Created ( $ "/atm/{ atmId } ", new { AtmId = atmId } ) ;
121
+ var currentBalance = await accountGrain . GetCurrentBalanceAsync ( ) ;
122
+
123
+ return TypedResults . Ok ( new {
124
+ AccountId = accountId ,
125
+ Credit = input . Amount ,
126
+ CurrentBalance = currentBalance
127
+ } ) ;
64
128
} ) ;
65
129
66
- app . MapPost ( "/atm/{atmId}/withdraw" , async ( Guid atmId , AtmWithdraw atmWithdraw , IClusterClient clusterClient ) => {
67
- var atmGrain = clusterClient . GetGrain < IAtmGrain > ( atmId ) ;
130
+ // Creates a recurring payment on an Account
131
+ app . MapPost ( "/account/{accountId}/recurring_payment" , async ( Guid accountId , RecurringPayment input , IClusterClient clusterClient ) => {
132
+ var accountGrain = clusterClient . GetGrain < IAccountGrain > ( accountId ) ;
68
133
69
- await atmGrain . WithdrawAsync ( atmWithdraw . AccountId , atmWithdraw . Amount ) ;
134
+ await accountGrain . AddRecurringPaymentAsync ( input . Amount , input . PeriodInMinutes ) ;
70
135
71
- return TypedResults . Ok ( atmWithdraw ) ;
136
+ return TypedResults . Ok ( new {
137
+ AccountId = accountId ,
138
+ RecurringAmout = input . Amount ,
139
+ RecurringPeriodInMinutes = input . PeriodInMinutes
140
+ } ) ;
72
141
} ) ;
73
142
74
143
app . Run ( ) ;
0 commit comments