public CompletePortfolio() { PortfolioProfit = new PortfolioProfit(); CurrentStockPortfolio = new StockPortfolio(); CurrentCryptoPortfolio = new CryptoPortfolio(); }
public async Task <Response <CryptoPortfolio> > GetCryptoPortfolioAsync() { var fnResult = new Response <CryptoPortfolio> { StatusCode = (int)HttpStatusCode.BadRequest }; try { var cryptocurrenies = await _unitOfWork.Cryptocurrencies.GetAll(); var cryptoPortfolio = new CryptoPortfolio(); var queryString = ""; foreach (var crypto in cryptocurrenies) { var coinInfo = _mapper.Map <CoinInfo>(crypto); if (!cryptoPortfolio.Cryptocurrencies.ContainsKey(coinInfo.Name)) { cryptoPortfolio.Cryptocurrencies.Add(coinInfo.Name, new CryptoProfile { CoinList = new List <CoinInfo> { coinInfo }, CoinCount = 1 }); queryString += coinInfo.Name + ","; } else { var profile = cryptoPortfolio.Cryptocurrencies[coinInfo.Name]; profile.CoinList.Add(coinInfo); profile.CoinCount += 1; cryptoPortfolio.Cryptocurrencies[coinInfo.Name] = profile; } } var ids = queryString.Remove(queryString.Length - 1); var prices = await GetCryptoValuesAsync(ids); if (prices.StatusCode != (int)HttpStatusCode.OK) { throw new Exception("There was an exception thrown getting crypto values while creating crypto portfolio."); } foreach (var value in prices.Data) { var coinProfile = cryptoPortfolio.Cryptocurrencies[value.Name]; coinProfile.CoinName = value.Name; coinProfile.FullName = value.FullName; coinProfile.CurrentPrice = value.Price; double avgPrice = 0; foreach (var entry in coinProfile.CoinList) { entry.CurrentPrice = value.Price; entry.CurrentValue = entry.Amount * entry.CurrentPrice; coinProfile.CurrentValue += entry.CurrentValue; entry.Profit = entry.CurrentValue - entry.TotalCost; coinProfile.TotalProfit += entry.Profit; coinProfile.TotalCost += entry.TotalCost; coinProfile.TotalAmount += entry.Amount; avgPrice += entry.TotalCost; } coinProfile.TotalCost = Math.Round(coinProfile.TotalCost, 2); coinProfile.TotalProfit = Math.Round(coinProfile.TotalProfit, 2); coinProfile.CurrentValue = Math.Round(coinProfile.CurrentValue, 2); coinProfile.CurrentPrice = Math.Round(coinProfile.CurrentPrice, 5); coinProfile.TotalAmount = Math.Round(coinProfile.TotalAmount, 3); coinProfile.AvgPrice = Math.Round(avgPrice / coinProfile.TotalAmount, 3); } fnResult.StatusCode = (int)HttpStatusCode.OK; fnResult.Data = cryptoPortfolio; return(fnResult); } catch (Exception e) { fnResult.StatusCode = (int)HttpStatusCode.InternalServerError; fnResult.Message = e.Message; return(fnResult); } }