public PremiumCalculatorResponse CalculatePremium(PremiumCalculatorRequest calculator) { try { ValidatePremiumCalculatorRequest(calculator); PremiumCalculatorDTO calculatorDTO = new PremiumCalculatorDTO { Age = calculator.Age, BirthDate = calculator.BirthDate, State = calculator.State, BirthDateFormatted = DateTime.ParseExact(calculator.BirthDate, Formats.DateFormatValid, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind) }; calculatorDTO.NumberOfMonth = calculatorDTO.BirthDateFormatted.Month; return(_premiumCalculatorRepository.CalculatePremium(calculatorDTO)); } catch (Exception e) { HttpResponseException ex = new HttpResponseException { Value = e.Message, Status = (int)HttpStatusCode.InternalServerError }; throw ex; } }
public PremiumCalculatorResponse CalculatePremium(PremiumCalculatorDTO calculatorDTO) { DataTable dt = GetPremiumRules(); var availableStates = (from states in dt.AsEnumerable() where states.Field <string>(StaticColumns.StateColumn) != StaticStrings.GenericCodeState select states.Field <string>(StaticColumns.StateColumn) ).Distinct().ToList(); var availableMonths = (from months in dt.AsEnumerable() where months.Field <int>(StaticColumns.MonthColumn) != StaticNumbers.GenericCodeMonth select months.Field <int>(StaticColumns.MonthColumn) ).Distinct().ToList(); if (availableStates.Contains(calculatorDTO.State)) { if (!availableMonths.Contains(calculatorDTO.NumberOfMonth)) { calculatorDTO.NumberOfMonth = StaticNumbers.GenericCodeMonth; } } else { calculatorDTO.State = StaticStrings.GenericCodeState; calculatorDTO.NumberOfMonth = StaticNumbers.GenericCodeMonth; } try { PremiumCalculatorResponse premiumResponse = new PremiumCalculatorResponse { Premium = (from stateMonthsPremium in dt.AsEnumerable() where stateMonthsPremium.Field <string>(StaticColumns.StateColumn) == calculatorDTO.State where stateMonthsPremium.Field <int>(StaticColumns.MinimumAgeColumn) <= calculatorDTO.Age where stateMonthsPremium.Field <int>(StaticColumns.MaximumAgeColumn) >= calculatorDTO.Age where stateMonthsPremium.Field <int>(StaticColumns.MonthColumn) == calculatorDTO.NumberOfMonth select stateMonthsPremium.Field <double>(StaticColumns.PremiumColumn)).ToList().ElementAt(0) }; return(premiumResponse); } catch (Exception) { throw new Exception(ValidateMessages.RuleValidation); } }