public async Task <ResponseDTO> RegisterExchangeRate(RegisterExchangeRateRequest request)
        {
            if (request.Target == request.Base)
            {
                return(ResponseDTO.Fail("Base and target currency can't be the same."));
            }

            var baseCurrency = await _context.CurrencySymbols.AsNoTracking()
                               .FirstOrDefaultAsync(x => x.Symbol == request.Base);

            var targetCurrency = await _context.CurrencySymbols.AsNoTracking()
                                 .FirstOrDefaultAsync(x => x.Symbol == request.Target);

            var currencyExistAndIsEnabled = CurrencyExistAndIsEnabled(baseCurrency, request.Base);

            if (!currencyExistAndIsEnabled)
            {
                return(currencyExistAndIsEnabled);
            }

            var targetCurrencyExist = CurrencyExistAndIsEnabled(targetCurrency, request.Target);

            if (!targetCurrencyExist)
            {
                return(targetCurrencyExist);
            }

            var exchangeRate = new Domain.ExchangeRate(request.Base, request.Target, request.Rate);

            _context.ExchangeRates.Add(exchangeRate);
            await _context.SaveChangesAsync();

            return(ResponseDTO.Ok());
        }
Ejemplo n.º 2
0
        public async Task Invoke(HttpContext context)
        {
            try
            {
                await _next(context);
            }
            catch (Exception ex)
            {
                try
                {
                    context.TraceIdentifier = Guid.NewGuid().ToString();
                    _logger.LogError(context.TraceIdentifier, ex);
                }
                catch (Exception ex2)
                {
                    _logger.LogError(0, ex2, "An exception was thrown attempting to execute the error handler.");
                }
            }

            try
            {
                await _next.Invoke(context);
            }
            catch (Exception ex)
            {
                context.TraceIdentifier = Guid.NewGuid().ToString();
                _logger.LogError(context.TraceIdentifier, ex, ex.Message);
                context.Response.StatusCode = 500;
            }

            if (!context.Response.HasStarted && context.Response.StatusCode != StatusCodes.Status204NoContent)
            {
                context.Response.ContentType = "application/json";

                ResponseDTO response;

                switch (context.Response.StatusCode)
                {
                case 401:
                    response = ResponseDTO.Fail("User not authenticated");

                    break;

                case 403:
                    response = ResponseDTO.Fail("User not authorized");
                    break;

                default:
                    response = ResponseDTO.FailWithRequestId(context.TraceIdentifier, "An error has ocurred.");
                    break;
                }

                var json = JsonConvert.SerializeObject(response, new JsonSerializerSettings {
                    ContractResolver = new CamelCasePropertyNamesContractResolver()
                });

                await context.Response.WriteAsync(json);
            }
        }
        private static ResponseDTO CurrencyExistAndIsEnabled(CurrencySymbol currency, string symbol)
        {
            if (currency == null)
            {
                return(ResponseDTO.Fail($"'{symbol}' currency is not registered."));
            }

            if (!currency.Enabled)
            {
                return(ResponseDTO.Fail($"'{symbol}' currency is disabled."));
            }

            return(true);
        }