Пример #1
0
        public ActionResult DoExchange(Guid id, [FromBody] DoExchangeRequest request)
        {
            var exchange     = _repository.GetByApiKey(id);
            var upCurrencies = request.Currencies.ToUpper();
            var from         = upCurrencies.Substring(0, 3);
            var to           = upCurrencies.Substring(3);

            if (!exchange.CanExchange(request.Currencies, request.Amount))
            {
                return(BadRequest("Couldn't perform the exchange.\n" +
                                  $"Required amount: {request.Amount}\n" +
                                  $"Actual balance: {exchange.Balances[from]}"));
            }

            var amountUsd = request.Amount * exchange.GetPrice(from + "USD");
            var percent   = amountUsd / exchange.GetFullBalanceInUsd() * 100;

            _repository.Percentages[from] -= percent;
            _repository.Percentages[to]   += percent;
            _repository.ApplyPercentageChanges();
            return(new JsonResult(_repository.GetAll()));
        }