Esempio n. 1
0
        public async Task <Result <PurseDto> > ConvertCurrencies(ConvertCurrenciesParam param)
        {
            var availableCurrencies = await _currencyProvider.GetAvailableCurrencies();

            if (!availableCurrencies.Contains(param.Currency) || !availableCurrencies.Contains(param.ToCurrency))
            {
                return(new Result <PurseDto>().SetUnprocessable("Currency not supported"));
            }

            var ratio = await _currencyProvider.GetCurrenciesRatio();

            var purse = await _purseRepository.GetOrCreatePurse(param.UserId.Value);

            if (!purse.TryConvertCurrencies(param.Currency, param.ToCurrency, ratio[param.Currency], ratio[param.ToCurrency], param.Sum.Value))
            {
                return(new Result <PurseDto>().SetUnprocessable("Not Enough Money"));
            }

            await _purseRepository.Commit();

            return(new Result <PurseDto>(purse.ToDto()));
        }