public async Task <IActionResult> Index()
        {
            try
            {
                NetWorth _netWorth = new NetWorth();
                if (CheckValid())
                {
                    int portfolioid = Convert.ToInt32(HttpContext.Session.GetString("Id"));
                    _log4net.Info("Showing the user with portfolio ID = " + portfolioid + "his networth and the assets he is currently holding");
                    CompleteDetails  completeDetails  = new CompleteDetails();
                    PortFolioDetails portFolioDetails = new PortFolioDetails();

                    using (var client = new HttpClient())
                    {
                        using (var response = await client.GetAsync("https://localhost:44375/api/NetWorth/GetPortFolioDetailsByID/" + portfolioid))
                        {
                            _log4net.Info("Calling the Calculate Networth Api for id" + portfolioid);
                            string apiResponse = await response.Content.ReadAsStringAsync();

                            portFolioDetails = JsonConvert.DeserializeObject <PortFolioDetails>(apiResponse);
                        }
                    }
                    StringContent content = new StringContent(JsonConvert.SerializeObject(portFolioDetails), Encoding.UTF8, "application/json");
                    using (var client = new HttpClient())
                    {
                        using (var response = await client.PostAsync("https://localhost:44375/api/NetWorth/GetNetWorth", content))
                        {
                            _log4net.Info("Calling the Networth api to return the networth of sent portfolio: " + content);
                            string apiResponse = await response.Content.ReadAsStringAsync();

                            _netWorth = JsonConvert.DeserializeObject <NetWorth>(apiResponse);
                        }
                    }
                    completeDetails.PFId = portFolioDetails.PortFolioId;
                    completeDetails.FinalMutualFundList = new List <CompleteMutualFundDetails>();
                    completeDetails.FinalStockList      = new List <CompleteStockDetails>();
                    Stock stock = new Stock();
                    MutualFundViewModel mutualFundViewModel = new MutualFundViewModel();
                    foreach (StockDetails stockDetails in portFolioDetails.StockList)
                    {
                        using (var client = new HttpClient())
                        {
                            using (var response = await client.GetAsync("http://localhost:58451/api/Stock/" + stockDetails.StockName))
                            {
                                _log4net.Info("Calling the StockPriceApi from:" + nameof(Index) + " for fetching the details of stock" + stockDetails.StockName);
                                string apiResponse = await response.Content.ReadAsStringAsync();

                                stock = JsonConvert.DeserializeObject <Stock>(apiResponse);
                            }

                            CompleteStockDetails completeStockDetails = new CompleteStockDetails();
                            completeStockDetails.StockName  = stockDetails.StockName;
                            completeStockDetails.StockCount = stockDetails.StockCount;
                            completeStockDetails.StockPrice = stock.StockValue;

                            completeDetails.FinalStockList.Add(completeStockDetails);
                        }
                    }

                    foreach (MutualFundDetails mutualFundDetails in portFolioDetails.MutualFundList)
                    {
                        using (var client = new HttpClient())
                        {
                            using (var response = await client.GetAsync("http://localhost:55953/api/MutualFundNAV/" + mutualFundDetails.MutualFundName))
                            {
                                _log4net.Info("Calling the MutualFundPriceApi from:" + nameof(Index) + " for fetching the details of the mutual fund:" + mutualFundDetails.MutualFundName);
                                string apiResponse = await response.Content.ReadAsStringAsync();

                                mutualFundViewModel = JsonConvert.DeserializeObject <MutualFundViewModel>(apiResponse);
                            }

                            CompleteMutualFundDetails completeMutualFundDetails = new CompleteMutualFundDetails();
                            completeMutualFundDetails.MutualFundName  = mutualFundDetails.MutualFundName;
                            completeMutualFundDetails.MutualFundUnits = mutualFundDetails.MutualFundUnits;
                            completeMutualFundDetails.MutualFundPrice = mutualFundViewModel.MutualFundValue;

                            completeDetails.FinalMutualFundList.Add(completeMutualFundDetails);
                        }
                    }
                    completeDetails.NetWorth = _netWorth.networth;
                    return(View(completeDetails));
                }
            }
            catch (Exception ex)
            {
                _log4net.Error("An exception occured while showing the portfolio details to the user. the message is :" + ex.Message);
            }
            return(RedirectToAction("Index", "Home"));
        }
