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); }
public async Task <int> Handle(CreateWalletRequest request, CancellationToken cancellationToken) { var notSupportedCurrencyCodes = request.AvailableCurrencyCodes.Where(c => !ConversionRateProvider.SupportedCurrencyCodes.Contains(c)).ToArray(); if (notSupportedCurrencyCodes.Any()) { throw new BusinessException($"Не удалось создать кошелек. Невозможно создать кошелек со счетами для валют: {notSupportedCurrencyCodes.ToCommaSeparated()}."); } var wallet = new Data.Domain.Wallet { UserId = request.UserId }; _walletRepository.Save(wallet); await _unitOfWork.CommitAsync(cancellationToken); var currencyAccounts = request.AvailableCurrencyCodes.Select(c => new CurrencyAccount { CurrencyCode = c, Balance = 0, WalletId = wallet.Id }); foreach (var currencyAccount in currencyAccounts) { _currencyAccountRepository.Save(currencyAccount); } await _unitOfWork.CommitAsync(cancellationToken); return(wallet.Id); }
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); }
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); }