Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        public InvestementAllocation Calculate(int totalAmountToBeInvested, Investor investor)
        {
            if (totalAmountToBeInvested <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(totalAmountToBeInvested), "The amount must be greater than zero.");
            }

            var totalCurrentPortfolioValue = investor.CurrentValueOfBonds + investor.CurrentValueOfStocks;

            decimal currentPercentageBonds = CalculateCurrentBondsPercentage(investor, totalCurrentPortfolioValue);

            bool areBondsOverweight = currentPercentageBonds > investor.TargetPercentageAllocationToBonds;

            if (areBondsOverweight)
            {
                return(AllocateAllToStocks(totalAmountToBeInvested));
            }

            return(AllocateAllToBonds(totalAmountToBeInvested));
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
0
 private static async Task UpdateInvestor(CloudTable cloudTable, Investor investor)
 {
     var operation = TableOperation.Replace(investor);
     await cloudTable.ExecuteAsync(operation);
 }
Ejemplo n.º 5
0
 private static void AddBonds(AssetPurchase bondPurchase, Investor investor)
 {
     investor.CurrentValueOfBonds += bondPurchase.Amount;
 }
Ejemplo n.º 6
0
 private static decimal CalculateCurrentBondsPercentage(Investor investor, decimal totalCurrentPortfolioValue)
 {
     // Normally all financial amounts would be type decimal, ints are used in this sample to simplify table storage
     return(((decimal)investor.CurrentValueOfBonds / totalCurrentPortfolioValue) * 100);
 }
Ejemplo n.º 7
0
 private static void AddStocks(AssetPurchase stocksPurchase, Investor investor)
 {
     investor.CurrentValueOfStocks += stocksPurchase.Amount;
 }