public ValidationResult SyncLines() { var validation = new ValidationResult(); var linesToCreate = new List <Line>(); var linesToUpdate = new List <Line>(); try { //Get data from external resource var rioBusLines = _riobusRepository.GetAllLines().ToList(); //Analyse Objects foreach (var rioBusLine in rioBusLines) { //Get line from database var dbLine = _lineService.Get(rioBusLine.Line); if (dbLine == null) //If does not exist { //Add to Save linesToCreate.Add(new Line(rioBusLine.Line, rioBusLine.Description)); } else if (string.Equals(dbLine.Description.ToLower(), rioBusLine.Description.ToLower()) == false) { //Check if Description changed dbLine.Description = rioBusLine.Description; dbLine.LastUpdateDate = DateTime.UtcNow; //Add to update linesToUpdate.Add(dbLine); } } if (linesToCreate.Any()) { //Create _lineService.CreateLines(linesToCreate); } if (linesToUpdate.Any()) { foreach (var line in linesToUpdate) { //Update _lineService.Edit(line); } //Commit validation.AddError(_work.Commit()); } } catch (Exception e) { validation.AddError(new ValidationError(e.Message)); } return(validation); }
public IActionResult Edit(Input input) { if (!ModelState.IsValid) { return(Redirect($"/Line/Edit?id={input.Id}")); } var departureTime = new DateTime(); if (!DateTime.TryParse(input.DepartureTime, out departureTime)) { return(Redirect($"/Line/Edit?id={input.Id}")); } var arrivalTime = new DateTime(); if (!DateTime.TryParse(input.ArrivalTime, out arrivalTime)) { return(Redirect($"/Line/Edit?id={input.Id}")); } var date = new DateTime(); if (!DateTime.TryParse(input.Date, out date)) { return(Redirect($"/Line/Edit?id={input.Id}")); } if (arrivalTime < departureTime) { return(Redirect($"/Line/Edit?id={input.Id}")); } var line = new LineServiceModel { Id = input.Id, From = input.From, To = input.To, ArrivalTime = arrivalTime, DepartureTime = departureTime, Date = date }; lineService.Edit(line); return(Redirect("/Line/GetAll?page=1")); }