/// <summary> /// Searches buildings to match criterias. /// </summary> /// <param name="model">The Search model.</param> /// <returns>The result</returns> public ActionResult SearchBuilding(SearchViewModel model) { System.Diagnostics.Debug.WriteLine(model.Name); int[] facilitiesIds = model.Facilities.Concat(model.TopFacilities).Where(f => f.Selected) .Select(f => f.FacilityTypeId).ToArray(); using (var db = new Models.OnLeaveContext()) { var result = db.UtilityBuildings .Include(b => b.UtilityBuildingPhotoDetails) .Include(b => b.Periods) .Include(b => b.Periods.Select(p => p.RoomAmounts)) .Include(b => b.UtilityBuildingLocales) .Where(b => (model.Name == null || model.Name.Trim().Length == 0 || b.UtilityBuildingLocales.Any(l => l.Name.ToLower().Contains(model.Name.ToLower()))) && b.UtilityBuildingPhotoDetails.Count > 0) .Where(b => !model.CityId.HasValue || b.CityId == model.CityId) .Where(b => !model.UtilityBuildingTypeId.HasValue || b.UtilityBuildingTypeId == model.UtilityBuildingTypeId) // UtilityBuildingTypeId .Where(b => facilitiesIds.All(fID => b.UtilityBuidingFacilityDetails.Any(ft => ft.UtilityBuildingFacilityTypeId == fID))) .Where(b => !model.Rating.HasValue || b.Rating == model.Rating) .Where(b => (!model.MinAmount.HasValue && !model.MaxAmount.HasValue) || b.Periods.SelectMany(p => p.RoomAmounts).Any(a => (!model.MinAmount.HasValue || model.MinAmount <= a.Amount) && (!model.MaxAmount.HasValue || model.MaxAmount >= a.Amount))) .OrderByDescending(b => b.SearchRating) .Take(51) .ToArray(); var buildings = result .Select(b => new UtilityBuildingModel { Id = b.UtilityBuildingId, Name = b.UtilityBuildingLocales.Where(l => l.LocaleId == (int)LocaleTypes.BG).Select(l => l.Name).FirstOrDefault(), Description = b.UtilityBuildingLocales.Where(l => l.LocaleId == (int)LocaleTypes.BG).Select(l => l.Description).FirstOrDefault(), UrlAddress = b.ExternalUrl, Rating = b.Rating ?? 0, SystemTypeId = b.SystemTypeId, Size = b.Size, PhotoIds = new System.Collections.Generic.List<int>(){ b.UtilityBuildingPhotoDetails.First().PhotoId }, Periods = b.Periods.OrderBy(p => p.RoomAmounts.Min(a => a.Amount)).Take(1).ToList() }).ToArray(); return PartialView("_SearchResult", buildings); } }
/// <summary> /// Renders Search Section. /// </summary> /// <returns></returns> public ActionResult Search() { var searchModel = new SearchViewModel(); var topFacilities = new int[] { (int)Common.Constant.FacilityTypes.WI_FI, (int)Common.Constant.FacilityTypes.SWIMMING_POOL, (int)Common.Constant.FacilityTypes.SPA, (int)Common.Constant.FacilityTypes.BREAKFASET }; using (var db = new Models.OnLeaveContext()) { searchModel.TopFacilities = db.UtilityBuildingFacilityTypes .Where(f => topFacilities.Any(tf => f.UtilityBuildingFacilityTypeId == tf)) .Select(ft => new FacilityTypeModel { FacilityTypeId = ft.UtilityBuildingFacilityTypeId, FacilityTypeName = ft.Name }).ToArray(); searchModel.Facilities = db.UtilityBuildingFacilityTypes .Where(f => !topFacilities.Any(tf => f.UtilityBuildingFacilityTypeId == tf)) .Select(ft => new FacilityTypeModel { FacilityTypeId = ft.UtilityBuildingFacilityTypeId, FacilityTypeName = ft.Name }).ToArray(); } ViewBag.Cities = StaticDataProvider.Cities.Select(c => new SelectListItem { Text = c.Name, Value = c.CityId.ToString() }); ViewBag.UtilityBuildingTypes = StaticDataProvider.UtilityBuildingTypes.Select(t => new SelectListItem { Text = t.Description, Value = t.UtilityBuildingTypeId.ToString() }); return PartialView("_SearchSection", searchModel); }