Example #1
0
        public async Task <Unit> Handle(ConvertMoneyRequest request, CancellationToken cancellationToken)
        {
            if (request.SourceCurrencyAmount <= 0)
            {
                throw new BusinessException("Сумма должна быть дольше нуля.");
            }

            var sourceAccount = await _currencyAccountRepository.GetByIdAsync(request.SourceAccountId);

            var destinationAccount = await _currencyAccountRepository.GetByIdAsync(request.DestinationAccountId);

            if (sourceAccount.Balance < request.SourceCurrencyAmount)
            {
                throw new BusinessException($"На счете {sourceAccount.CurrencyCode} недостаточно средств.");
            }

            var conversionRate = await _conversionRateProvider.GetConversionRateAsync(sourceAccount.CurrencyCode, destinationAccount.CurrencyCode);

            sourceAccount.Balance      -= request.SourceCurrencyAmount;
            destinationAccount.Balance += request.SourceCurrencyAmount * conversionRate;

            _currencyAccountRepository.Save(sourceAccount);
            _currencyAccountRepository.Save(destinationAccount);
            await _unitOfWork.CommitAsync(cancellationToken);

            return(Unit.Value);
        }
Example #2
0
        public async Task <Unit> Handle(DepositMoneyRequest request, CancellationToken cancellationToken)
        {
            if (request.Amount <= 0)
            {
                throw new BusinessException("Сумма должна быть дольше нуля.");
            }

            var account = await _currencyAccountRepository.GetByIdAsync(request.CurrencyAccountId);

            account.Balance += request.Amount;

            _currencyAccountRepository.Save(account);
            await _unitOfWork.CommitAsync(cancellationToken);

            return(Unit.Value);
        }
Example #3
0
        public async Task <Unit> Handle(WithdrawMoneyRequest request, CancellationToken cancellationToken)
        {
            if (request.Amount <= 0)
            {
                throw new BusinessException("Сумма должна быть дольше нуля.");
            }

            var account = await _currencyAccountRepository.GetByIdAsync(request.CurrencyAccountId);

            if (account.Balance < request.Amount)
            {
                throw new BusinessException($"На счете {account.CurrencyCode} недостаточно средств.");
            }

            account.Balance -= request.Amount;

            _currencyAccountRepository.Save(account);
            await _unitOfWork.CommitAsync(cancellationToken);

            return(Unit.Value);
        }