예제 #1
0
        private static Event[] GetEvents()
        {
            string sql = "select id, date, dayPart, label from Events";

            Event[] events = GetItems <Event>(sql, (SqlDataReader reader) =>
            {
                Event newEvent = new Event();

                newEvent.ID = reader["id"].ToString();

                newEvent.DayPart = DayPartSelection.GetDayPart(reader["dayPart"].ToString());

                newEvent.Date = GetDateFromDateString(reader["date"].ToString());

                newEvent.Label = reader["label"].ToString();

                return(newEvent);
            });

            return(events);
        }
예제 #2
0
        GetBikesAvailability(InventoryGroup[] inventoryGroups,
                             string name,
                             string email,
                             string phone,
                             DateTime startDate,
                             DateTime endDate,
                             DateSelection dateSelection)
        {
            BikesAvailability bikesAvailability
                = new BikesAvailability(
                      dateSelection,
                      name,
                      email,
                      phone);


            List <DisplayTime> availableDates = new List <DisplayTime>();

            DateTime runningDate = new DateTime(startDate.Ticks);

            DateTime toDate = new DateTime(endDate.Ticks);

            bikesAvailability.Inventory = inventoryGroups?.ToArray() ?? GetInventory();

            Appointment[] appointments = DatabaseOperations.GetAppointments();

            while (runningDate <= toDate)
            {
                List <DayPartSelection.DayPart> availableDayParts =
                    new List <DayPartSelection.DayPart>(DayPartSelection.GetAvailableDayParts(runningDate));

                foreach (DayPartSelection.DayPart dayPart in DayPartSelection.AllDayParts)
                {
                    Appointment[] appointmentsTodayAndDayPart =
                        appointments.Where(appointment => appointment.Date.Ticks == runningDate.Ticks &&
                                           appointment.DayPartOverlaps(dayPart))
                        .ToArray();

                    BikeBooking[] bikeBookings
                        = appointmentsTodayAndDayPart.SelectMany(a => a.BikeBookings)
                          .ToArray();

                    InventoryGroup[] newInventoryGroups = bikesAvailability.Inventory.Select(i => new InventoryGroup(i)).ToArray();

                    foreach (InventoryGroup inventoryGroup in newInventoryGroups)
                    {
                        BikeBooking[] overlappingBikeBookings =
                            bikeBookings.Where(b => b.BikeId == inventoryGroup.BikeId &&
                                               b.ModelId == inventoryGroup.ModelId).ToArray();

                        foreach (BikeBooking overlappingBikeBooking in overlappingBikeBookings)
                        {
                            inventoryGroup.Available--;

                            if (inventoryGroup.Wanted > inventoryGroup.Available)
                            {
                                availableDayParts.Remove(dayPart);
                            }
                        }
                    }
                }
                availableDates.Add(new DisplayTime(runningDate, availableDayParts.ToArray()));

                runningDate = runningDate.AddDays(1);
            }

            bikesAvailability.AvailableDates = availableDates.ToArray();

            if (availableDates.Any() == false)
            {
                bikesAvailability.Message = $"The bikes you want are not available in the timeframe you specified";
            }

            Event[] plannedEvents = GetEvents();

            bikesAvailability.BillingCost = new BillingCost();

            if (!(dateSelection == null || plannedEvents.Any(e => e.Date.Ticks == dateSelection.CSharpDayTime.Ticks)))
            {
                bikesAvailability.BillingCost
                    = GetPriceEstimateRental(email, bikesAvailability.Inventory, dateSelection);
            }

            return(bikesAvailability);
        }