public ActionResult AdvancedSearch(SearchModel searchModel, int pageNum = 1) { IEnumerable<EventModel> model = new List<EventModel>(); PageNum = pageNum; model = store.GetFilteredEvents(searchModel, EventsNumOnPage, (pageNum - 1)*EventsNumOnPage); foreach (var el in model) { el.MainPhoto = new PhotoManager().GetImage(el.MainPhoto, Server.MapPath("~"), true); } ViewBag.Page = PageNum; if (searchModel.Filter) { return PartialView("FilteredSearch", model); } var types = new Dictionary<string, bool>(); var allTypes = store.GetAllEventTypes().Select(t => t.Type); foreach (var type in allTypes) { types.Add(type, searchModel.Types != null ? searchModel.Types.Contains(type):false); } ViewBag.PageCount = store.GetEventsCount() / EventsNumOnPage; ViewBag.Types = types; ViewBag.Categories = store.GetAllPersonCategories().Select(c => c.Category); return View(model); }
public void AddingFilter(SearchModel searchModel, ref IEnumerable<EventModel> model) { if (searchModel.IsFree) { model = model.Where(e => e.isFreeEntrance == true); } if (searchModel.IsCharitable) { model = model.Where(e => e.IsCharitable == true); } if (searchModel.Types != null) { model = model.Where(e => searchModel.Types.Contains(e.Type.Type)); } if (searchModel.Categories != null) { model = model.Where(e => searchModel.Categories.Contains(e.PersonsCategory.Category)); } }
public void TimeFilter(SearchModel searchModel, ref IEnumerable<EventModel> model) { if (!string.IsNullOrEmpty(searchModel.FromTime)) { try { DateTime from = Convert.ToDateTime(searchModel.FromTime); model = model.Where(e => e.StartTime >= from); } catch (Exception exc) { } } if (!string.IsNullOrEmpty(searchModel.ToTime)) { try { DateTime to = Convert.ToDateTime(searchModel.ToTime); model = model.Where(e => e.StartTime <= to); } catch (Exception exc) { } } }
public void TextFilter(SearchModel searchModel, ref IEnumerable<EventModel> model) { if (!string.IsNullOrEmpty(searchModel.Text)) { var text = searchModel.Text.ToLower(); if (searchModel.OnlyInTitles) { model = model.Where(e => e.Title.ToLower().Contains(text)); } else { model = model.Where(e => e.Title.ToLower().Contains(text) || e.Description.ToLower().Contains(text) || e.ShortDescription.ToLower().Contains(text) || e.Organizers.ToLower().Contains(text) || e.Place.ToLower().Contains(text) || e.Publisher.Name.ToLower().Contains(text) || e.Publisher.LastName.ToLower().Contains(text) || (!string.IsNullOrEmpty(e.Enter) && e.Enter.ToLower().Contains(text)) || (!string.IsNullOrEmpty(e.Sponsors) && e.Sponsors.ToLower().Contains(text))); } } }
public void PlaceFilter(SearchModel searchModel, ref IEnumerable<EventModel> model) { if (!string.IsNullOrWhiteSpace(searchModel.Country)) { model = model.Where(e => e.City.Region.Country.Country == searchModel.Country); } if (!string.IsNullOrWhiteSpace(searchModel.Region)) { model = model.Where(e => e.City.Region.RegionName == searchModel.Region); } if (!string.IsNullOrWhiteSpace(searchModel.City)) { model = model.Where(e => e.City.CityName == searchModel.City); } }
/// <summary> /// /// </summary> /// <param name="count"></param> /// <param name="skip"></param> /// <param name="model"></param> /// <returns></returns> public List<EventModel> GetFilteredEvents( SearchModel model=null, int count=-1, int skip=-1) { using (var ctx = new EventContext()) { IEnumerable<EventModel> res = ctx.Events.Include("Type") .Include("PersonsCategory") .Include("Publisher") .Include("City") .Include("City.Region") .Include("City.Region.Country") .Include("Likes") .Include("Dislikes") .OrderByDescending(e => e.Id); if (count != -1) { res = res.Skip(skip).Take(count); } if (model != null) { PlaceFilter(model, ref res); TimeFilter(model, ref res); AddingFilter(model, ref res); TextFilter(model, ref res); } return res.ToList(); } }