public void TestImpossibleReservationsDueToLackOfTimeSlots()
        {
            for (int i = 0; i < this.lanes.Count; i++)
            {
                var reservation = new Reservation()
                {
                    CreatedAt = DateTime.Now,
                    Name = "Gert",
                    NumberOfPlayers = 4,
                    PhoneNumber = 1234,
                    PlayAt = DateTime.Now
                };

                reservation.AddLane(this.lanes[i]);
                reservation.AddTimeSlot((from y in this.timeSlots select y).First<TimeSlot>());

                this._session.Save(reservation);
            }
            // since we filled out all lanes at that timeslot, the next booking for that timeslot should not be possible
            var service = new ReservationPossibleService();
            var request = new ReservationPossible()
            {
                Reservation = new ReservationType()
                {
                    HowManyHours = 2,
                    NumberOfPlayers = 8,
                    PlayAt = DateTime.Now,
                    TimeOfDay = (from y in this.timeSlots select y).First<TimeSlot>().Start
                }
            };

            ReservationPossibleResponse response = service.OnPost(request) as ReservationPossibleResponse;
            Assert.That(response.IsPossible, Is.False);
            Assert.That(response.Suggestions.Count, Is.EqualTo(2));
        }
Esempio n. 2
0
        public void TestGetReservationsForGivenDay()
        {
            for (int i = 0; i < this.lanes.Count-5; i++)
            {
                var reservation = new Reservation()
                {
                    CreatedAt = DateTime.Now,
                    Name = "Gert",
                    NumberOfPlayers = 4,
                    PhoneNumber = 1234,
                    PlayAt = DateTime.Now
                };

                reservation.AddLane(this.lanes[i]);
                reservation.AddTimeSlot((from y in this.timeSlots select y).First<TimeSlot>());

                this._session.Save(reservation);
            }

            var reservationService = new ReservationsService();
            var request = new Reservations {
                Date = DateTime.Now
            };

            ReservationsResponse response = reservationService.OnGet(request) as ReservationsResponse;
            Assert.That(response.ReservationList.Count, Is.EqualTo(5), "We expect that there are 5 reservations today");

            foreach (var r in response.ReservationList)
            {
                Assert.That(r, Is.Not.Null);
            }
        }
Esempio n. 3
0
        public void TestAddingSameTimeSlotReturnsFalse()
        {
            var slot1 = new TimeSlot { Start = TimeSpan.FromHours(10), End = TimeSpan.FromHours(11) };
            var slot2 = new TimeSlot { Start = TimeSpan.FromHours(11), End = TimeSpan.FromHours(12) };
            this._session.Save(slot1);
            this._session.Save(slot2);

            Reservation resv = new Reservation();
            Assert.That(resv.AddTimeSlot(slot1), Is.True);
            Assert.That(resv.AddTimeSlot(slot2), Is.True);
            Assert.That(resv.AddTimeSlot(slot2), Is.False);
        }
