public ServiceCallResult CreateStop(ScheduledStop stop) { ServiceCallResult result = new ServiceCallResult() { Success = false }; try { repository.Add<ScheduledStop>(stop); repository.SaveChanges(); result.Success = true; } catch (Exception ex) { string[] errors = new[] { ex.Message }; result.Errors = errors; result.Success = false; } return result; }
public ServiceCallResult EditStop(ScheduledStop stop) { ServiceCallResult result = new ServiceCallResult() { Success = false }; repository.SaveChanges(); return result; }
//generate instances of a recurring stop for the given time span private IEnumerable<ScheduledStop> CreateRecurringInstances(ScheduledStop recurringStop, DateTime from, DateTime to) { var instances = new List<ScheduledStop>(); DateTime possibleStopTime = CombineDateAndTime(from, recurringStop.From); if (possibleStopTime < recurringStop.RecurringStart.Value) possibleStopTime = CombineDateAndTime(recurringStop.RecurringStart.Value, recurringStop.From); while (possibleStopTime >= from && possibleStopTime <= to) { while (possibleStopTime.DayOfWeek != recurringStop.RecurringStart.Value.DayOfWeek) { possibleStopTime = possibleStopTime.AddDays(1); } if (possibleStopTime > to) break; DateTime possibleStopDate = possibleStopTime.Date; var exceptions = repository.GetAll<RecurringException>().Where(e => e.ScheduledStopId == recurringStop.Id); if (exceptions.Count() == 0 || !exceptions.Any(e => e.Day == possibleStopDate)) { ScheduledStop newStop = new ScheduledStop() { Id = recurringStop.Id, From = possibleStopTime, To = CreateToDate(possibleStopTime, recurringStop.To.TimeOfDay), LocationId = recurringStop.LocationId, Location = recurringStop.Location, Recurring = true }; instances.Add(newStop); } possibleStopTime = possibleStopTime.AddDays(1); } return instances; }
public ActionResult Edit(ScheduledStopEditViewModel vm, int id) { var stop = schedService.GetStop(id); DateTime from; DateTime to; DateTime.TryParseExact(vm.SelectedFrom, @"h\:mm tt", CultureInfo.CurrentCulture, DateTimeStyles.None, out from); DateTime.TryParseExact(vm.SelectedTo, @"h\:mm tt", CultureInfo.CurrentCulture, DateTimeStyles.None, out to); if (!vm.Recurring && stop.Recurring) { var newStop = new ScheduledStop(); newStop = mapper.Map<ScheduledStop>(vm); newStop.FoodTruckId = stop.FoodTruckId; newStop.From = new DateTime(vm.Day.Value.Year, vm.Day.Value.Month, vm.Day.Value.Day, from.Hour, from.Minute, from.Second); newStop.To = new DateTime(vm.Day.Value.Year, vm.Day.Value.Month, vm.Day.Value.Day, to.Hour, to.Minute, to.Second); newStop.LastModifiedOn = DateTime.Now; newStop.Recurring = false; //create the 'new' stop schedService.CreateStop(newStop); //update the existing recurring stop with the exception schedService.CreateStopException(newStop.From, stop.Id); } else { stop.LocationId = vm.LocationId; stop.Recurring = vm.Recurring; stop.RecurringStart = vm.RecurringStart; stop.HasRecurringEnd = vm.HasRecurringEnd; stop.RecurringEnd = vm.RecurringEnd; stop.From = new DateTime(vm.Day.Value.Year, vm.Day.Value.Month, vm.Day.Value.Day, from.Hour, from.Minute, from.Second); stop.To = new DateTime(vm.Day.Value.Year, vm.Day.Value.Month, vm.Day.Value.Day, to.Hour, to.Minute, to.Second); stop.LastModifiedOn = DateTime.Now; schedService.EditStop(stop); } return RedirectToAction("Index", "Schedule", new { @name = stop.FoodTruck.UrlSlug }); }