Beispiel #1
0
 public bool Equals(IScheduleElem obj)
 {
     if (obj.GetType() != this.GetType())
     {
         return(false);
     }
     return(Equals((Lesson)obj));
 }
Beispiel #2
0
 public bool Equals(IScheduleElem other)
 {
     if (other.GetType() != this.GetType())
     {
         return(false);
     }
     return(Equals((Week)other));
 }
        public static CheckResult CheckElemIsCorrect(this IScheduleElem scheduleElem)
        {
            var res = new CheckResult();

            if (scheduleElem == null)
            {
                return(res.AddError("Null schedule elem"));
            }
            try
            {
                switch (scheduleElem.Level)
                {
                case ScheduleElemLevel.Week:
                    if (!(scheduleElem is Week))
                    {
                        res.AddError($"level week is not week level {scheduleElem.Level}, type: {scheduleElem.GetType()}");
                    }
                    if (scheduleElem.Elems == null)
                    {
                        res.AddError("null children: " + JsonConvert.SerializeObject(scheduleElem));
                    }
                    else
                    {
                        res = scheduleElem.Elems?.Aggregate(res,
                                                            (result, elem) => result + CheckElemIsCorrect(elem)) ?? res;
                        var nonUniqueDays = scheduleElem.Elems?.OfType <Day>()?.GroupBy(d => d.DayOfWeek)
                                            ?.FirstOrDefault(group => @group.Count() > 1);
                        if (nonUniqueDays != null)
                        {
                            res.AddError($"not unique days in week: {nonUniqueDays.Key}");
                        }
                    }

                    break;

                case ScheduleElemLevel.Day:
                    if (!(scheduleElem is Day))
                    {
                        res.AddError($"level day is not day: level {scheduleElem.Level}, type: {scheduleElem.GetType()}");
                    }
                    else
                    {
                        var day = (Day)scheduleElem;
                        if (day.DayOfWeek > DayOfWeek.Saturday || day.DayOfWeek < DayOfWeek.Sunday)
                        {
                            res.AddError($"unknown dayofweek: {day.DayOfWeek}");
                        }
                    }

                    if (scheduleElem.Elems == null)
                    {
                        res.AddError("null children: " + JsonConvert.SerializeObject(scheduleElem));
                    }
                    else
                    {
                        res = scheduleElem.Elems?.Aggregate(res,
                                                            (result, elem) => result + CheckElemIsCorrect(elem)) ?? res;
                        scheduleElem.Elems?.OfType <Lesson>()?.OrderBy(l => l.BeginTime).Aggregate((prev, current) =>
                        {
                            if (current.BeginTime <= prev.BeginTime &&
                                (current.IsOnEvenWeek == null || prev.IsOnEvenWeek == null ||
                                 prev.IsOnEvenWeek == current.IsOnEvenWeek))
                            {
                                res.AddError($"incompatible lessons in day {((Day)scheduleElem).DayOfWeek}:" +
                                             JsonConvert.SerializeObject(prev) +
                                             JsonConvert.SerializeObject(current));
                            }
                            return(current);
                        });
                    }

                    break;

                case ScheduleElemLevel.Lesson:
                    if (!(scheduleElem is Lesson))
                    {
                        res.AddError("level lesson is not lesson" + JsonConvert.SerializeObject(scheduleElem));
                    }
                    if (scheduleElem.Elems != null)
                    {
                        res.AddError("not null children for lesson: " +
                                     JsonConvert.SerializeObject(scheduleElem));
                    }

                    break;

                default:
                    res.AddError($"Impossible to check for such level {scheduleElem.Level}");
                    break;
                }

                return(res);
            }
            catch (Exception e)
            {
                return(res.AddExeption(e));
            }
        }