public IActionResult SaveConvertedCurrency([FromBody] ConvertedCurrency convertedCurrency)
        {
            convertedCurrency = _operationServices.SaveConvertedCurrency(convertedCurrency);

            if (convertedCurrency == null)
            {
                return(NotFound());
            }

            return(Ok(convertedCurrency));
        }
Ejemplo n.º 2
0
        private const string ApiKey = "36aa5f091ff20d27a16f"; // 183615e1b9c729e8ac6e

        public async ValueTask <ConvertedCurrency> ConvertAsync(string from, string to, IProgress <int> progress, CancellationToken ct)
        {
            if (_cachedConvertedCurrencies.TryGetValue($"{from}_{to}", out var value))
            {
                return(value);
            }

            progress.Report(2);
            using var response = await _client.GetAsync($"convert?q={from}_{to},{to}_{from}&compact=ultra&apiKey={ApiKey}", ct);

            if (response.StatusCode != HttpStatusCode.OK)
            {
                progress.Report(0);
                return(null);
            }

            progress.Report(4);
            var jsonStream = await response.Content.ReadAsStreamAsync();

            progress.Report(6);
            using var jsonDocument = await JsonDocument.ParseAsync(jsonStream, cancellationToken : ct);

            progress.Report(8);
            foreach (var jsonObject in jsonDocument.RootElement.EnumerateObject())
            {
                if (!jsonObject.Value.TryGetDecimal(out var convertedValue))
                {
                    continue;
                }

                var separated         = jsonObject.Name.Split('_');
                var convertedCurrency = new ConvertedCurrency
                {
                    From  = separated[0],
                    To    = separated[1],
                    Value = convertedValue
                };
                _cachedConvertedCurrencies.TryAdd(jsonObject.Name, convertedCurrency);

                if (convertedCurrency.From == from && convertedCurrency.To == to)
                {
                    value = convertedCurrency;
                }
            }

            progress.Report(0);
            return(value);
        }
        public ConvertedCurrency SaveConvertedCurrency(ConvertedCurrency convertedCurrency)
        {
            try {
                decimal valueSubtotal = ConvertCurrencyValue(convertedCurrency.ValueToconvert, convertedCurrency.BaseValue);
                decimal getTotalValueBeforeCalcIofAndSpread = CalcIofAndSpread(valueSubtotal, convertedCurrency.IOF, convertedCurrency.Spread, convertedCurrency.BaseValue);

                convertedCurrency.TotalValueConverted = getTotalValueBeforeCalcIofAndSpread;

                SaveOperationTransaction(convertedCurrency);

                var saveConvertedCurrency = _context.Add(convertedCurrency).Entity;
                _context.SaveChanges();

                return(saveConvertedCurrency);
            }catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public void SaveOperationTransaction(ConvertedCurrency convertedCurrency)
        {
            try
            {
                Operation operation = new Operation()
                {
                    FromCoin       = convertedCurrency.FromCoin,
                    ToCoin         = convertedCurrency.ToCoin,
                    IOF            = convertedCurrency.IOF,
                    OperationDate  = DateTime.Now,
                    OperationValue = convertedCurrency.TotalValueConverted.Value,
                    Spread         = convertedCurrency.Spread
                };

                var saveOperation = _context.Add(operation);
                _context.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }