public static void Run( [QueueTrigger("deposit-requests")] DepositRequest depositRequest, [Queue("buy-stocks")] out AssetPurchase stockPurchase, [Queue("buy-bonds")] out AssetPurchase bondPurchase, [Inject] IInvestementAllocator investementAllocator, ILogger log) { stockPurchase = null; bondPurchase = null; log.LogInformation($"C# Queue trigger function processed: {depositRequest}"); InvestementAllocation allocation = investementAllocator.Calculate(depositRequest.Amount, depositRequest.Investor); if (allocation.AmountToBonds > 0) { log.LogInformation($"Allocating {allocation.AmountToBonds} to bonds"); bondPurchase = new AssetPurchase { InvestorId = depositRequest.Investor.RowKey, Amount = allocation.AmountToBonds }; } if (allocation.AmountToStocks > 0) { log.LogInformation($"Allocating {allocation.AmountToStocks} to stocks"); stockPurchase = new AssetPurchase { InvestorId = depositRequest.Investor.RowKey, Amount = allocation.AmountToStocks }; } if (bondPurchase is null && stockPurchase is null) { throw new System.Exception($"The deposit request for {depositRequest.Amount} did not result in an allocation to stocks or bonds. Check the input amount and the correctness of the IInvestementAllocator being used."); } }
public static async Task Run( [QueueTrigger("buy-bonds")] AssetPurchase bondPurchase, [Table("Portfolio", InvestorType.Individual)] CloudTable cloudTable, ILogger log) { log.LogInformation($"C# Queue trigger function processed for investorId: {bondPurchase.InvestorId}"); Investor investor = await LoadInvestor(bondPurchase, cloudTable, log); AddBonds(bondPurchase, investor); await UpdateInvestor(cloudTable, investor); }
private static async Task <Investor> LoadInvestor(AssetPurchase bondPurchase, CloudTable cloudTable, ILogger log) { TableQuery <Investor> investorQuery = new TableQuery <Investor>().Where( TableQuery.CombineFilters( TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, InvestorType.Individual), TableOperators.And, TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.Equal, bondPurchase.InvestorId))); var queryExecution = await cloudTable.ExecuteQuerySegmentedAsync(investorQuery, null); var investor = queryExecution.Results.FirstOrDefault(); if (investor is null) { var message = $"The investor id '{bondPurchase.InvestorId}' was not found in table storage."; log.LogError(message); throw new ArgumentException(message, nameof(bondPurchase)); } return(investor); }
private static void AddBonds(AssetPurchase bondPurchase, Investor investor) { investor.CurrentValueOfBonds += bondPurchase.Amount; }
private static void AddStocks(AssetPurchase stocksPurchase, Investor investor) { investor.CurrentValueOfStocks += stocksPurchase.Amount; }