public IActionResult SellAssets(List <PortFolioDetails> listOfAssetsCurrentlyHoldingAndAssetsToSell) { try { _log4net.Info("Selling some assets"); AssetSaleResponse assetSaleResponse = new AssetSaleResponse(); if (listOfAssetsCurrentlyHoldingAndAssetsToSell == null) { return(BadRequest("Please Provide a Valid List of portFolios")); } else { assetSaleResponse = _netWorthProvider.sellAssets(listOfAssetsCurrentlyHoldingAndAssetsToSell); if (assetSaleResponse == null) { _log4net.Info("Couldn't be sold because of invalid portfolio"); return(BadRequest("Please provide a valid list of portfolios")); } return(Ok(assetSaleResponse)); } } catch (Exception ex) { return(BadRequest(ex.Message)); } }
public async Task <IActionResult> SellMutualFund(MutualFundDetails mutualFundDetails) { try{ if (CheckValid()) { int portfolioid = Convert.ToInt32(HttpContext.Session.GetString("Id")); _log4net.Info("Selling mutual fund" + mutualFundDetails.MutualFundName + " of user with id:" + portfolioid); AssetSaleResponse assetSaleResponse = await _customerRepository.sellMutualFunds(portfolioid, mutualFundDetails); _log4net.Info("sale of mutual fund of user with id" + portfolioid + "done"); Sale _sale = new Sale(); _sale.PortFolioID = 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 IActionResult SellAssets(List <PortFolioDetails> listOfAssetsCurrentlyHoldingAndAssetsToSell) { try { AssetSaleResponse assetSaleResponse = new AssetSaleResponse(); if (listOfAssetsCurrentlyHoldingAndAssetsToSell == null || listOfAssetsCurrentlyHoldingAndAssetsToSell.Any() == false) { _log4net.Info("The portfolio provided don't hold any data"); return(NotFound("Please Provide a Valid List of portFolios")); } else { foreach (PortFolioDetails portFolioDetails in listOfAssetsCurrentlyHoldingAndAssetsToSell) { if (portFolioDetails == null || portFolioDetails.PortFolioId == 0) { return(NotFound("We didn't find any portfolio with that Id")); } } assetSaleResponse = _netWorthProvider.sellAssets(listOfAssetsCurrentlyHoldingAndAssetsToSell); if (assetSaleResponse == null) { _log4net.Info("Couldn't be sold because of invalid portfolio"); return(NotFound("Please provide a valid list of portfolios")); } _log4net.Info("The response is" + JsonConvert.SerializeObject(assetSaleResponse)); return(Ok(assetSaleResponse)); } } catch (Exception ex) { _log4net.Info("An exception occured while calculating the networth:" + ex + " In the controller" + nameof(SellAssets)); return(new StatusCodeResult(500)); } }
public void Setup() { networth = 10789.97; netWorth = new NetWorth(); netWorth.Networth = networth; assetSaleResponse = new AssetSaleResponse() { SaleStatus = true, Networth = 12456.44 }; _portFolioDetails = new List <PortFolioDetails>() { new PortFolioDetails { PortFolioId = 123, MutualFundList = new List <MutualFundDetails>() { new MutualFundDetails { MutualFundName = "Cred", MutualFundUnits = 3 }, new MutualFundDetails { MutualFundName = "Viva", MutualFundUnits = 5 } }, StockList = new List <StockDetails>() { new StockDetails { StockCount = 1, StockName = "BTC" }, new StockDetails { StockCount = 6, StockName = "ETH" } } }, new PortFolioDetails { PortFolioId = 12345, MutualFundList = new List <MutualFundDetails>() { new MutualFundDetails { MutualFundName = "Cred", MutualFundUnits = 1 }, new MutualFundDetails { MutualFundName = "Viva", MutualFundUnits = 1 } }, StockList = new List <StockDetails>() { new StockDetails { StockCount = 1, StockName = "BTC" }, new StockDetails { StockCount = 2, StockName = "ETH" } } } }; }
/// <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 AssetSaleResponse sellAssets(List <PortFolioDetails> listOfAssetsCurrentlyHoldingAndAssetsToSell) { _log4net.Info("Provider called from Controller to sell some assets"); AssetSaleResponse assetSaleResponse = _netWorthRepository.sellAssets(listOfAssetsCurrentlyHoldingAndAssetsToSell); if (assetSaleResponse == null) { return(null); } return(assetSaleResponse); }
public ActionResult <AssetSaleResponse> SellAssets([FromBody] PortfolioDetails portfolioDetails) { if (portfolioDetails == null) { return(this.BadRequest("Empty Input")); } AssetSaleResponse assetSaleResponse = new AssetSaleResponse { Networth = _PortfolioService.CalculateNetworth(portfolioDetails).Result, SaleStatus = true }; return(assetSaleResponse); }
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> /// Selling the assets of the user. The lsit contains two portfolios ofthe same person. In the first, we have his /// current portfolio and in the other, we have the portfolio he wants to sell. /// </summary> /// <param name="listOfAssetsCurrentlyHoldingAndAssetsToSell"></param> /// <returns></returns> public AssetSaleResponse sellAssets(List <PortFolioDetails> listOfAssetsCurrentlyHoldingAndAssetsToSell) { AssetSaleResponse assetSaleResponse = new AssetSaleResponse(); try { if (listOfAssetsCurrentlyHoldingAndAssetsToSell.Any() == false) { return(null); } _log4net.Info(nameof(sellAssets) + " method called to sell assets of user with id = " + listOfAssetsCurrentlyHoldingAndAssetsToSell[0].PortFolioId); assetSaleResponse = _netWorthRepository.sellAssets(listOfAssetsCurrentlyHoldingAndAssetsToSell); } catch (Exception ex) { _log4net.Error("Exception occured while selling the assets:" + ex.Message); } return(assetSaleResponse); }
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); }
/// <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); }
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")); } }