Пример #1
0
        public static Task <StoredResponse> ExecuteTransaction <TPayload, T>(this IMachineInterfaceConnector <T> connector, string requestId,
                                                                             Func <TPayload, T> getPartitionKey,
                                                                             Func <IMachineInterfaceConnectorMessageSession <TPayload>, Task <StoredResponse> > transaction)
        {
            return(connector.ExecuteTransaction(requestId, async stream =>
            {
                using (var streamReader = new StreamReader(stream))
                    using (var reader = new JsonTextReader(streamReader))
                    {
                        var request = serializer.Deserialize <TPayload>(reader);

                        var partitionKey = getPartitionKey(request);
                        return (request, partitionKey);
                    }
            }, transaction));
        }
Пример #2
0
        public async Task <IActionResult> AuthorizePost(string transactionId)
        {
            var result = await connector.ExecuteTransaction(transactionId,
                                                            async payload =>
            {
                var formReader     = new FormReader(payload);
                var values         = await formReader.ReadFormAsync().ConfigureAwait(false);
                var formCollection = new FormCollection(values);

                var amount     = decimal.Parse(formCollection["Amount"].Single());
                var customerId = formCollection["CustomerId"].Single();

                return(new AuthorizeRequest
                {
                    CustomerId = customerId,
                    TransactionId = transactionId,
                    Amount = amount
                }, customerId.Substring(0, 2));
            },
                                                            async session =>
            {
                Account account;
                try
                {
                    account = await session.TransactionContext.Batch()
                              .ReadItemAsync <Account>(session.Payload.CustomerId);
                }
                catch (CosmosException e)
                {
                    if (e.StatusCode == HttpStatusCode.NotFound)
                    {
                        account = new Account
                        {
                            Number       = session.Payload.CustomerId,
                            Partition    = session.Payload.CustomerId.Substring(0, 2),
                            Balance      = 0,
                            Transactions = new List <Transaction>()
                        };
                    }
                    else
                    {
                        throw new Exception("Error while loading account data", e);
                    }
                }

                account.Balance -= session.Payload.Amount;
                session.TransactionContext.Batch().UpsertItem(account);
                await session.Send(new SettleTransaction
                {
                    AccountNumber = account.Number,
                    Amount        = session.Payload.Amount,
                    TransactionId = transactionId
                });

                return(new StoredResponse(200, null));
            });

            if (result.Body != null)
            {
                Response.Body = result.Body;
            }
            return(StatusCode(result.Code));
        }