Esempio n. 4
0
        public void TestAddingSameLaneReturnsFalse()
        {
            var lane1 = new Lane { Name = "42" };
            var lane2 = new Lane { Name = "45" };
            this._session.Save(lane1);
            this._session.Save(lane2);

            Reservation resv = new Reservation();

            Assert.That(resv.AddLane(lane1), Is.True);
            Assert.That(resv.AddLane(lane2), Is.True);
            Assert.That(resv.AddLane(lane1), Is.False);
        }
        public List<Reservation> Convert(LaneSchedulerState state, out Reservation newReservation)
        {
            int [,] internalState = state.State;
            int current = 0;
            newReservation = new Reservation();
            int laneCount = this.laneRepos.GetAll().Count();
            int timeSlotCount = this.timeSlotRepos.GetAll().Count();

            List<ReservationLaneTimeSlotPair> pairs = new List<ReservationLaneTimeSlotPair>();

            for (int i = 0; i < timeSlotCount; i++)
            {
                for (int j = 0; j < laneCount; j++)
                {
                    current = internalState[i, j];
                    if (current == 0)
                    {
                        continue;
                    }
                    var pair = new ReservationLaneTimeSlotPair()
                    {
                        LaneId = j+1,
                        TimeSlotId = i+1,
                        ReservationId = current
                    };

                    pairs.Add(pair);
                }
            }

            // load all reservations besides -1 by first gettting the distinct list of
            // ids, then loading these from db and converting to a dictionary
            // then finally clearing all lane and timeslots from these
            var distinctReservations = (from y in pairs
                                            where y.ReservationId != -1
                                            select y.ReservationId).Distinct<int>().ToList();
            var distinctTimeSlots = (from y in pairs
                                     select y.TimeSlotId).Distinct<int>().ToList();
            var distinctLanes = (from y in pairs
                                 select y.LaneId).Distinct<int>().ToList();

            var reservations = (from y in reservationRepos.GetAll()
                                where distinctReservations.Contains(y.Id)
                                select y).ToDictionary(t => t.Id, t => t);

            var timeslots = (from y in timeSlotRepos.GetAll()
                             where distinctTimeSlots.Contains(y.Id)
                             select y).ToDictionary(t => t.Id, t => t);

            var lanes = (from y in laneRepos.GetAll()
                         where distinctLanes.Contains(y.Id)
                         select y).ToDictionary(t => t.Id, t => t);

            foreach (var r in reservations)
            {
                r.Value.Lanes.Clear();
                r.Value.TimeSlots.Clear();
            }

            // now we can reschedule all of the reservations, and add the lanes and timeslots
            // to the new reservation as well
            foreach (var pair in pairs)
            {
                if (pair.ReservationId == -1)
                {
                    newReservation.AddLane(lanes[pair.LaneId]);
                    newReservation.AddTimeSlot(timeslots[pair.TimeSlotId]);
                    continue;
                }

                reservations[pair.ReservationId].AddLane(lanes[pair.LaneId]);
                reservations[pair.ReservationId].AddTimeSlot(timeslots[pair.TimeSlotId]);

            }

            var toReturn = new List<Reservation>();
            foreach(var r in reservations)
            {
                toReturn.Add(r.Value);
            }
            distinctLanes = null;
            distinctReservations = null;
            distinctTimeSlots = null;

            return toReturn;
        }
        /// <summary>
        /// Determines whether the new reservation <paramref name="reservation"/> is possible
        /// based on the current reservation schedule for the day of that reservations
        /// </summary>
        /// <remarks>
        /// The lane scheduler will be queried to check for availablility. While doing this,
        /// all current reservations for that day might be rescheduled, therefore, the new schedule
        /// is returned. This schedule will have to be persisted if the new reservation is acuallly
        /// added to the database.
        /// 
        /// The out paramter "newReservation" will contain an reservation instance ready for persisting
        /// if this is sought.
        /// 
        /// Execptions will be thrown if the reservation does not fit in the current schedule
        /// </remarks>
        /// <param name="reservation">The new reservation</param>
        /// <param name="theReservation">An intance of a reservation, based on the data from <paramref name="reservation"/></param>
        /// <returns>The list of rescheduled reservations and an instance ready to commit in <paramref name="theReservation"/></returns>
        /// <exception cref="ArgumentException">If the <paramref name="reservation"/> is not possible</exception>
        public List<Reservation> Go(ReservationType reservation, out Reservation theReservation, out bool isPossible, out List<LaneSchedulerReservation> suggestions)
        {
            // quick pruning of the result - does the requested timeslot exist at all??
            TimeSlot startTimeSlot = timeSlotRepos.GetAll().FindTimeSlotStartingAt(reservation.TimeOfDay);
            if (startTimeSlot == null)
            {
                throw new ArgumentException("Reservation is not possible as the timeslot does not exist", "timeSlot");
            }

            var reservations = reservationRepos.GetAll().FindReservationsByDate(reservation.PlayAt);

            // convert these into a format the scheduler understands...
            List<LaneSchedulerReservation> schedulerReservations;
            var convertToState = ServiceLocator.Current.GetInstance<ReservationsToLaneSchedulerState>();
            var schedulerState = convertToState.Convert(reservations.ToList(), out schedulerReservations);

            LaneSchedulerReservation newReservation = new LaneSchedulerReservation()
            {
                Id = -1,
                NumberOfLanes = (int)Math.Ceiling(reservation.NumberOfPlayers / 6.0m),
                NumberOfTimeSlots = reservation.HowManyHours,
                StartTimeSlot = startTimeSlot.Id - 1
            };

            // do the scheduling
            var newState = LaneScheduler.Search(schedulerState, schedulerReservations, newReservation);

            if (newState.state == null)
            {

                suggestions = newState.reservations;
                isPossible = false;
                theReservation = null;
                return null;

            }
            suggestions = null;
            // The scheduler attempts to place the reservation +/- one time slot in the schedule if it's full,
            // so we have to try and handle that
            var convertToReservation = ServiceLocator.Current.GetInstance<LaneSchedulerStateToReservations>();
            var rescheduledReservations = convertToReservation.Convert(newState.state, out theReservation);
            theReservation.NumberOfPlayers = reservation.NumberOfPlayers;
            theReservation.PlayAt = reservation.PlayAt;
            theReservation.Name = reservation.Name;
            theReservation.PhoneNumber = reservation.PhoneNumber;
            theReservation.Status = ReservationStatus.Pending;
            theReservation.CreatedAt = DateTime.Now;

            // now that we have the actual reservation, we can check
            // the added time-slots against the requested timeslot
            if (theReservation.TimeSlots[0].Start != startTimeSlot.Start)
            {
                isPossible = false;
            }
            else
            {
                isPossible = true;
            }

            return rescheduledReservations;
        }
        public void TestValidResrvationWithSomeOtherReservations()
        {
            var reservation = new Reservation()
            {
                CreatedAt = DateTime.Now,
                Name = "Gert",
                NumberOfPlayers = 4,
                PhoneNumber = 1234,
                PlayAt =DateTime.Now
            };

            reservation.AddLane((from y in this.lanes select y).First<Lane>());
            reservation.AddTimeSlot((from y in this.timeSlots select y).First<TimeSlot>());

            this._session.Save(reservation);

            var service = new ReservationPossibleService();

            var request = new ReservationPossible()
            {
                Reservation = new ReservationType()
                {
                    HowManyHours = 2,
                    NumberOfPlayers = 8,
                    PlayAt = DateTime.Now,
                    TimeOfDay = (from y in this.timeSlots select y).First<TimeSlot>().Start
                }
            };

            ReservationPossibleResponse response = service.OnPost(request) as ReservationPossibleResponse;
            Assert.That(response.IsPossible, Is.True);
        }