/// <summary> /// Get paged list of all projects within the radius of a given point /// </summary> /// <param name="latitude">Latitude</param> /// <param name="longitude">Longitude</param> /// <param name="pageIndex">page index</param> /// <param name="pageSize">page size</param> /// <param name="categories">Selected categories</param> /// <param name="radius">Radius</param> /// <param name="start">Start</param> /// <param name="childFriendly">Child friendly</param> /// <returns>paged project list</returns> public IPaginatedList<Project> GetAllProjectsByCoordsPaged(decimal latitude, decimal longitude, int pageIndex = 0, int pageSize = -1, IList<int> categories = null, ProjectSearchRadius radius = ProjectSearchRadius.FiveMiles, ProjectSearchStart start = ProjectSearchStart.Whenever, ProjectChildFriendly childFriendly = ProjectChildFriendly.NoPreference) { // Get the base projects // This returns IEnumerable so I guess searching by co-ords requires var projectIds = GetAllProjectsByCoords(latitude, longitude, categories, radius, start, childFriendly).Select(b => b.Id); var query = from p in _projectRepository.Table where projectIds.Contains(p.Id) orderby 1 ascending // needs order to paginate select p; var projects = new PaginatedList<Project>(query, pageIndex, pageSize); return projects; }
/// <summary> /// Get all projects within the radius of a given point /// </summary> /// <param name="latitude">Latitude</param> /// <param name="longitude">Longitude</param> /// <param name="categories">Selected categories</param> /// <param name="radius">Radius</param> /// <param name="start">Start</param> /// <param name="childFriendly">Child friendly</param> /// <returns>Base project collection</returns> public IList<BaseProject> GetAllProjectsByCoords(decimal latitude, decimal longitude, IList<int> categories = null, ProjectSearchRadius radius = ProjectSearchRadius.FiveMiles, ProjectSearchStart start = ProjectSearchStart.Whenever, ProjectChildFriendly childFriendly = ProjectChildFriendly.NoPreference) { int radiusValue; switch (radius) { case ProjectSearchRadius.TenMiles: radiusValue = 10; break; case ProjectSearchRadius.FifteenMiles: radiusValue = 15; break; case ProjectSearchRadius.FiftyMiles: radiusValue = 50; break; case ProjectSearchRadius.AnyDistance: radiusValue = 0; break; default: radiusValue = 5; break; } var query = from p in GetAllCachedProjects().ToList() where radiusValue == 0 || _webHelper.Haversine((double)latitude, (double)longitude, (double)p.Latitude, (double)p.Longitude, DistanceType.Miles) <= radiusValue select p; if (categories != null && categories.Count > 0) query = query.Where(p => categories.All(x => p.Categories.Any(c => c.Id == x))); switch (start) { case ProjectSearchStart.TodayOrTomorrow: var tomorrow = DateTime.Now.AddDays(1); query = query.Where(p => p.StartDate <= new DateTime(tomorrow.Year, tomorrow.Month, tomorrow.Day, 23, 59, 59)).ToList(); break; case ProjectSearchStart.ThisWeek: var thisWeek = DateTime.Now.AddDays((int)DateTime.Now.DayOfWeek); query = query.Where(p => p.StartDate <= new DateTime(thisWeek.Year, thisWeek.Month, thisWeek.Day, 23, 59, 59)).ToList(); break; case ProjectSearchStart.ThisMonth: query = query.Where(p => p.StartDate <= new DateTime(DateTime.Now.Year, DateTime.Now.AddMonths(1).Month, 1, 23, 59, 59).AddDays(-1)).ToList(); break; } switch (childFriendly) { case ProjectChildFriendly.Friendly: query = query.Where(p => p.ChildFriendly).ToList(); break; case ProjectChildFriendly.UnFriendly: query = query.Where(p => !p.ChildFriendly).ToList(); break; } return query.ToList(); }