public static XtracurEventsSearchViewModel Build(XtracurDivision xtracurDivision, string query, int?offset, bool?showPast) { LanguageCode language = CultureHelper.CurrentLanguage; bool isEmptyQuery = String.IsNullOrWhiteSpace(query); int totalCount; var today = DateTime.Today; IEnumerable <XtracurEventItemViewModel> events = Enumerable.Empty <XtracurEventItemViewModel>(); if (!showPast.HasValue && !offset.HasValue) { var upcomingEvents = GetEvents(xtracurDivision, query, 0, today, DateTimeHelper.MaxValue, out totalCount); if (upcomingEvents.Any()) { events = upcomingEvents; showPast = false; } else { var pastEvents = GetEvents(xtracurDivision, query, 0, DateTimeHelper.MinValue, today.AddDays(1), out totalCount); if (pastEvents.Any()) { events = pastEvents; showPast = true; } } } else { var fromDate = showPast.Value ? DateTimeHelper.MinValue : today; var toDate = showPast.Value ? today.AddDays(1) : DateTimeHelper.MaxValue; events = GetEvents(xtracurDivision, query, offset ?? 0, fromDate, toDate, out totalCount); } IEnumerable <XtracurEventsSearchPagerItemViewModel> pagerItems = GeneratePagerItems(offset ?? 0, (totalCount - 1) / ItemsOnPageCount + 1); return(new XtracurEventsSearchViewModel { Alias = xtracurDivision.Alias, Title = xtracurDivision.GetNameByLanguage(language), Events = events, PagerItems = pagerItems, ShowPager = pagerItems.Any(pi => pi.IsEnabled && !pi.IsActive), ShowPast = showPast ?? false, ShowUpcoming = showPast.HasValue ? !(showPast ?? false) : false, IsEmptyQuery = isEmptyQuery, Offset = offset ?? 0, QueryDisplayText = query, Breadcrumb = new Breadcrumb() { BreadcrumbHelper.GetBreadcrumbRootItem(false), BreadcrumbHelper.GetBreadcrumbXtracurEventsItem(xtracurDivision, false), BreadcrumbHelper.GetBreadcrumbXtracurSearchItem(true) } }); }