/// <summary> /// Gets the grid of days for current range. /// </summary> private IReadOnlyList <IReadOnlyList <CalendarDayVM> > GetMonthGrid(DateTime from, DateTime to, int month, IEnumerable <CalendarEventVM> events) { var curr = from; var weeks = new List <List <CalendarDayVM> >(); var cache = events.Where(x => x.Day != null) .GroupBy(x => x.Day.Value) .ToDictionary(x => x.Key, x => x.ToList()); while (curr < to) { var week = new List <CalendarDayVM>(); for (var i = 0; i < 7; i++) { var day = new CalendarDayVM { Day = curr.Day }; if (curr.Month == month) { day.IsActive = true; if (cache.TryGetValue(curr.Day, out var dayEvents)) { day.Events = dayEvents; } } week.Add(day); curr = curr.AddDays(1); } weeks.Add(week); } return(weeks); }
internal CalendarDayVM[] GetCalendarView() { //var date = "22.04.1988"; var date = new DateTime(2020, 01, 01); var reservations = context.Reservation.ToArray(); var result = new List <CalendarDayVM>(); for (int i = 0; i < DateTime.DaysInMonth(date.Year, date.Month); i++) { // we need to give the new date all the class properties: isFull, isWeekend, List<TimeSlot> . // the List is full of objects of a TimeSlot: with properties public DateTime Start, DateTime End, int Count // we can start with givin ALL DAYS same instances in the <TimeSlot> List! var now = date.AddDays(i); var day = new CalendarDayVM { StartDateTime = now, IsWeekend = (now.DayOfWeek == DayOfWeek.Saturday || now.DayOfWeek == DayOfWeek.Sunday), IsClosed = (now.DayOfWeek == DayOfWeek.Monday), CalendarTimeSlots = new List <CalendarTimeSlotVM>() }; if (!day.IsClosed) { if (day.IsWeekend) { day.StartDateTime = day.StartDateTime.AddHours(10); //day.EndDateTime = day.StartDateTime.AddHours(8); int openinghours = 8; //day.EndDateTime = day.EndDateTime.AddMinutes(30); //for (day.StartDateTime = day.StartDateTime; day.StartDateTime < day.EndDateTime; day.StartDateTime.AddMinutes(15)) //{ // var slot = new CalendarTimeSlotVM { StartDateTime = day.StartDateTime, EndDateTime = day.StartDateTime.AddHours(2) }; // day.CalendarTimeSlots.Add(slot); // // trigger a method that checks how many bookings there is for the day && specific TIMESLOT //} for (double t = 0; t < openinghours; t += 0.25) { var slot = new CalendarTimeSlotVM { StartDateTime = day.StartDateTime.AddHours(t) }; slot.IsFull = context.Reservation.Where(r => r.StartDateTime == slot.StartDateTime).Select(r => r.NumberOfPeople).Sum() > 4; day.CalendarTimeSlots.Add(slot); } } else { day.StartDateTime = day.StartDateTime.AddHours(12); //day.EndDateTime = day.StartDateTime.AddHours(6); int openinghours = 6; //day.EndDateTime = day.EndDateTime.AddMinutes(30); //for (day.StartDateTime = day.StartDateTime; day.StartDateTime < day.EndDateTime; day.StartDateTime.AddMinutes(15)) //{ // var slot = new CalendarTimeSlotVM { StartDateTime = day.StartDateTime, EndDateTime = day.StartDateTime.AddHours(2) }; // day.CalendarTimeSlots.Add(slot); //} for (int t = 0; t < openinghours; t++) { var slot = new CalendarTimeSlotVM { StartDateTime = day.StartDateTime.AddHours(t) }; day.CalendarTimeSlots.Add(slot); } } } // IF all timeSpaces(amountOfFullTimeSlots) were full => Day is full result.Add(day); } //return new CalendarViewVM {IsFull = context.Reservation.Any(o => o.Date == date)}; return(result.ToArray()); }