/// <summary>
        /// Converts a given price in a given currency per area to another currency per area
        /// </summary>
        /// <param name="convertPriceModelView">Model View with all the necessary conversion information</param>
        /// <param name="clientFactory">ClientFactory to create the HTTP Client to get currency conversion rates</param>
        /// <returns>ModelView with the converted price value</returns>
        public async Task <PriceModelView> convertPrice(ConvertPriceModelView convertPriceModelView, IHttpClientFactory clientFactory)
        {
            PriceModelView convertedPrice = new PriceModelView();

            if (Double.IsNaN(convertPriceModelView.value) || Double.IsInfinity(convertPriceModelView.value) || Double.IsNegative(convertPriceModelView.value))
            {
                throw new ArgumentException(INVALID_VALUE_TO_CONVERT);
            }

            convertedPrice.value =
                await new CurrencyPerAreaConversionService(clientFactory)
                .convertCurrencyPerArea(convertPriceModelView.value, convertPriceModelView.fromCurrency, convertPriceModelView.toCurrency, convertPriceModelView.fromArea, convertPriceModelView.toArea);

            convertedPrice.currency = convertPriceModelView.toCurrency;
            convertedPrice.area     = convertPriceModelView.toArea;

            return(convertedPrice);
        }
Beispiel #2
0
 public async Task <ActionResult> convertPrice([FromQuery] string fromCurrency, [FromQuery] string toCurrency, [FromQuery] string fromArea, [FromQuery] string toArea, [FromQuery] double value)
 {
     try
     {
         ConvertPriceModelView convertPriceModelView = new ConvertPriceModelView();
         convertPriceModelView.fromCurrency = fromCurrency;
         convertPriceModelView.toCurrency   = toCurrency;
         convertPriceModelView.fromArea     = fromArea;
         convertPriceModelView.toArea       = toArea;
         convertPriceModelView.value        = value;
         PriceModelView convertedPrice = await new core.application.CurrenciesPerAreaController().convertPrice(convertPriceModelView, clientFactory);
         return(Ok(convertedPrice));
     }
     catch (ArgumentException e)
     {
         return(BadRequest(new SimpleJSONMessageService(e.Message)));
     }
     catch (Exception)
     {
         return(StatusCode(500, new SimpleJSONMessageService(UNEXPECTED_ERROR)));
     }
 }