public CurrencyExchangeRequest(CurrencyExchangeModel currencyExchangeRequest) { // Refactor option, use a automapper to map the model to this class // one class depends on another Amount = currencyExchangeRequest.Amount; SourceCurrency = currencyExchangeRequest.SourceCurrency; TargetCurrency = currencyExchangeRequest.TargetCurrency; }
public async Task <IActionResult> ConvertCurrency([FromBody] CurrencyExchangeModel currencyExchangeRequest) { var trackingId = Guid.NewGuid().ToString(); //ToDo Refactor: use fluentvalidation validate request and fail fast if an issue // & write tests for these cases if (currencyExchangeRequest == null) { _logger.LogError(_logFormatter.FormatMessage(LogType.Error, $"Request body is null {trackingId}")); return(Problem("Request Body is null or empty ", statusCode: (int)HttpStatusCode.BadRequest)); } if (currencyExchangeRequest.Amount <= 0) { _logger.LogError(_logFormatter.FormatMessage(LogType.Error, $"Amount should be greater than zero {trackingId}")); return(Problem("Amount should be greater than zero", statusCode: (int)HttpStatusCode.BadRequest)); } if (currencyExchangeRequest.TargetCurrency.Length != 3 || currencyExchangeRequest.SourceCurrency.Length != 3) { _logger.LogError(_logFormatter.FormatMessage(LogType.Error, $"Source and target currency should be 3 characters {trackingId}")); return(Problem("Source and target currency should be 3 characters", statusCode: (int)HttpStatusCode.BadRequest)); } if (!CurrencyStringService.IsAllUpper(currencyExchangeRequest.TargetCurrency) && CurrencyStringService.IsAllUpper(currencyExchangeRequest.SourceCurrency)) { _logger.LogError(_logFormatter.FormatMessage(LogType.Error, $"Source and target currency should be upper case {trackingId}")); return(Problem("Source and target currency should be upper case", statusCode: (int)HttpStatusCode.BadRequest)); } try { var result = await _mediator .Send(new CurrencyExchangeRequest(currencyExchangeRequest)); return(Ok(result)); } catch (Exception e) { _logger.LogError(_logFormatter.FormatMessage(LogType.Error, "API request failed with Exception"), e); Console.WriteLine(_logFormatter.FormatMessage(LogType.Error, "API request failed with Exception"), e); return(new StatusCodeResult(StatusCodes.Status500InternalServerError)); } }