public IActionResult GetNetWorth(PortFolioDetails portFolioDetails) { NetWorth _netWorth = new NetWorth(); _log4net.Info("Calculating the networth of user with id = " + portFolioDetails.PortFolioId + "In the method:" + nameof(GetNetWorth)); try { if (portFolioDetails == null) { return(NotFound("The portfolio doesn't contain any data")); } else if (portFolioDetails.PortFolioId == 0) { return(NotFound("The user with that id not found")); } else { _log4net.Info("The portfolio details are correct.Returning the networth of user with id" + portFolioDetails.PortFolioId); _netWorth = _netWorthProvider.calculateNetWorthAsync(portFolioDetails).Result; _log4net.Info("The networth is:" + JsonConvert.SerializeObject(_netWorth)); return(Ok(_netWorth)); } } catch (Exception ex) { _log4net.Info("An exception occured while calculating the networth:" + ex + " In the controller" + nameof(GetNetWorth)); return(new StatusCodeResult(500)); } }
//[Route("GetNetWorth")] public IActionResult GetNetWorth(PortFolioDetails portFolioDetails) { NetWorth _netWorth = new NetWorth(); _log4net.Info("Calculating the networth"); try { //if(HttpContext.Request.Body == null) //{ // return BadRequest("Please provide a valid PortFolio"); //} //else if (portFolioDetails.ToString() == "") //{ // return BadRequest("Please provide a valid PortFolio"); //} //else if(portFolioDetails.MutualFundList==null && portFolioDetails.StockList == null) //{ // _log4net.Info("Both The lists are empty"); // return BadRequest("The customer doesn't hold any assets"); //} _netWorth = _netWorthProvider.calculateNetWorthAsync(portFolioDetails).Result; return(Ok(_netWorth)); } catch (Exception ex) { return(BadRequest(ex.Message)); } }
public IActionResult GetPortFolioDetailsByID(int portFolioId) { PortFolioDetails portFolioDetails = new PortFolioDetails(); try { if (portFolioId <= 0) { return(NotFound("ID can't be 0 or less than 0")); } portFolioDetails = _netWorthProvider.GetPortFolioDetailsByID(portFolioId); if (portFolioDetails == null) { return(NotFound("Sorry, We don't have a portfolio with that ID")); } else { _log4net.Info("The deatils of the user are" + JsonConvert.SerializeObject(portFolioDetails)); return(Ok(portFolioDetails)); } } catch (Exception ex) { _log4net.Info("An exception occured while fetching the portfolio by id in the " + nameof(GetPortFolioDetailsByID) + ". The message is:" + ex.Message); return(new StatusCodeResult(500)); } }
/// <summary> /// This Will calculate the networth of the Client based on the number of stocks and mutual Funds he has. /// </summary> /// <param name="pd"></param> /// <returns></returns> /// public async Task <NetWorth> calculateNetWorthAsync(PortFolioDetails portFolioDetails) { Stock stock = new Stock(); MutualFund mutualfund = new MutualFund(); NetWorth networth = new NetWorth(); _log4net.Info("Calculating the networth in the repository method of user with id = " + portFolioDetails.PortFolioId); try { using (var httpClient = new HttpClient()) { var fetchStock = configuration["GetStockDetails"]; var fetchMutualFund = configuration["GetMutualFundDetails"]; if (portFolioDetails.StockList != null && portFolioDetails.StockList.Any() == true) { foreach (StockDetails stockDetails in portFolioDetails.StockList) { if (stockDetails.StockName != null) { using (var response = await httpClient.GetAsync(fetchStock + stockDetails.StockName)) { _log4net.Info("Fetching the details of stock " + stockDetails.StockName + "from the stock api"); string apiResponse = await response.Content.ReadAsStringAsync(); stock = JsonConvert.DeserializeObject <Stock>(apiResponse); _log4net.Info("Th stock details are " + JsonConvert.SerializeObject(stock)); } networth.Networth += stockDetails.StockCount * stock.StockValue; } } } if (portFolioDetails.MutualFundList != null && portFolioDetails.MutualFundList.Any() == true) { foreach (MutualFundDetails mutualFundDetails in portFolioDetails.MutualFundList) { if (mutualFundDetails.MutualFundName != null) { using (var response = await httpClient.GetAsync(fetchMutualFund + mutualFundDetails.MutualFundName)) { _log4net.Info("Fetching the details of mutual Fund " + mutualFundDetails.MutualFundName + "from the MutualFundNAV api"); string apiResponse = await response.Content.ReadAsStringAsync(); mutualfund = JsonConvert.DeserializeObject <MutualFund>(apiResponse); _log4net.Info("The mutual Fund Details are" + JsonConvert.SerializeObject(mutualfund)); } networth.Networth += mutualFundDetails.MutualFundUnits * mutualfund.MutualFundValue; } } } } networth.Networth = Math.Round(networth.Networth, 2); } catch (Exception ex) { _log4net.Error("Exception occured while calculating the networth of user" + portFolioDetails.PortFolioId + ":" + ex.Message); } return(networth); }
public void TestForCalculatingNetWorthWhenObjectisNullinRepository() { netWorthRepository.Setup(x => x.calculateNetWorthAsync(It.Ref <PortFolioDetails> .IsAny)).ReturnsAsync(() => null); PortFolioDetails portfolio = new PortFolioDetails(); var result = netWorthProvider.calculateNetWorthAsync(portfolio); Assert.IsNull(result); //Assert.Pass(); }
/// <summary> /// Takes as input the stock you want to sell and sends a sale response object back /// </summary> /// <param name="portfolioid"></param> /// <param name="stockdetails"></param> /// <returns></returns> public async Task <AssetSaleResponse> SellStocks(int portfolioid, StockDetails stockdetails) { try { var fetchPortFolio = _configuration["FetchingPortFolioById"]; var fetchNetWorth = _configuration["FetchingNetWorthByPortFolio"]; var fetchResponse = _configuration["FetchingSellDetails"]; PortFolioDetails current = new PortFolioDetails(); PortFolioDetails toSell = new PortFolioDetails(); _log4net.Info("Selling Stock " + stockdetails.StockName + " of user with id = " + portfolioid); using (var client = new HttpClient()) { using (var response = await client.GetAsync(fetchPortFolio + portfolioid)) { _log4net.Info("Fetching his portFolio"); string apiResponse = await response.Content.ReadAsStringAsync(); current = JsonConvert.DeserializeObject <PortFolioDetails>(apiResponse); _log4net.Info("The portfolio " + JsonConvert.SerializeObject(current)); } } toSell.PortFolioId = portfolioid; toSell.StockList = new List <StockDetails> { stockdetails }; toSell.MutualFundList = new List <MutualFundDetails>() { }; List <PortFolioDetails> list = new List <PortFolioDetails> { current, toSell }; AssetSaleResponse assetSaleResponse = new AssetSaleResponse(); StringContent content = new StringContent(JsonConvert.SerializeObject(list), Encoding.UTF8, "application/json"); using (var client = new HttpClient()) { using (var response = await client.PostAsync(fetchResponse, content)) { _log4net.Info("Fetching the response of sale of stock " + stockdetails.StockName + " of user with id " + portfolioid); string apiResponse = await response.Content.ReadAsStringAsync(); assetSaleResponse = JsonConvert.DeserializeObject <AssetSaleResponse>(apiResponse); _log4net.Info("The response is " + JsonConvert.SerializeObject(assetSaleResponse)); } } return(assetSaleResponse); } catch (Exception ex) { _log4net.Info("An exception occured:" + ex.Message); return(null); } }
public Task <NetWorth> calculateNetWorthAsync(PortFolioDetails portFolioDetails) { _log4net.Info("Provider called from Controller to calculate the networth"); var networth = _netWorthRepository.calculateNetWorthAsync(portFolioDetails); if (networth.Result.Networth == 0) { return(null); } return(networth); }
public void TestForCalculatingNetWorthWhenObjectDoesNotHaveValuesinProvider() { PortFolioDetails portFolioDetails = new PortFolioDetails(); networthProviderMock.Setup(x => x.calculateNetWorthAsync(It.Ref <PortFolioDetails> .IsAny)).ReturnsAsync(() => null); var data = networthController.GetNetWorth(portFolioDetails); ObjectResult okResult = data as ObjectResult; Assert.AreEqual(404, okResult.StatusCode); //Assert.Pass(); }
/// <summary> /// Calculates the networth based on the details provided in the portfolio object /// </summary> /// <param name="portFolio"></param> /// <returns></returns> public async Task <NetWorth> calculateNetWorth(PortFolioDetails portFolio) { try { NetWorth _networth = new NetWorth(); Stock stock = new Stock(); MutualFund mutualFund = new MutualFund(); double networth = 0; using (var httpClient = new HttpClient()) { var fetchStock = configuration["GetStockDetails"]; var fetchMutualFund = configuration["GetMutualFundDetails"]; if (portFolio.StockList != null || portFolio.MutualFundList.Any() != false) { foreach (StockDetails stockDetails in portFolio.StockList) { using (var response = await httpClient.GetAsync(fetchStock + stockDetails.StockName)) { _log4net.Info("Fetching the details of stock " + stockDetails.StockName + " from the stock api"); string apiResponse = await response.Content.ReadAsStringAsync(); stock = JsonConvert.DeserializeObject <Stock>(apiResponse); } networth += stockDetails.StockCount * stock.StockValue; } } if (portFolio.MutualFundList != null || portFolio.MutualFundList.Any() != false) { foreach (MutualFundDetails mutualFundDetails in portFolio.MutualFundList) { using (var response = await httpClient.GetAsync(fetchMutualFund + mutualFundDetails.MutualFundName)) { _log4net.Info("Fetching the details of stock " + mutualFundDetails.MutualFundName + " from the stock api"); string apiResponse = await response.Content.ReadAsStringAsync(); mutualFund = JsonConvert.DeserializeObject <MutualFund>(apiResponse); } networth += mutualFundDetails.MutualFundUnits * mutualFund.MutualFundValue; } } } networth = Math.Round(networth, 2); _networth.Networth = networth; return(_networth); } catch (Exception ex) { _log4net.Error("An exception occured while selling the assets:" + ex.Message); return(null); } }
/// <summary> /// Returns the PortFolio object with the wanted id. /// </summary> /// <param name="id"></param> /// <returns></returns> public PortFolioDetails GetPortFolioDetailsByID(int id) { PortFolioDetails portFolioDetails = new PortFolioDetails(); try { _log4net.Info("Returning portfolio details with the id" + id + " from the repository method"); portFolioDetails = _portFolioDetails.FirstOrDefault(e => e.PortFolioId == id); } catch (Exception ex) { _log4net.Error("An exception occured while fetching the portFolio details:" + ex.Message); } return(portFolioDetails); }
/// <summary> /// This method returns the portfolio of the user with the id provided in the parameter /// </summary> /// <param name="id"></param> /// <returns></returns> public PortFolioDetails GetPortFolioDetailsByID(int id) { PortFolioDetails portfolioDetails = new PortFolioDetails(); try { _log4net.Info("Returning the portfolio object from the provider method of user :"******"Exception ocured while getting the portfolio:" + ex.Message); } return(portfolioDetails); }
public async Task <IActionResult> BuyStock(StockDetails sd) { PortFolioDetails current = new PortFolioDetails(); PortFolioDetails toSell = new PortFolioDetails(); int id = Convert.ToInt32(HttpContext.Session.GetString("Id")); using (var client = new HttpClient()) { using (var response = await client.GetAsync("https://localhost:44375/api/NetWorth/" + id)) { string apiResponse = await response.Content.ReadAsStringAsync(); current = JsonConvert.DeserializeObject <PortFolioDetails>(apiResponse); //ord = o[0]; } } toSell.PortFolioId = id; toSell.StockList = new List <StockDetails> { sd }; toSell.MutualFundList = new List <MutualFundDetails>() { }; List <PortFolioDetails> list = new List <PortFolioDetails> { current, toSell }; AssetSaleResponse assetSaleResponse = new AssetSaleResponse(); StringContent content = new StringContent(JsonConvert.SerializeObject(list), Encoding.UTF8, "application/json"); using (var client = new HttpClient()) { using (var response = await client.PostAsync("https://localhost:44375/api/NetWorth/SellAssets/", content)) { string apiResponse = await response.Content.ReadAsStringAsync(); assetSaleResponse = JsonConvert.DeserializeObject <AssetSaleResponse>(apiResponse); //ord = o[0]; } } nw = assetSaleResponse.Networth; return(View("Reciept", assetSaleResponse)); }
/// <summary> /// Calculating the networth of user with given portfolio details /// </summary> /// <param name="portFolioDetails"></param> /// <returns></returns> public Task <NetWorth> calculateNetWorthAsync(PortFolioDetails portFolioDetails) { NetWorth networth = new NetWorth(); try { _log4net.Info("Provider called from Controller to calculate the networth"); if (portFolioDetails.PortFolioId == 0) { return(null); } networth = _netWorthRepository.calculateNetWorthAsync(portFolioDetails).Result; } catch (Exception ex) { _log4net.Error("Exception occured while calculating the networth" + ex.Message); } return(Task.FromResult(networth)); }
//private List<PortFolioDetails> _portFolioDetails; /// <summary> /// This Will calculate the networth of the Client using the number of stoks and mutual funds he has. /// </summary> /// <param name="pd"></param> /// <returns></returns> /// public async Task <NetWorth> calculateNetWorthAsync(PortFolioDetails portFolioDetails) { //PortFolioDetails simp = _portFolioDetails.FirstOrDefault(exec => exec.PortFolioId == id); Stock st = new Stock(); MutualFund mf = new MutualFund(); //double networth = 0; NetWorth networth = new NetWorth(); PortFolioDetails pd = portFolioDetails; _log4net.Info("Calculating the networth in the repository method"); using (var httpClient = new HttpClient()) { if (pd.StockList != null) { foreach (StockDetails x in pd.StockList) { using (var response = await httpClient.GetAsync("http://localhost:58451/api/Stock/" + x.StockName)) { string apiResponse = await response.Content.ReadAsStringAsync(); st = JsonConvert.DeserializeObject <Stock>(apiResponse); } networth.Networth += x.StockCount * st.StockValue; } } if (pd.MutualFundList != null) { foreach (MutualFundDetails x in pd.MutualFundList) { using (var response = await httpClient.GetAsync("https://localhost:55953/api/MutualFundNAV/" + x.MutualFundName)) { string apiResponse = await response.Content.ReadAsStringAsync(); mf = JsonConvert.DeserializeObject <MutualFund>(apiResponse); } networth.Networth += x.MutualFundUnits * mf.MValue; } } } networth.Networth = Math.Round(networth.Networth, 2); return(networth); }
public async Task <IActionResult> Index() { NetWorth _netWorth = new NetWorth(); if (CheckValid()) { CompleteDetails cd = new CompleteDetails(); PortFolioDetails portFolioDetails = new PortFolioDetails(); int id = Convert.ToInt32(HttpContext.Session.GetString("Id")); using (var client = new HttpClient()) { using (var response = await client.GetAsync("https://localhost:44375/api/NetWorth/" + id)) { string apiResponse = await response.Content.ReadAsStringAsync(); portFolioDetails = JsonConvert.DeserializeObject <PortFolioDetails>(apiResponse); //ord = o[0]; } } 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/", content)) { string apiResponse = await response.Content.ReadAsStringAsync(); _netWorth = JsonConvert.DeserializeObject <NetWorth>(apiResponse);; } } cd.PFId = portFolioDetails.PortFolioId; cd.FinalMutualFundList = portFolioDetails.MutualFundList; cd.FinalStockList = portFolioDetails.StockList; cd.NetWorth = _netWorth.networth; return(View(cd)); } return(RedirectToAction("Index", "Home")); }
public async Task <NetWorth> calculateNetWorth(PortFolioDetails pd) { NetWorth _networth = new NetWorth(); Stock st = new Stock(); MutualFund mf = new MutualFund(); double networth = 0; using (var httpClient = new HttpClient()) { if (pd.StockList != null) { foreach (StockDetails x in pd.StockList) { using (var response = await httpClient.GetAsync("http://localhost:58451/api/Stock/" + x.StockName)) { string apiResponse = await response.Content.ReadAsStringAsync(); st = JsonConvert.DeserializeObject <Stock>(apiResponse); } networth += x.StockCount * st.StockValue; } } if (pd.MutualFundList != null) { foreach (MutualFundDetails x in pd.MutualFundList) { using (var response = await httpClient.GetAsync("https://localhost:44394/api/MutualFund/" + x.MutualFundName)) { string apiResponse = await response.Content.ReadAsStringAsync(); mf = JsonConvert.DeserializeObject <MutualFund>(apiResponse); } networth += x.MutualFundUnits * mf.MValue; } } } networth = Math.Round(networth, 2); _networth.Networth = networth; return(_networth); }
public async Task <IActionResult> SellMutualFund(MutualFundDetails mutualFundDetails) { try{ if (CheckValid()) { PortFolioDetails current = new PortFolioDetails(); PortFolioDetails toSell = new PortFolioDetails(); int id = Convert.ToInt32(HttpContext.Session.GetString("Id")); _log4net.Info("Selling mutual fund" + mutualFundDetails.MutualFundName + " of user with id:" + id); using (var client = new HttpClient()) { using (var response = await client.GetAsync("https://localhost:44375/api/NetWorth/GetPortFolioDetailsByID/" + id)) { string apiResponse = await response.Content.ReadAsStringAsync(); current = JsonConvert.DeserializeObject <PortFolioDetails>(apiResponse); } } toSell.PortFolioId = id; toSell.MutualFundList = new List <MutualFundDetails> { mutualFundDetails }; toSell.StockList = new List <StockDetails>(); List <PortFolioDetails> list = new List <PortFolioDetails> { current, toSell }; AssetSaleResponse assetSaleResponse = new AssetSaleResponse(); StringContent content = new StringContent(JsonConvert.SerializeObject(list), Encoding.UTF8, "application/json"); using (var client = new HttpClient()) { using (var response = await client.PostAsync("https://localhost:44375/api/NetWorth/SellAssets", content)) { string apiResponse = await response.Content.ReadAsStringAsync(); assetSaleResponse = JsonConvert.DeserializeObject <AssetSaleResponse>(apiResponse); } } _log4net.Info("sale of mutual fund of user with id" + current.PortFolioId + "done"); Sale _sale = new Sale(); _sale.PortFolioID = current.PortFolioId; _sale.NetWorth = assetSaleResponse.Networth; _sale.status = assetSaleResponse.SaleStatus; _saleRepository.Add(_sale); return(View("Reciept", assetSaleResponse)); } else { return(RedirectToAction("Index", "Home")); } } catch (Exception ex) { _log4net.Error("An exception occured in the" + nameof(SellStock) + " while selling mutualFund. The message is:" + ex.Message); return(RedirectToAction("Index", "Home")); } }
public AssetSaleResponse sellAssets(List <PortFolioDetails> details) { NetWorth networth = new NetWorth(); NetWorth networth2 = new NetWorth(); PortFolioDetails current = details[0]; PortFolioDetails toSell = details[1]; _log4net.Info("Selling the assets"); foreach (PortFolioDetails x in details) { if (x == null) { return(null); } } AssetSaleResponse aq = new AssetSaleResponse(); networth = calculateNetWorth(current).Result; aq.SaleStatus = true; foreach (StockDetails x in current.StockList) { foreach (StockDetails y in toSell.StockList) { if (x.StockName == y.StockName) { if (x.StockCount < y.StockCount) { _log4net.Info("Not enough stocks to sell"); aq.SaleStatus = false; aq.Networth = networth.Networth; return(aq); } x.StockCount = x.StockCount - y.StockCount; /*if (x.StockCount == 0) * { * both[0].StockList.Remove(x); * }*/ } break; } } foreach (MutualFundDetails x in current.MutualFundList) { foreach (MutualFundDetails y in toSell.MutualFundList) { if (x.MutualFundName == y.MutualFundName) { if (x.MutualFundUnits < y.MutualFundUnits) { _log4net.Info("Not enough mutualFunds to sell"); aq.SaleStatus = false; aq.Networth = networth.Networth; return(aq); } x.MutualFundUnits = x.MutualFundUnits - y.MutualFundUnits; /* if (x.MutualFundUnits == 0) * { * both[0].MutualFundList.Remove(x); * }*/ } break; } } foreach (PortFolioDetails portfolio in _portFolioDetails) { if (portfolio.PortFolioId == toSell.PortFolioId) { foreach (StockDetails currentstock in portfolio.StockList) { foreach (StockDetails sellstock in toSell.StockList) { if (sellstock.StockName == currentstock.StockName) { currentstock.StockCount = currentstock.StockCount - sellstock.StockCount; } } } foreach (MutualFundDetails currentmutualfund in portfolio.MutualFundList) { foreach (MutualFundDetails sellmutualfund in toSell.MutualFundList) { if (sellmutualfund.MutualFundName == currentmutualfund.MutualFundName) { currentmutualfund.MutualFundUnits = currentmutualfund.MutualFundUnits - sellmutualfund.MutualFundUnits; } } } } } networth2 = calculateNetWorth(toSell).Result; aq.Networth = networth.Networth - networth2.Networth; net = aq.Networth; return(aq); }
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")); }
public PortFolioController() { _portFolioDetails = new PortFolioDetails(); }
/// <summary> /// The list contains two PortFolio objects. The first one has the current holdings, the other has the list of stocks or mutual funds /// the user wants to sell. The function returns whether the sale is successful and also returns the new networth. /// </summary> /// <param name="details"></param> /// <returns></returns> public AssetSaleResponse sellAssets(List <PortFolioDetails> details) { NetWorth networth = new NetWorth(); NetWorth networth2 = new NetWorth(); int flag1 = 1; int flag2 = 1; StockDetails stocktoremove = new StockDetails(); MutualFundDetails fundToRemove = new MutualFundDetails(); AssetSaleResponse assetSaleResponse = new AssetSaleResponse(); PortFolioDetails current = details[0]; PortFolioDetails toSell = details[1]; try { foreach (PortFolioDetails portFolioDetails in details) { if (portFolioDetails == null) { return(null); } } _log4net.Info("Selling the assets of user with id" + details[0].PortFolioId); networth = calculateNetWorth(current).Result; assetSaleResponse.SaleStatus = true; foreach (StockDetails toSellStock in toSell.StockList) { foreach (StockDetails currentStock in current.StockList) { if (currentStock.StockName == toSellStock.StockName) { if (currentStock.StockCount < toSellStock.StockCount) { _log4net.Info("Not enough stocks to sell for user :"******"Not enough mutualFunds to sell for user" + current.PortFolioId); assetSaleResponse.SaleStatus = false; assetSaleResponse.Networth = networth.Networth; return(assetSaleResponse); } break; } } } foreach (PortFolioDetails portfolio in _portFolioDetails) { if (portfolio.PortFolioId == toSell.PortFolioId) { foreach (StockDetails currentstock in portfolio.StockList) { foreach (StockDetails sellstock in toSell.StockList) { if (sellstock.StockName == currentstock.StockName) { currentstock.StockCount = currentstock.StockCount - sellstock.StockCount; if (currentstock.StockCount == 0) { _log4net.Info("The user with id = " + current.PortFolioId + "sold all of his " + currentstock.StockName + " stocks."); flag1 = 0; stocktoremove = currentstock; break; } } } } foreach (MutualFundDetails currentmutualfund in portfolio.MutualFundList) { foreach (MutualFundDetails sellmutualfund in toSell.MutualFundList) { if (sellmutualfund.MutualFundName == currentmutualfund.MutualFundName) { currentmutualfund.MutualFundUnits = currentmutualfund.MutualFundUnits - sellmutualfund.MutualFundUnits; if (currentmutualfund.MutualFundUnits == 0) { _log4net.Info("The user with id = " + current.PortFolioId + " has sold all of his mutual funds of" + currentmutualfund.MutualFundName); flag2 = 0; fundToRemove = currentmutualfund; break; } } } } } } if (flag1 == 0) { foreach (PortFolioDetails portfolio in _portFolioDetails) { if (portfolio.PortFolioId == current.PortFolioId) { portfolio.StockList.Remove(stocktoremove); } } } if (flag2 == 0) { foreach (PortFolioDetails portfolio in _portFolioDetails) { if (portfolio.PortFolioId == current.PortFolioId) { portfolio.MutualFundList.Remove(fundToRemove); } } } networth2 = calculateNetWorth(toSell).Result; assetSaleResponse.Networth = networth.Networth - networth2.Networth; _log4net.Info("The sale ststus of user with id = " + current.PortFolioId + " is equal to" + JsonConvert.SerializeObject(assetSaleResponse)); // } catch (Exception ex) { _log4net.Error("An exception occured while selling the assets:" + ex.Message); } return(assetSaleResponse); }
/// <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); } }