Beispiel #1
0
        public async Task <ActionResult> Order(BrokerOrderDto order)
        {
            try
            {
                var user = Convert.ToInt32(_context.HttpContext.User.Identity.Name);

                var clientList = await _bankService.GetBankClients(user);

                var client = clientList.First();

                var clientAccountList = await _bankService.GetBankAccount(client.Id);

                var clientAccount = clientAccountList.First();

                await _brokerService.ProcessOrder(clientAccount, order);

                return(Ok("Order accepted successfully"));
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Unable to buy stock");
                return(StatusCode(StatusCodes.Status500InternalServerError,
                                  "An error occurred and the server could not buy the stock. If the problem persists, contact an administrator"));
            }
        }
Beispiel #2
0
        public async Task ProcessOrder(BankAccountEntity bankAccount, BrokerOrderDto order)
        {
            try
            {
                var stockList = (await _stockRepository.Select(a => a.Symbol == order.Symbol)).ToList();

                if (stockList.Count() > 1)
                {
                    throw new ArgumentException($"Symbol {order.Symbol.ToUpper()} is not unique");
                }

                var stock = stockList.Single();

                var stockValue = stock.CurrentPrice * order.Amount;

                bankAccount.BalanceInBrl -= stockValue;

                if (bankAccount.BalanceInBrl < 0)
                {
                    throw new ArgumentException("Insufficient balance in account");
                }

                await _bankAccountRepository.Update(bankAccount);

                var accountStock = (await _bankAccountStockRepository.Select(a =>
                                                                             a.BankAccountId == bankAccount.Id && a.Symbol == order.Symbol)).SingleOrDefault();
                if (accountStock != null)
                {
                    accountStock.Amount += order.Amount;
                    if (accountStock.Amount < 0)
                    {
                        throw new ArgumentException("Insufficient stocks to sell");
                    }

                    await _bankAccountStockRepository.Update(accountStock);
                }
                else
                {
                    if (order.Amount < 0)
                    {
                        throw new ArgumentException("Insufficient stocks to sell");
                    }

                    var newAccountStock = new BankAccountStockEntity()
                    {
                        BankAccountId = bankAccount.Id,
                        Symbol        = stock.Symbol,
                        Amount        = order.Amount
                    };

                    await _bankAccountStockRepository.Insert(newAccountStock);
                }

                _bankAccountStockRepository.Save();
                _bankAccountRepository.Save();
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Service could not complete order");
                throw;
            }
        }