// TODO: orchestrate with Chain of Responsibility pattern public Validation ValidateConsoleInput(IList <string> input, out TimerDto timer) { timer = new TimerDto(); foreach (var validator in _consoleInputValidators) { foreach (var i in input) { var r = validator.IsValid(i); if (!r.IsValid) { return(r); } } } // Add them into the out objects timer.Entry = Convert.ToDateTime(input.ElementAt(0)); timer.Exit = Convert.ToDateTime(input.ElementAt(1)); foreach (var validator in _datesValidators) { var r = validator.IsValid(timer); if (!r.IsValid) { return(r); } } return(new Validation() { IsValid = true }); }
public string Calculate(DateTime entryDate, DateTime exitDate) { var input = new List <string>() { entryDate.ToString("dd/MM/yyyy hh:mm"), exitDate.ToString("dd/MM/yyyy hh:mm") }; var timer = new TimerDto(); var valid = _parkingService.ValidateInput(input, out timer); if (!valid.IsValid) { throw new Exception(valid.ErrorMessage); } try { var response = _parkingService.CalculateAsync(timer).Result; return(response); } catch (Exception ex) { throw new Exception(ex.Message); } }
public async Task <ParkingRateDto> Calculate(TimerDto input) { var specialRates = await _specialService.GetAllAsync(); var normalRates = await _normalService.GetAllAsync(); var result = new ParkingRateDto(); // TODO: Refactor this with Abstraction like IValidator // TODO: and orchestrate with Chain of Responsibility pattern var resultSpecial = _specialService.Calculate(specialRates, input.Entry, input.Exit); result = resultSpecial; var resultNormal = _normalService.Calculate(normalRates, input.Entry, input.Exit); // choose for either normal vs special if (resultNormal.Price > 0 && (result.Price == 0 || result.Price > resultNormal.Price)) { result.Name = resultNormal.Name; result.Price = resultNormal.Price; } return(result); }
public async Task <string> CalculateAsync(TimerDto input) { var response = await calculate(input); var result = $"\nRate : {response.Name}\nPayable amount : {response.Price.ToString("C")}"; return(result); }
public async Task <string> ProcessAsync(TimerDto input) { var response = await _calculatorService.Calculate(input); var result = "\nPackage : " + response.Name + "\nTOTAL COST : " + response.Price.ToString("C"); return(result); }
public IActionResult Index(string dateFrom, string dateTo) { //var localDate = DateTime.UtcNow.ToLocalDateTime(); //var todayStartDate = new DateTime(localDate.Year, localDate.Month, localDate.Day, 0, 0, 0); //var todayEndDate = new DateTime(localDate.Year, localDate.Month, localDate.Day, 23, 59, 59); //var startDate = string.IsNullOrEmpty(dateFrom) ? todayStartDate : Convert.ToDateTime(DateTime.ParseExact( // dateFrom, "dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture)); //var endDate = string.IsNullOrEmpty(dateTo) ? todayEndDate : Convert.ToDateTime(DateTime.ParseExact( // dateTo, "dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture)).AddDays(1); if (string.IsNullOrEmpty(dateFrom) || string.IsNullOrEmpty(dateTo)) { return(View()); } var timer = new TimerDto(); var input = new List <string> { dateFrom, dateTo }; var valid = _applicationService.ValidateInput(input, out timer); if (!valid.IsValid) { _logger.LogError(valid.ErrorMessage); ViewData["message"] = valid.ErrorMessage; return(View()); } try { var response = _applicationService.ProcessAsync(timer).Result; ViewData["response"] = response; } catch (Exception ex) { ViewData["message"] = ex.Message; } return(View()); }
public static void Main(string[] args) { /* Autofac */ var builder = new ContainerBuilder(); builder.RegisterModule <MainModule>(); IContainer container = builder.Build(); var appService = container.Resolve <IApplicationService>(); var timer = new TimerDto(); var valid = new Validation(); while (!valid.IsValid) { var input = new List <string>(); System.Console.WriteLine("Enter entry date & time (DD/MM/YYYY HH:mm) : "); input.Add(System.Console.ReadLine()); System.Console.WriteLine("Enter exit date & time (DD/MM/YYYY HH:mm) : "); input.Add(System.Console.ReadLine()); valid = appService.ValidateConsoleInput(input, out timer); if (!valid.IsValid) { System.Console.WriteLine(valid.ErrorMessage); } } try { var response = appService.ProcessAsync(timer).Result; System.Console.WriteLine(response); } catch (Exception ex) { System.Console.WriteLine(ex.Message + "\n"); } System.Console.WriteLine("Press any key to exit..."); System.Console.ReadLine(); }
//Todo: transfer the calculation logic to the Domain project (ParkingManager) private async Task <ParkingRateDto> calculate(TimerDto input) { var normalRates = await _normalRepository.SelectAllAsync(); var specialRates = await _specialRepository.SelectAllAsync(); var result = new ParkingRateDto(); var resultSpecial = calculateSpecial(specialRates, input.Entry, input.Exit); result = resultSpecial; var resultNormal = calculateNormal(normalRates, input.Entry, input.Exit); if (resultNormal.Price > 0 && (result.Price == 0 || result.Price > resultNormal.Price)) { result.Name = resultNormal.Name; result.Price = resultNormal.Price; } return(result); }
public async Task <ParkingRateDto> Calculate(TimerDto input) { var specialRates = await _specialService.GetAllAsync(); var standardRates = await _standardService.GetAllAsync(); var result = new ParkingRateDto(); var resultSpecial = _specialService.Calculate(specialRates, input.Entry, input.Exit); result = resultSpecial; var resultStandard = _standardService.Calculate(standardRates, input.Entry, input.Exit); // select the lowest price if (resultStandard.Price > 0 && (result.Price == 0 || result.Price > resultStandard.Price)) { result.Name = resultStandard.Name; result.Price = resultStandard.Price; } return(result); }