Ejemplo n.º 1
0
        public async Task <ResponseHelper <CurrencyPurchase> > BuyCurrencyAsync(CurrencyPurchase purchase)
        {
            CurrencyQuotation currencyQuotation = await GetCurrencyQuotationAsync(purchase.ISOCode);

            purchase.Amount /= currencyQuotation.PurchaseRate;

            CurrencyApiSetting currencySetting = CurrenciesSettings.CurrenciesApiSettings
                                                 .FirstOrDefault(c => c.ISOCode == purchase.ISOCode);

            decimal monthlyAmmout = GetUserMonthlyAmmount(purchase);

            List <string> errors = ValidatePurchase(purchase, currencySetting.Limit, monthlyAmmout);

            if (errors.Count > 0)
            {
                return(new ResponseHelper <CurrencyPurchase>(errors, statusCode: 400));
            }
            else
            {
                purchase.DateTime = DateTime.Now;
                UnitOfWork.GenericRepository <CurrencyPurchase>().Add(purchase);
                await UnitOfWork.CompleteAsync();

                return(new ResponseHelper <CurrencyPurchase>(purchase));
            }
        }
Ejemplo n.º 2
0
        public async Task <CurrencyQuotation> GetCurrencyQuotationAsync(string ISOCode)
        {
            CurrencyApiSetting currencySetting = CurrenciesSettings.CurrenciesApiSettings
                                                 .FirstOrDefault(c => c.ISOCode == ISOCode);

            JObject response = await _currenciesExternalService.GetCurrencyExchangeRateAsync(currencySetting.Url);

            CurrencyQuotation currencyQuote = new CurrencyQuotation()
            {
                ISOCode      = currencySetting.ISOCode,
                PurchaseRate = (decimal)response.SelectToken($"response{currencySetting.PurchaseRateField}") * currencySetting.QuoteRate,
                SaleRate     = (decimal)response.SelectToken($"response{currencySetting.SaleRateField}") * currencySetting.QuoteRate,
                LastUpdate   = DateTime.Now
            };

            return(currencyQuote);
        }