public ActionResult Index() { var eventsToSkip = eventsRepository.GetAll().Count() - Const.EventsOnMainPage; var eventsEnum = eventsRepository.Where(ev => ev.Date >= DateTime.Today).Count() < Const.EventsOnMainPage ? eventsRepository.GetAll().Skip(eventsToSkip).OrderBy(ev => ev.Date).Take(Const.EventsOnMainPage) : eventsRepository.Where(ev => ev.Date >= DateTime.Today).OrderBy(ev => ev.Date).Take(Const.EventsOnMainPage); var eventsList = eventsEnum.Select(ev => new EventViewModel { Id = ev.Id, Day = String.Format("{0} {1}", ev.Date.Day, DateTimeUtils.GetMonthNameGenitive(ev.Date)), Title = ev.Title, Text = ev.Text, Location = ev.Location, Date = ev.Date.ToString("dd.MM.yyyy")}).ToList(); var model = new MainPageViewModel() { Articles = articlesRepository.GetAll().OrderByDescending(art => art.PostedAt).Take(4) .Select(art => new ArticleViewModel() { Id = art.Id, Title = art.Title, Text = art.Text, ArticleImagePath = art.File.FileName}).ToList(), Slides = slideshowRepository.GetAll().OrderByDescending(slide => slide.Date).Take(5) .Select(slide => new SlideViewModel() { Id = slide.Id, Title = slide.Title, Text = slide.Text, ImagePath = slide.File.FileName}).ToList(), Events = eventsList, GalleryItems = galleryRepository.GetAll().OrderByDescending(gal => gal.PostedAt).Take(10) .Select(gal => new GalleryItemViewModel() { AlbumId = gal.AlbumId, ThumbnailPath = gal.Thumbnail.FileName}).ToList() }; return View(model); }
public ActionResult Search(string query) { var threeDaysAgo = DateTime.Today.Subtract(new TimeSpan(3, 0, 0, 0)); var containsInTitle = articlesRepository.Where(article => article.Title.ToLower().Contains(query.ToLower())).OrderByDescending(art => art.PostedAt); var containsInText = articlesRepository.Where(article => article.Text.ToLower().Contains(query.ToLower())).OrderByDescending(art => art.PostedAt); var relevantArticles = containsInText.Union(containsInTitle).OrderByDescending(article => article.PostedAt).Take(10) .Select(art => new ArticleViewModel() { Id = art.Id, Title = art.Title, Text = art.Text, ArticleImagePath = art.File.FileName }).ToList(); var eventsInTitle = eventsRepository.Where(ev => ev.Title.ToLower().Contains(query.ToLower()) && ev.Date > threeDaysAgo).OrderBy(ev => ev.Date); var eventsInText = eventsRepository.Where(ev => ev.Text.ToLower().Contains(query.ToLower()) && ev.Date > threeDaysAgo).OrderBy(ev => ev.Date); var relevantEvents = eventsInTitle.Union(eventsInText).OrderBy(ev => ev.Date).Take(20).Select(ev => new EventViewModel { Id = ev.Id, Day = String.Format("{0} {1}", ev.Date.Day, DateTimeUtils.GetMonthNameGenitive(ev.Date)), Title = ev.Title, Text = ev.Text, Location = ev.Location, Date = ev.Date.ToString("dd.MM.yyyy") }).ToList(); var lecturersInName = lecturersRepository.Where(l => l.Name.ToLower().Contains(query.ToLower())); var lecturersinText = lecturersRepository.Where(l => l.Description.ToLower().Contains(query.ToLower())); var relevantLecturers = lecturersInName.Union(lecturersinText).Take(25).Select(l => new LecturerViewModel() { Id = l.Id, Name = l.Name, Post = l.Profile.Post }).ToList(); var result = new MainPageViewModel() { Articles = relevantArticles, Events = relevantEvents, Lecturers = relevantLecturers }; ViewBag.IsAnythingFound = relevantArticles.Any() || relevantEvents.Any() || relevantLecturers.Any(); ViewBag.Query = query; return View(result); }