コード例 #1
0
        public IActionResult OnPost()
        {
            if (!ModelState.IsValid)
            {
                throw new Exception("Antiforgery check failed.");
            }

            InitPage();

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

                var locService = new LocalizationService();

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

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

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

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

                    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);

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