/// <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); }
/// <summary> /// Calculates the final price of a customized product /// </summary> /// <param name="customizedProductTotalPriceModelView">ModelView containing the detailed info about the customized product's price</param> /// <param name="fetchCustomizedProductPriceModelView">ModelView containing information about currency/area conversion</param> /// <returns>PriceModelView containing the final price of a customized product</returns> private static PriceModelView calculateFinalPriceOfCustomizedProduct(CustomizedProductFinalPriceModelView customizedProductTotalPriceModelView, FetchCustomizedProductPriceModelView fetchCustomizedProductPriceModelView) { PriceModelView finalPriceModelView = new PriceModelView(); double finalPrice = 0; foreach (CustomizedProductPriceModelView customizedProductPrice in customizedProductTotalPriceModelView.customizedProducts) { finalPrice += customizedProductPrice.price.value; } finalPriceModelView.value = finalPrice; if (fetchCustomizedProductPriceModelView.currency != null && fetchCustomizedProductPriceModelView.area != null) { finalPriceModelView.currency = fetchCustomizedProductPriceModelView.currency; } else { finalPriceModelView.currency = CurrencyPerAreaConversionService.getBaseCurrency(); } return(finalPriceModelView); }
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))); } }