public KeyValuePair <int, IEnumerable <Model.Portfolio> > ListRoboAdvisors(string email, int goalOptionId, int risk) { var user = UserBusiness.GetValidUser(email); var purchases = Task.Factory.StartNew(() => BuyBusiness.ListPurchases(user.Id)); var goalOption = GoalOptionsBusiness.Get(goalOptionId); var riskType = RiskType.Get(risk, goalOption.Risk); var riskPriority = RiskType.GetRiskPriority(riskType); var advisors = Data.ListRobosAvailable(); var portfolios = Task.Factory.StartNew(() => PortfolioBusiness.List(advisors.Select(c => c.Id))); Task.WaitAll(portfolios); var portfolioQty = Task.Factory.StartNew(() => BuyBusiness.ListPortfoliosPurchases(portfolios.Result.SelectMany(c => c.Value.Select(x => x.Id)))); List <Task <List <PortfolioHistory> > > histories = new List <Task <List <PortfolioHistory> > >(); foreach (DomainObjects.Portfolio.Portfolio portfolio in portfolios.Result.SelectMany(c => c.Value)) { histories.Add(Task.Factory.StartNew(() => PortfolioHistoryBusiness.ListHistory(portfolio.Id))); } Task.WaitAll(purchases, portfolioQty); Task.WaitAll(histories.ToArray()); List <Model.Portfolio> portfolioWithSameRisk = new List <Model.Portfolio>(); List <Model.Portfolio> portfolioWithLittleLowerRisk = new List <Model.Portfolio>(); List <Model.Portfolio> portfolioWithLittleHigherRisk = new List <Model.Portfolio>(); List <Model.Portfolio> portfolioWithLowerRisk = new List <Model.Portfolio>(); List <Model.Portfolio> portfolioWithHigherRisk = new List <Model.Portfolio>(); List <Model.Portfolio> portfolioWithVeryLowerRisk = new List <Model.Portfolio>(); List <Model.Portfolio> portfolioWithVeryHigherRisk = new List <Model.Portfolio>(); foreach (KeyValuePair <int, List <DomainObjects.Portfolio.Portfolio> > advisorPortfolios in portfolios.Result) { var advisor = advisors.Single(c => c.Id == advisorPortfolios.Key); advisorPortfolios.Value.ForEach(c => c.PortfolioHistory = histories.SelectMany(x => x.Result.Where(g => g.PortfolioId == c.Id)).ToList()); foreach (var r in riskPriority) { var riskFound = advisorPortfolios.Value.SingleOrDefault(c => c.Projection.RiskType == r); if (riskFound != null) { var port = PortfolioBusiness.FillPortfolioModel(riskFound, advisor, user, purchases.Result, portfolioQty.Result); var difference = riskFound.Projection.RiskType.Value - riskType.Value; if (difference == 0) { portfolioWithSameRisk.Add(port); } else if (difference == 1) { portfolioWithLittleHigherRisk.Add(port); } else if (difference == -1) { portfolioWithLittleLowerRisk.Add(port); } else if (difference == 2) { portfolioWithHigherRisk.Add(port); } else if (difference == -2) { portfolioWithLowerRisk.Add(port); } else if (difference > 2) { portfolioWithVeryHigherRisk.Add(port); } else { portfolioWithVeryLowerRisk.Add(port); } break; } } } var result = portfolioWithSameRisk.OrderByDescending(c => c.PurchaseQuantity).ThenByDescending(c => c.ProjectionPercent).ToList(); result.AddRange(portfolioWithLittleLowerRisk.OrderByDescending(c => c.PurchaseQuantity).ThenByDescending(c => c.ProjectionPercent)); result.AddRange(portfolioWithLittleHigherRisk.OrderByDescending(c => c.PurchaseQuantity).ThenByDescending(c => c.ProjectionPercent)); result.AddRange(portfolioWithLowerRisk.OrderByDescending(c => c.PurchaseQuantity).ThenByDescending(c => c.ProjectionPercent)); result.AddRange(portfolioWithHigherRisk.OrderByDescending(c => c.PurchaseQuantity).ThenByDescending(c => c.ProjectionPercent)); result.AddRange(portfolioWithVeryLowerRisk.OrderByDescending(c => c.PurchaseQuantity).ThenByDescending(c => c.ProjectionPercent)); result.AddRange(portfolioWithVeryHigherRisk.OrderByDescending(c => c.PurchaseQuantity).ThenByDescending(c => c.ProjectionPercent)); return(new KeyValuePair <int, IEnumerable <Model.Portfolio> >(riskType.Value, result)); }
public List <Model.Portfolio> ListPortfolios(string email) { return(PortfolioBusiness.List(email)); }