private async Task InsertToSoldInstrumentTable(Stock stockDbOBj, SellStockDTO stockSaleInfo) { var client = _httpClientFactory.CreateClient(_exchangeRateApiSettings.ClientName); var exRateResponse = await client.SendAsync(new HttpRequestMessage(HttpMethod.Get, _exchangeRateApiSettings.BaseClient + _exchangeRateApiSettings.GetExchangeRateUri)); // placeholder for when ExRateStorage Cache is not working var exchangeRate = 1.33; if (exRateResponse.StatusCode == HttpStatusCode.OK) { var content = await exRateResponse.Content.ReadAsStringAsync(); exchangeRate = double.Parse(content); } if (stockDbOBj.Country == "US") { // create SoldInstrumentDTO with stockObj data and send to SoldInstrumentService var soldInstrumentDTO = new SoldInstrumentDTO { Amount = stockSaleInfo.Amount, Name = stockSaleInfo.Name, PurchasePrice = stockDbOBj.PurchasePrice, PuchaseDate = stockDbOBj.PurchaseDate, SalePrice = stockSaleInfo.SellPrice, SaleDate = DateTime.Now, DiscountApplied = stockDbOBj.PurchaseDate.AddMonths(12) <= DateTime.Now ? true : false, Profit = ((stockSaleInfo.SellPrice * exchangeRate) * stockSaleInfo.Amount) - ((stockDbOBj.PurchasePrice * exchangeRate) * stockSaleInfo.Amount) }; soldInstrumentDTO.CGTPayable = soldInstrumentDTO.DiscountApplied ? (soldInstrumentDTO.Profit * .5) : soldInstrumentDTO.Profit; await _soldInstrumentsRepository.AddSoldInstrumentToDBTable(soldInstrumentDTO); } else { // create SoldInstrumentDTO with stockObj data and send to SoldInstrumentService var soldInstrumentDTO = new SoldInstrumentDTO { Amount = stockSaleInfo.Amount, Name = stockSaleInfo.Name, PurchasePrice = stockDbOBj.PurchasePrice, PuchaseDate = stockDbOBj.PurchaseDate, SalePrice = stockSaleInfo.SellPrice, SaleDate = DateTime.Now, DiscountApplied = stockDbOBj.PurchaseDate.AddMonths(12) <= DateTime.Now ? true : false, Profit = stockSaleInfo.SellPrice * stockSaleInfo.Amount - stockDbOBj.PurchasePrice * stockSaleInfo.Amount }; soldInstrumentDTO.CGTPayable = !soldInstrumentDTO.DiscountApplied ? (soldInstrumentDTO.Profit * .5) : soldInstrumentDTO.Profit; await _soldInstrumentsRepository.AddSoldInstrumentToDBTable(soldInstrumentDTO); } }
public async Task <IActionResult> SellStockAsync([FromBody] SellStockDTO sellStockDtoParam) { try { var result = await _mediator.Send(new SellStockCommand { sellStockDTO = sellStockDtoParam }); if (result == null) { return(BadRequest()); } return(Ok()); } catch (Exception e) { Log.Error("Error in SellStock function - " + e.Message); return(Problem("Error in SellStockAsync function", statusCode: 500)); } }
public async Task <StockDTO> SellStockAsync(SellStockDTO stockBeingSold) { try { var stockObj = await _unitOfWork.Stocks.GetAll(q => q.Name == stockBeingSold.Name); var currentStockTotal = 0.0; foreach (var foundStock in stockObj) { currentStockTotal += foundStock.Amount; } if (stockBeingSold.Amount > currentStockTotal) { return(null); } var returnStock = new StockDTO(); var firstStockEntry = stockObj.First(); if (stockBeingSold.Amount < firstStockEntry.Amount) { firstStockEntry.Amount = firstStockEntry.Amount - stockBeingSold.Amount; _unitOfWork.Stocks.Update(firstStockEntry); await _unitOfWork.Save(); await InsertToSoldInstrumentTable(firstStockEntry, stockBeingSold); } else { foreach (var currentStock in stockObj) { if (stockBeingSold.Amount >= currentStock.Amount) { var saleAmount = new SellStockDTO(stockBeingSold); saleAmount.Amount = currentStock.Amount; stockBeingSold.Amount = stockBeingSold.Amount - currentStock.Amount; await _unitOfWork.Stocks.Delete(currentStock.Id); await InsertToSoldInstrumentTable(currentStock, saleAmount); } else { await InsertToSoldInstrumentTable(currentStock, stockBeingSold); currentStock.Amount = currentStock.Amount - stockBeingSold.Amount; if (currentStock.Amount == 0) { await _unitOfWork.Stocks.Delete(currentStock.Id); } else { _unitOfWork.Stocks.Update(currentStock); } returnStock = _mapper.Map <StockDTO>(currentStock); } } await _unitOfWork.Save(); } return(returnStock); } catch (Exception ex) { var message = ex.Message; throw ex; } }
public async Task <StockDTO> SellStockAsync(SellStockDTO stock) { var result = await _stocksRepository.SellStockAsync(stock); return(result); }