예제 #1
0
        public async Task <DateTime> Transfer(int accountFromId, int accountToId, decimal amount)
        {
            if (amount < 0)
            {
                throw new ArgumentException("Сумма перевода не должна быть меньше нуля", nameof(amount));
            }

            var accountFrom = await _repository.LoadAsync <Account>(accountFromId);

            if (accountFrom == null)
            {
                throw new ArgumentException($"Нет счета с Id = {accountFromId}");
            }
            var accountTo = await _repository.LoadAsync <Account>(accountToId);

            if (accountTo == null)
            {
                throw new ArgumentException($"Нет счета с Id = {accountToId}");
            }

            if (accountToId == accountFromId)
            {
                throw new InvalidOperationException("Нельзя вполнить перевод между одинковыми счетами");
            }
            if (accountFrom.Balance < amount)
            {
                throw new InvalidOperationException(
                          $"Невозможно перевести сумму {amount} со счета \"{accountFrom.Name}\", так как на его балансе не хватает средств");
            }

            var transferTime = _timeService.ClientLocalNow;

            accountFrom.Balance      -= amount;
            accountFrom.AvailBalance -= amount;
            accountTo.Balance        += amount;
            accountTo.AvailBalance   += amount;
            _repository.Update(accountFrom);
            _repository.Update(accountTo);
            _transactionBuilder.CreateTransfer(accountFrom, accountTo, transferTime, amount);
            await _repository.SaveChangesAsync().ConfigureAwait(false);

            return(transferTime);
        }