Ejemplo n.º 1
0
        public IActionResult OnPost()
        {
            if (!ModelState.IsValid)
            {
                throw new Exception("Antiforgery check failed.");
            }

            var initResult = Init();

            if (initResult != null)
            {
                return(initResult);
            }

            InitPage();

            try
            {
                Id             = Plan.Id;
                Plan.Name      = Name;
                Plan.StartDate = StartDate;
                Plan.EndDate   = EndDate;
                Plan.Type      = Type;

                if (StartDate.HasValue)
                {
                    Plan.StartDate = StartDate.ConvertAppDateToUtc();
                }
                else
                {
                    Plan.StartDate = null;
                }

                if (EndDate.HasValue)
                {
                    Plan.EndDate = EndDate.ConvertAppDateToUtc();
                }
                else
                {
                    Plan.EndDate = null;
                }

                if (StartTimespan.HasValue)
                {
                    StartTimespan      = StartTimespan.ConvertAppDateToUtc();
                    Plan.StartTimespan = StartTimespan.Value.Hour * 60 + StartTimespan.Value.Minute;
                }
                else
                {
                    Plan.StartTimespan = null;
                }

                if (EndTimespan.HasValue)
                {
                    EndTimespan = EndTimespan.ConvertAppDateToUtc();

                    Plan.EndTimespan = EndTimespan.Value.Hour * 60 + EndTimespan.Value.Minute;
                    if (Plan.EndTimespan == 0)
                    {
                        Plan.EndTimespan = 1440;
                    }
                }
                else
                {
                    Plan.EndTimespan = null;
                }

                Plan.IntervalInMinutes = IntervalInMinutes;

                Plan.Enabled = Enabled;

                Plan.ScheduledDays.ScheduledOnMonday    = false;
                Plan.ScheduledDays.ScheduledOnTuesday   = false;
                Plan.ScheduledDays.ScheduledOnWednesday = false;
                Plan.ScheduledDays.ScheduledOnThursday  = false;
                Plan.ScheduledDays.ScheduledOnFriday    = false;
                Plan.ScheduledDays.ScheduledOnSaturday  = false;
                Plan.ScheduledDays.ScheduledOnSunday    = false;

                foreach (var schDay in ScheduledDays)
                {
                    if (DayOfWeek.Monday.ToString() == schDay)
                    {
                        Plan.ScheduledDays.ScheduledOnMonday = true;
                    }

                    if (DayOfWeek.Tuesday.ToString() == schDay)
                    {
                        Plan.ScheduledDays.ScheduledOnTuesday = true;
                    }

                    if (DayOfWeek.Wednesday.ToString() == schDay)
                    {
                        Plan.ScheduledDays.ScheduledOnWednesday = true;
                    }

                    if (DayOfWeek.Thursday.ToString() == schDay)
                    {
                        Plan.ScheduledDays.ScheduledOnThursday = true;
                    }

                    if (DayOfWeek.Friday.ToString() == schDay)
                    {
                        Plan.ScheduledDays.ScheduledOnFriday = true;
                    }

                    if (DayOfWeek.Saturday.ToString() == schDay)
                    {
                        Plan.ScheduledDays.ScheduledOnSaturday = true;
                    }

                    if (DayOfWeek.Sunday.ToString() == schDay)
                    {
                        Plan.ScheduledDays.ScheduledOnSunday = true;
                    }
                }

                ValidationException valEx = new ValidationException();

                if (string.IsNullOrWhiteSpace(Name))
                {
                    valEx.AddError("Name", "Name is required field and cannot be empty.");
                }

                if (Plan.StartDate >= Plan.EndDate)
                {
                    valEx.AddError("StartDate", "Start date must be before end date.");
                    valEx.AddError("EndDate", "End date must be greater than start date.");
                }

                if ((Plan.Type == SchedulePlanType.Daily || Plan.Type == SchedulePlanType.Interval) && !Plan.ScheduledDays.HasOneSelectedDay())
                {
                    valEx.AddError("ScheduledDays", "At least one day have to be selected for schedule days field.");
                }

                if (Plan.Type == SchedulePlanType.Interval && (Plan.IntervalInMinutes <= 0 || Plan.IntervalInMinutes > 1440))
                {
                    valEx.AddError("IntervalInMinutes", "The value of Interval in minutes field must be greater than 0 and less or  equal than 1440.");
                }

                valEx.CheckAndThrow();

                if (Plan.Enabled)
                {
                    Plan.NextTriggerTime = ScheduleManager.Current.FindSchedulePlanNextTriggerDate(Plan);
                }
                else
                {
                    Plan.NextTriggerTime = null;
                }

                ScheduleManager.Current.UpdateSchedulePlan(Plan);

                BeforeRender();
                return(Redirect(ReturnUrl));
            }
            catch (ValidationException ex)
            {
                Validation.Message = ex.Message;
                Validation.Errors  = ex.Errors;

                BeforeRender();
                return(Page());
            }
        }