Esempio n. 1
0
        public InvestmentPerformance GetInvestmentDetail(int investmentId)
        {
            const string sql = @"SELECT sp.[Id],
                                       s.[Name],
                                       sp.Shares,
	                                   sp.PurchaseCostPerShare,
	                                   sp.Shares * s.Price As CurrentValue,
	                                   s.Price As CurrentPrice,
	                                   sp.PurchaseDate,
                                       (sp.Shares * s.Price) - (sp.Shares * sp.PurchaseCostPerShare) As NetGain
                                FROM [Investment].[dbo].[StockPurchase] sp
                                INNER JOIN [Investment].[dbo].[Stock] s on sp.StockId = s.Id
                                WHERE sp.[Id] = @InvestmentId";

            StockPurchase stockPurchase;

            using (var connection = new SqlConnection(_configuration["ConnectionStrings:DefaultConnection"]))
            {
                stockPurchase = connection.QuerySingleOrDefault <StockPurchase>(sql, new { InvestmentId = investmentId });
            }

            InvestmentPerformance investmentPerformance = null;

            if (stockPurchase != null)
            {
                investmentPerformance = new InvestmentPerformance
                {
                    Id                = stockPurchase.Id,
                    Name              = stockPurchase.Name,
                    Shares            = stockPurchase.Shares,
                    CostBasisPerShare = stockPurchase.PurchaseCostPerShare,
                    CurrentValue      = stockPurchase.CurrentValue,
                    CurrentPrice      = stockPurchase.CurrentPrice,
                    Term              = PerformanceCalculationHelper.GetTerm(stockPurchase.PurchaseDate),
                    NetGain           = PerformanceCalculationHelper.CalculateNetGain(stockPurchase.Shares, stockPurchase.CurrentPrice, stockPurchase.PurchaseCostPerShare),
                };
            }

            return(investmentPerformance);
        }
 [TestCase("6/1/2018", "6/1/2019")]   // Cross a year boundry
 public void More_Than_365_Days_Will_Return_Long(DateTime purchaseDate, DateTime endDate)
 {
     Assert.IsTrue(PerformanceCalculationHelper.GetTerm(purchaseDate, endDate) == Models.Term.Long);
 }
 [TestCase("6/1/2018", "5/31/2019")]  // Cross a year boundry
 public void Less_Than_366_Days_Will_Return_Short(DateTime purchaseDate, DateTime endDate)
 {
     Assert.IsTrue(PerformanceCalculationHelper.GetTerm(purchaseDate, endDate) == Models.Term.Short);
 }