Example #1
0
        /// <summary>
        /// Tries to add given reservation to db, after validating it
        /// </summary>
        public ValidationResult Add(NewReservationItem newReservation)
        {
            if (newReservation == null)
            {
                throw new ArgumentNullException(nameof(newReservation));
            }

            var result = ValidateNewReservation(newReservation);

            if ((result & ValidationResult.Ok) == ValidationResult.Ok)
            {
                var reservation = new Reservation
                {
                    From     = newReservation.From,
                    To       = newReservation.To,
                    Lecturer = queryAllLecturers.Execute().Single(p => p.Id == newReservation.LecturerId),
                    Hall     = queryAllLectureHalls.Execute().Single(p => p.Number == newReservation.LectureHallNumber),
                };

                queryAdd.Execute(reservation);
            }

            return(result);
        }
Example #2
0
        /// <summary>
        /// Lists all lecturers that exist in db
        /// </summary>
        public IEnumerable <LecturerItem> All()
        {
            var lecturers = query.Execute().ToList();

            return(Mapper.Map <IEnumerable <LecturerItem> >(lecturers));
        }
 /// <summary>
 /// Lists all lecturers that exist in db
 /// </summary>
 public IEnumerable <LecturerItem> All()
 {
     return(Mapper.Map <IEnumerable <LecturerItem> >(_query.Execute().ToList()));
 }
 /// <summary>
 /// Lists all lecturers that exist in db
 /// </summary>
 public IEnumerable <LecturerItem> All()
 {
     return(Mapper.Map <IEnumerable <LecturerItem> >(_query.Execute().ToList().OrderBy(x => x.Title)));
 }
        /// <summary>
        /// Checks whether given reservation can be added.
        /// Performs logical and business validation.
        /// </summary>
        public ValidationResult ValidateNewReservation(NewReservationItem newReservation)
        {
            if (newReservation == null)
            {
                throw new ArgumentNullException("newReservation");
            }

            var result = ValidationResult.Default;

            if (newReservation.From.DayOfWeek != newReservation.To.DayOfWeek)
            {
                result |= ValidationResult.MoreThanOneDay;
            }

            if (newReservation.From > newReservation.To)
            {
                result |= ValidationResult.ToBeforeFrom;
            }

            if ((newReservation.From.Hour < 8) || (newReservation.From.Hour > 18) || (newReservation.From.Hour == 18 && newReservation.From.Minute != 0) ||
                (newReservation.To.Hour < 8) || (newReservation.To.Hour > 18) || (newReservation.To.Hour == 18 && newReservation.To.Minute != 0))
            {
                result |= ValidationResult.OutsideWorkingHours;
            }

            var secondsBetweenToAndFrom = (newReservation.To - newReservation.From).TotalSeconds;

            if (secondsBetweenToAndFrom > 10800) //10800 is 3 hours in seconds
            {
                result |= ValidationResult.TooLong;
            }

            var existingReservations = _queryAll.Execute();
            var listOfPossibleOverlappingReservations = existingReservations.Select(x => x).Where(x => x.Hall.Number == newReservation.LectureHallNumber);

            foreach (Reservation res in listOfPossibleOverlappingReservations.ToList())
            {
                if (res.From > newReservation.From && res.To <= newReservation.To)
                {
                    result |= ValidationResult.Conflicting;
                    break;
                }
                if (res.From < newReservation.From && res.To >= newReservation.To)
                {
                    result |= ValidationResult.Conflicting;
                    break;
                }
                if (res.From == newReservation.From && res.To == newReservation.To)
                {
                    result |= ValidationResult.Conflicting;
                    break;
                }
                if (res.From >= newReservation.From && res.To < newReservation.To)
                {
                    result |= ValidationResult.Conflicting;
                    break;
                }
                if (res.From <= newReservation.From && res.To > newReservation.To)
                {
                    result |= ValidationResult.Conflicting;
                    break;
                }
            }

            var lectureHalls = _queryAllLectureHalls.Execute();

            if (!lectureHalls.Any(x => x.Number == newReservation.LectureHallNumber))
            {
                result |= ValidationResult.HallDoesNotExist;
            }

            var lecturers = _queryAllLecturers.Execute();

            if (!lecturers.Any(x => x.Id == newReservation.LecturerId))
            {
                result |= ValidationResult.LecturerDoesNotExist;
            }

            // TODO
            // Note that for reservation dates, we take into account only date and an hour, minutes and seconds don't matter.

            return(result);
        }
