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.");
            }
        }
Exemplo n.º 2
0
        public static async Task <DepositRequest> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = "portfolio/{investorId}")] HttpRequest req,
            [Table("Portfolio", InvestorType.Individual, "{investorId}")] Investor investor,
            string investorId,
            ILogger log)
        {
            log.LogInformation($"C# HTTP trigger function processed a request.");

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();

            log.LogInformation($"Request body: {requestBody}");

            var deposit = JsonConvert.DeserializeObject <Deposit>(requestBody);

            if (investor == null)
            {
                throw new ArgumentException($"Invalid investorId '{investorId}.");
            }
            if (deposit is null)
            {
                throw new ArgumentException($"Invalid deposit.");
            }
            if (deposit.Amount <= 0)
            {
                throw new ArgumentException($"Deposit amount must be greater than 1.");
            }

            // Additional validation omitted for demo purposes

            var depositRequest = new DepositRequest
            {
                Amount   = deposit.Amount,
                Investor = investor
            };

            log.LogInformation($"Deposit created: {depositRequest}");

            return(depositRequest);
        }