public ActionResult Calculate(DimensionInputModel input) { var result = new GenericJsonResult(); if (!ModelState.IsValid) { result = new GenericJsonResult() { Data = PartialView("ErrorSection", ModelState.Where(x => x.Value.Errors.Any()).Select(x => $"{x.Key} : { String.Join(" ", x.Value.Errors.Select(y => y.ErrorMessage))}")).RenderToString(), Success = false }; } else { var parameters = new CalculationCostParameters() { DimensionsParameters = new DimensionsParameters() { Depth = Convert.ToInt32(input.Depth), Height = Convert.ToInt32(input.Height), Width = Convert.ToInt32(input.Width) }, WieghtParameters = new WeightParameters() { Weight = Convert.ToInt32(input.Weight) } }; CalculationResultModel calculationResult = packagePriceCalculation.CalculateCost(parameters); var Packages = new List <PackageViewModel>(); Packages = calculationResult.SingleCompanyCosts.Select(x => new PackageViewModel() { CompanyName = x.CompanyName, PriceTag = new PriceTagViewModel() { Currency = "EUR", PriceType = x.CostType.ToString(), PriceValue = x.Cost.ToString() }, Promoted = x.Promoted }).ToList(); result = new GenericJsonResult() { Data = PartialView("CalculationResult", Packages).RenderToString(), Success = true }; } return(Json(result, JsonRequestBehavior.AllowGet)); }
public CalculationResultModel CalculateCost(CalculationCostParameters parameters) { var configs = companyCalculationConfigurationRepository.GetCofigs(); List <SingleCompanyCost> costs = configs.Companys.Select(x => CalculateCost(x, parameters)).Where(x => x != null).ToList(); costs = costs.OrderBy(x => x.Cost).ToList(); costs.First().Promoted = true; var result = new CalculationResultModel() { SingleCompanyCosts = costs }; return(result); }
private SingleCompanyCost CalculateCost(CompanyConfigurationModel config, CalculationCostParameters parameters) { decimal?weightPrice = CalculateWeight(config.Weight, parameters.WieghtParameters); decimal?dimensionPrice = CalculateDimensionPrice(config.Size, parameters.DimensionsParameters); if (weightPrice == null || dimensionPrice == null) { SaveNotValdiateInput(parameters); return(null); } var cost = Math.Max(weightPrice.Value, dimensionPrice.Value); SingleCompanyCost result = new SingleCompanyCost() { CompanyName = config.Name, Promoted = false, Cost = cost, CostType = cost == weightPrice ? CostType.Weight : CostType.Dimension }; return(result); }
private void SaveNotValdiateInput(CalculationCostParameters parameters) { this.inputLogger.SaveWrongInput($"{parameters.DimensionsParameters.Depth}|{parameters.DimensionsParameters.Height}|{parameters.DimensionsParameters.Width}|{parameters.WieghtParameters.Weight}|{DateTime.Now}"); }