Example #2
0
        /// <summary>
        /// Takes as input the ID and sends the Portfolio with that ID back
        /// </summary>
        /// <param name="portfolioid"></param>
        /// <returns></returns>
        public async Task <CompleteDetails> showPortFolio(int portfolioid)
        {
            try
            {
                CompleteDetails  completeDetails  = new CompleteDetails();
                PortFolioDetails portFolioDetails = new PortFolioDetails();
                NetWorth         _netWorth        = new NetWorth();

                var fetchPortFolio  = _configuration["FetchingPortFolioById"];
                var fetchNetWorth   = _configuration["FetchingNetWorthByPortFolio"];
                var fetchStock      = _configuration["FetchStock"];
                var fetchMutualFund = _configuration["FetchMutualFund"];

                using (var client = new HttpClient())
                {
                    using (var response = await client.GetAsync(fetchPortFolio + portfolioid))
                    {
                        _log4net.Info("Calling the Calculate Networth Api for id" + portfolioid);
                        string apiResponse = await response.Content.ReadAsStringAsync();

                        portFolioDetails = JsonConvert.DeserializeObject <PortFolioDetails>(apiResponse);
                    }
                }
                StringContent content = new StringContent(JsonConvert.SerializeObject(portFolioDetails), Encoding.UTF8, "application/json");
                using (var client = new HttpClient())
                {
                    using (var response = await client.PostAsync(fetchNetWorth, content))
                    {
                        _log4net.Info("Calling the Networth api to return the networth of sent portfolio: " + content);
                        string apiResponse = await response.Content.ReadAsStringAsync();

                        _netWorth = JsonConvert.DeserializeObject <NetWorth>(apiResponse);
                        _log4net.Info(" The networth is " + JsonConvert.SerializeObject(_netWorth));
                    }
                }
                completeDetails.PFId = portFolioDetails.PortFolioId;
                completeDetails.FinalMutualFundList = new List <CompleteMutualFundDetails>();
                completeDetails.FinalStockList      = new List <CompleteStockDetails>();
                Stock stock = new Stock();
                MutualFundViewModel mutualFundViewModel = new MutualFundViewModel();
                foreach (StockDetails stockDetails in portFolioDetails.StockList)
                {
                    using (var client = new HttpClient())
                    {
                        using (var response = await client.GetAsync(fetchStock + stockDetails.StockName))
                        {
                            _log4net.Info("Calling the StockPriceApi from:" + nameof(Index) + " for fetching the details of stock" + stockDetails.StockName);
                            string apiResponse = await response.Content.ReadAsStringAsync();

                            stock = JsonConvert.DeserializeObject <Stock>(apiResponse);
                            _log4net.Info("the stock details are " + JsonConvert.SerializeObject(stock));
                        }

                        CompleteStockDetails completeStockDetails = new CompleteStockDetails();
                        completeStockDetails.StockName  = stockDetails.StockName;
                        completeStockDetails.StockCount = stockDetails.StockCount;
                        completeStockDetails.StockPrice = stock.StockValue;

                        completeDetails.FinalStockList.Add(completeStockDetails);
                    }
                }

                foreach (MutualFundDetails mutualFundDetails in portFolioDetails.MutualFundList)
                {
                    using (var client = new HttpClient())
                    {
                        using (var response = await client.GetAsync(fetchMutualFund + mutualFundDetails.MutualFundName))
                        {
                            _log4net.Info("Calling the MutualFundPriceApi from:" + nameof(Index) + " for fetching the details of the mutual fund:" + mutualFundDetails.MutualFundName);
                            string apiResponse = await response.Content.ReadAsStringAsync();

                            mutualFundViewModel = JsonConvert.DeserializeObject <MutualFundViewModel>(apiResponse);
                            _log4net.Info("The mutual fund details are " + JsonConvert.SerializeObject(mutualFundViewModel));
                        }

                        CompleteMutualFundDetails completeMutualFundDetails = new CompleteMutualFundDetails();
                        completeMutualFundDetails.MutualFundName  = mutualFundDetails.MutualFundName;
                        completeMutualFundDetails.MutualFundUnits = mutualFundDetails.MutualFundUnits;
                        completeMutualFundDetails.MutualFundPrice = mutualFundViewModel.MutualFundValue;

                        completeDetails.FinalMutualFundList.Add(completeMutualFundDetails);
                    }
                }
                completeDetails.NetWorth = _netWorth.networth;
                return(completeDetails);
            }
            catch (Exception ex)
            {
                _log4net.Info("An exception occured " + ex.Message);
                return(null);
            }
        }