private ExtracurEventsContract GetMonthEvents(XtracurDivision division, DateTime?month = null) { var firstDayOfCurrentMonth = DateTimeHelper.GetFirstDayOfCurrentMonth(); var firstDayOfChosenMonth = DateTimeHelper.GetFirstDayOfMonth(month); var firstDayOfTheNextMonth = DateTimeHelper.GetFirstDayOfTheNextMonth(firstDayOfChosenMonth); var firstDayOfThePreviousMonth = DateTimeHelper.GetFirstDayOfThePreviousMonth(firstDayOfChosenMonth); using (var repository = new XtracurAppointmentsRepository(division, firstDayOfChosenMonth, firstDayOfTheNextMonth)) { var eventsByKind = repository .GetAppointments() .Where(a => a.IsPublicMaster) .Select(extracurEventMapper.Map) .OrderBy(c => c.Start) .OrderBy(c => c.OrderIndex) .GroupBy(c => c.SubkindDisplayName) .Select(g => new ExtracurEventsContract.EventsByKind { Kind = g.Key, Events = g.AsEnumerable() }) .ToList(); return(new ExtracurEventsContract { Alias = division.Alias, TimetableTitle = division.GetNameByLanguage(language), IsCurrentMonthReferenceAvailable = firstDayOfCurrentMonth != firstDayOfChosenMonth, ChosenMonthDisplayText = firstDayOfChosenMonth.ToString("MMMM yyyy"), PreviousMonthDisplayText = $"« {firstDayOfThePreviousMonth.ToString("MMMM yyyy")}", PreviousMonthDate = DateTimeHelper.GetDateStringForWeb(firstDayOfThePreviousMonth), NextMonthDisplayText = $"{firstDayOfTheNextMonth.ToString("MMMM yyyy")} »", NextMonthDate = DateTimeHelper.GetDateStringForWeb(firstDayOfTheNextMonth), KindsEvents = eventsByKind, ShowGroupingCaptions = eventsByKind.Count() > 1, HasEventsToShow = eventsByKind.Any(eg => eg.Events.Any(e => !e.IsShowImmediateHidden)) }); } }
private ExtracurEventsContract GetWeekEvents(XtracurDivision division, DateTime?fromDate = null) { var weekFromDate = DateTimeHelper.GetWeekStart(fromDate); var weekToDate = weekFromDate.AddDays(7); var previousWeekFromDate = weekFromDate.AddDays(-7); var nextWeekFromDate = weekFromDate.AddDays(7); var currentWeekFromDate = DateTimeHelper.GetWeekStart(DateTime.Today); using (var repository = new XtracurAppointmentsRepository(division, weekFromDate, weekToDate)) { var events = repository .GetAppointments() .Where(a => a.IsPublicMaster) .Select(extracurEventMapper.Map) .OrderBy(c => c.Start) .ToList(); var eventsDays = events .Where(e => e.Start >= weekFromDate) .GroupBy(e => e.Start.Date) .OrderBy(g => g.Key) .Select(g => { var day = g.Key; var dayEvents = g.AsEnumerable(); return(new ExtracurEventsContract.EventsDay { Day = day, DayString = (language == LanguageCode.English) ? day.ToString("dddd, MMMM d") : day.ToString("dddd, d MMMM"), DayEvents = hideRepeatedTimeFromEvents(dayEvents) }); }) .ToList(); return(new ExtracurEventsContract { Alias = division.Alias, TimetableTitle = division.GetNameByLanguage(language), EarlierEvents = events.Where(e => e.Start < weekFromDate), EventsDays = eventsDays, WeekDisplayText = DateTimeHelper.GetWeekDisplayText(language, weekFromDate, weekToDate), PreviousWeekMonday = DateTimeHelper.GetDateStringForWeb(previousWeekFromDate), WeekMonday = DateTimeHelper.GetDateStringForWeb(weekFromDate), NextWeekMonday = DateTimeHelper.GetDateStringForWeb(nextWeekFromDate), IsCurrentWeekReferenceAvailable = (currentWeekFromDate != weekFromDate), HasEventsToShow = events.Any(e => !e.IsShowImmediateHidden), IsPreviousWeekReferenceAvailable = true, IsNextWeekReferenceAvailable = true }); /// <summary> /// Returns events with <see cref="ExtracurEventContract.HasTheSameTimeAsPreviousItem"/> and /// <see cref="ExtracurEventContract.DisplayDateAndTimeIntervalString"/> set /// </summary> /// <param name="dayEvents"></param> /// <returns></returns> IEnumerable <ExtracurEventContract> hideRepeatedTimeFromEvents( IEnumerable <ExtracurEventContract> dayEvents) { // day events ordered by subject in time interval var dayEventsOrdered = dayEvents .GroupBy(de => de.TimeIntervalString) .Select(g => g.OrderBy(e => e.Subject)) .SelectMany(g => g.AsEnumerable()) .ToList(); var eventWithTimeShown = dayEventsOrdered[0]; for (int i = 1; i < dayEventsOrdered.Count; i++) { var @event = dayEventsOrdered[i]; if (@event.TimeIntervalString == eventWithTimeShown.TimeIntervalString) { @event.HasTheSameTimeAsPreviousItem = true; @event.DisplayDateAndTimeIntervalString = @event.WithinTheSameDay ? @event.HasTheSameTimeAsPreviousItem ? string.Empty : @event.TimeIntervalString : @event.DateWithTimeIntervalString; } else { eventWithTimeShown = @event; } } return(dayEventsOrdered); } } }