Example #6
0
        /// <summary>
        /// Checks whether given reservation can be added.
        /// Performs logical and business validation.
        /// </summary>
        public ValidationResult ValidateNewReservation(NewReservationItem newReservation)
        {
            if (newReservation == null)
            {
                throw new ArgumentNullException("newReservation");
            }

            var result = ValidationResult.Default;

            // TODO
            // Implement following validation rules:
            // (idea is to check all and give customer full information, not to stop after first fail - therefore enum with [Flags] Attribute is returned)

            // - newReservation.From must be the same day as newReservation.To. If it isn't, set result |= ValidationResult.MoreThanOneDay
            if (newReservation.From.Day != newReservation.To.Day)
            {
                result |= ValidationResult.MoreThanOneDay;
            }

            // - newReservation.From obviously can't be >= newReservation.To. If it is, set result |= ValidationResult.ToBeforeFrom
            if (DateTime.Compare(newReservation.From, newReservation.To) >= 0)
            {
                result |= ValidationResult.ToBeforeFrom;
            }

            // - whole newReservation must be included inside working hours: 8-18 (it can't start before 8 and must finish at 18 at the very latest).
            //   If it's not met, set result |= ValidationResult.OutsideWorkingHours
            if (newReservation.From.TimeOfDay.Hours < 8 ||
                newReservation.To.TimeOfDay.Hours > 18)
            {
                result |= ValidationResult.OutsideWorkingHours;
            }

            // - newReservation must last 3 hours at most. If it's not, set result |= ValidationResult.TooLong
            if ((newReservation.To.TimeOfDay.Hours - newReservation.From.TimeOfDay.Hours) > 3)
            {
                result |= ValidationResult.TooLong;
            }

            // - newReservation obviously cannot be in conflict (same hallNumber and overlapping hours) with any existing reservation.
            //   If it is, set result |= ValidationResult.Conflicting. Use _queryAll to get all extisting reservations
            var reservationToCheck = _queryAll.Execute()
                                     .Where(r => r.Hall.Number == newReservation.LectureHallNumber &&
                                            (r.From.TimeOfDay >= newReservation.From.TimeOfDay &&
                                             r.To.TimeOfDay <= newReservation.To.TimeOfDay))
                                     .FirstOrDefault();

            if (reservationToCheck != null)
            {
                result |= ValidationResult.Conflicting;
            }

            // - check if newReservation.LectureHallNumber points at existing lecture hall. If it's not,
            //   set result |= ValidationResult.HallDoesNotExist. Use _queryAllLectureHalls to get all extisting lecture halls
            var lectureHallsToCheck = _queryAllLectureHalls.Execute()
                                      .Where(h => h.Number == newReservation.LectureHallNumber)
                                      .FirstOrDefault();

            if (lectureHallsToCheck == null)
            {
                result |= ValidationResult.HallDoesNotExist;
            }

            // - check if newReservation.LecturerId points at existing lecturer. If it's not,
            //   set result |= ValidationResult.LecturerDoesNotExist. Use _queryAllLecturers to get all extisting lecturers
            var lecturersTocheck = _queryAllLecturers.Execute()
                                   .Where(l => l.Id == newReservation.LecturerId)
                                   .FirstOrDefault();

            if (lecturersTocheck == null)
            {
                result |= ValidationResult.LecturerDoesNotExist;
            }

            // Remember ! Check ALL validation rules and set result with appropriate enum flag described above.
            // Note that for reservation dates, we take into account only date and an hour, minutes and seconds don't matter.

            if (result == ValidationResult.Default)
            {
                return(ValidationResult.Ok);
            }

            return(result);
        }