Ejemplo n.º 1
0
        public BusySchedules AddBusyInterval(int userId, DateTime startOn, DateTime endsOn)
        {
            Users user = GetUserById(userId);

            if (user == null)
            {
                throw new NoEntryFoundException(userId, typeof(Users).Name);
            }

            if (startOn.CompareTo(endsOn) >= 0)
            {
                throw new InvalidPeriodRangeException(startOn, endsOn);
            }

            DateTime current = DateTime.UtcNow;

            if (endsOn.CompareTo(current) <= 0)
            {
                throw new CanNotAddPeriodsInThePastException(startOn, endsOn);
            }

            DateTime real = startOn.CompareTo(current) <= 0 ? current : startOn;

            BusySchedules newSchedule = new BusySchedules
            {
                UserId            = user.Id,
                BusyPeriodStartOn = real,
                BusyPeriodEndsOn  = endsOn
            };

            List <BusySchedules> existingSchedules = BusyScheduleRepository.GetAllInPeriod(user.Id, real, endsOn);

            if (existingSchedules.Count == 0)
            {
                BusyScheduleRepository.Add(newSchedule);
                return(newSchedule);
            }

            newSchedule = SolveOverlappedSchedules(newSchedule, existingSchedules);
            bool added = false;

            foreach (var sch in existingSchedules)
            {
                if (!added && sch.BusyPeriodStartOn.CompareTo(newSchedule.BusyPeriodStartOn) == 0)
                {
                    sch.BusyPeriodStartOn = newSchedule.BusyPeriodStartOn;
                    sch.BusyPeriodEndsOn  = newSchedule.BusyPeriodEndsOn;
                    BusyScheduleRepository.Update(sch);
                    added       = true;
                    newSchedule = sch;
                }
                else
                {
                    BusyScheduleRepository.Remove(sch);
                }
            }

            return(newSchedule);
        }
Ejemplo n.º 2
0
        public bool RemoveBusyInterval(int userId, int scheduleId)
        {
            Users user = GetUserById(userId);

            if (user == null)
            {
                throw new NoEntryFoundException(userId, typeof(Users).Name);
            }

            BusySchedules existing = BusyScheduleRepository.FindByIdAndUserId(scheduleId, user.Id);

            if (existing == null)
            {
                throw new NoEntryFoundException(scheduleId, userId, typeof(BusySchedules).Name);
            }

            BusyScheduleRepository.Remove(existing);
            return(true);
        }
Ejemplo n.º 3
0
        private BusySchedules SolveOverlappedSchedules(BusySchedules currentSchedule, List <BusySchedules> schedules)
        {
            DateTime minDateTime = currentSchedule.BusyPeriodStartOn;
            DateTime maxDateTime = currentSchedule.BusyPeriodEndsOn;

            foreach (var sch in schedules)
            {
                if (sch.BusyPeriodStartOn.CompareTo(minDateTime) < 0)
                {
                    minDateTime = sch.BusyPeriodStartOn;
                }

                if (sch.BusyPeriodEndsOn.CompareTo(maxDateTime) > 0)
                {
                    maxDateTime = sch.BusyPeriodEndsOn;
                }
            }

            return(new BusySchedules {
                BusyPeriodStartOn = minDateTime,
                BusyPeriodEndsOn = maxDateTime
            });
        }
Ejemplo n.º 4
0
        public BusySchedules UpdateBusyInterval(int userId, int scheduleId, DateTime startOn, DateTime endsOn)
        {
            Users user = GetUserById(userId);

            if (user == null)
            {
                throw new NoEntryFoundException(userId, typeof(Users).Name);
            }

            if (startOn.CompareTo(endsOn) >= 0)
            {
                throw new InvalidPeriodRangeException(startOn, endsOn);
            }

            DateTime current = DateTime.UtcNow;

            if (endsOn.CompareTo(current) <= 0)
            {
                throw new CanNotAddPeriodsInThePastException(startOn, endsOn);
            }

            DateTime real = startOn.CompareTo(current) <= 0 ? current : startOn;

            List <BusySchedules> existingSchedules = BusyScheduleRepository.GetAllInPeriod(user.Id, real, endsOn);
            BusySchedules        existing          = null;

            if (existingSchedules.Count != 0)
            {
                existing = existingSchedules.Where(x => x.Id == scheduleId).SingleOrDefault();
            }

            if (existing == null)
            {
                existing = BusyScheduleRepository.FindByIdAndUserId(scheduleId, user.Id);
            }

            if (existing == null)
            {
                throw new NoEntryFoundException(scheduleId, userId, typeof(BusySchedules).Name);
            }

            existing.BusyPeriodStartOn = real;
            existing.BusyPeriodEndsOn  = endsOn;

            if (existingSchedules.Count == 0)
            {
                BusyScheduleRepository.Update(existing);
                return(existing);
            }

            BusySchedules newSchedule = SolveOverlappedSchedules(existing, existingSchedules);

            existing.BusyPeriodStartOn = newSchedule.BusyPeriodStartOn;
            existing.BusyPeriodEndsOn  = newSchedule.BusyPeriodEndsOn;
            BusyScheduleRepository.Update(existing);

            foreach (var sch in existingSchedules)
            {
                if (sch.Id != existing.Id)
                {
                    BusyScheduleRepository.Remove(sch);
                }
            }

            return(existing);
        }