Exemplo n.º 1
0
        public async Task ConvCmdAsync(string amount, string from, [Remainder] string to)
        {
            amount = amount.Replace(',', '.');
            if (!double.TryParse(amount, out var amountD))
            {
                await Context.MarkCmdFailedAsync($"Unable to parse {amount} to double").ConfigureAwait(false);

                return;
            }

            if (to.StartsWith("to ", StringComparison.OrdinalIgnoreCase))
            {
                to = to.Substring(3);
            }

            var result = await Fixer.ConvertAsync(from, to, amountD).ConfigureAwait(false);

            result = Math.Round(result, 2);
            var embed = new EmbedBuilder()
                        .WithColor(Color.DarkGreen)
                        .WithAuthor(new EmbedAuthorBuilder()
                                    .WithName(Context.User.GetNickname())
                                    .WithIconUrl(Context.User.GetAvatarUrl() ?? Context.User.GetDefaultAvatarUrl()))
                        .WithCurrentTimestamp()
                        .WithDescription($"{amountD.ToString(CultureInfo.InvariantCulture)} {from.ToUpper()} = {result.ToString(CultureInfo.InvariantCulture).Bold()} {to.ToUpper().Bold()}");

            await ReplyEmbedAsync(embed).ConfigureAwait(false);
        }
Exemplo n.º 2
0
        public async Task <IActionResult> ConvertAmount(CurrencyViewModel model)
        {
            try{
                var convertedAmount = Math.Round(await Fixer.ConvertAsync(
                                                     model.SourceCurrency,
                                                     model.TargetCurrency,
                                                     model.Amount), 2).ToString("N2");

                var exchangeRate = await Fixer.RateAsync(model.SourceCurrency, model.TargetCurrency);

                await LogCurrencyConversion(model, exchangeRate.Rate, convertedAmount);

                var currencyLogs = new List <CurrencyLoggingModel>();
                if (model.FromDate != null && model.ToDate != null)
                {
                    currencyLogs = await _currencyLoggingService.GetCurrencyLogs(
                        model.FromDate,
                        model.ToDate,
                        model.SourceCurrencyId,
                        model.TargetCurrencyId
                        );
                }

                var response = new
                {
                    amount       = convertedAmount,
                    returnedLogs = currencyLogs
                };

                return(Ok(response));
            }
            catch (Exception ex)
            {
                _logger.Error(string.Format("An error occurred while converting the currency ({0} to {1} for {2}) : {3}", model.SourceCurrency, model.TargetCurrency, model.Amount, ex.Message));
                return(BadRequest());
            }
        }