public async Task GetProperties() { CurrentPage = 0; var requestParams = new PropertyListRequest { CurrentPage = CurrentPage, PageSize = PageSize, NameFilter = NameFilter, CityFilter = CityFilter, StateFilter = StateFilter, SortColumn = SortColumn, SortAscending = SortAscending, UserIdListFilter = UserIdListFilter, }; var result = await GetPropertiesAsync(requestParams); if(result != null) { Properties.Clear (); foreach (var p in result.Properties) { Properties.Add(p); } LastResult = result; TotalPages = LastResult.TotalPages; OnPropertyListUpdated (result); } }
// #future: If we want to build on this query - make return type IQueryable<> private IQueryable<Entity.Models.Property> PropertiesQuery(PropertyListRequest requestParams = null) { var query = db.Properties.Where(p => p.CompanyId == loggedInUser.CompanyId); /* loggedInUser.Properties.AsQueryable() */ if (requestParams != null) { if (requestParams.UserIdListFilter.Count > 0) { query = query.Where(p => p.Users.Count(u => requestParams.UserIdListFilter.Contains(u.Id)) > 0); } // First apply search filter if (!String.IsNullOrEmpty(requestParams.StateFilter)) { var stateFilter = requestParams.StateFilter.Trim().ToLower(); query = query.Where(p => (!String.IsNullOrEmpty(p.StateProvince) && p.StateProvince.ToLower().Contains(stateFilter))); } if (!String.IsNullOrEmpty(requestParams.CityFilter)) { var cityFilter = requestParams.CityFilter.Trim().ToLower(); query = query.Where(p => (!String.IsNullOrEmpty(p.City) && p.City.ToLower().Contains(cityFilter))); } if (!String.IsNullOrEmpty(requestParams.NameFilter)) { var nameFilter = requestParams.NameFilter.Trim().ToLower(); query = query.Where(p => (!String.IsNullOrEmpty(p.Name) && p.Name.ToLower().Contains(nameFilter))); } // Second apply sorting if (!String.IsNullOrEmpty(requestParams.SortColumn)) { bool asc = requestParams.SortAscending; switch (requestParams.SortColumn) { case PropertyListRequest.NameColumn: query = asc ? query.OrderBy(p => p.Name) : query.OrderByDescending(p => p.Name); break; case PropertyListRequest.CityColumn: query = asc ? query.OrderBy(p => p.City) : query.OrderByDescending(p => p.City); break; case PropertyListRequest.StateColumn: query = asc ? query.OrderBy(p => p.StateProvince) : query.OrderByDescending(p => p.StateProvince); break; } } else { // Skip Needs orderby clause query = query.OrderBy(p => p.Name); } } return query; }
private PaginatedPropertyList GetPaginatedPropertyList(PropertyListRequest requestParams) { var query = PropertiesQuery(requestParams); var totalItems = query.Count(); // if no limit for page size, then page size will be equal to total items so we return all results. var pageSize = requestParams.PageSize != PropertyListRequest.NoLimitForPageSize ? requestParams.PageSize : totalItems; query = query.Skip(requestParams.CurrentPage * pageSize).Take(pageSize); var propertyDtoList = Mapper.Map<IQueryable<Entity.Models.Property>, List<Dto.Models.Property>>(query); GenerateUserPhotoLinks(propertyDtoList); // If requestParams is not empty, we should have received page info. var paginatedList = new PaginatedPropertyList { Properties = propertyDtoList, CurrentPage = requestParams.CurrentPage, PageSize = pageSize, TotalPages = (int)Math.Ceiling((double)totalItems / pageSize), TotalItems = totalItems }; return paginatedList; }
public async Task<object> GetProperties(PropertyListRequest requestParams) { if (!LoggedIn) { Debug.WriteLine("Not logged in"); return null; } var flurl = PropertiesRequestUrl.SetQueryParams(requestParams); // Flurl seems to skip bool parameters. Add it manually. flurl.SetQueryParam ("SortAscending", requestParams.SortAscending ? "true" : "false"); // Flurl doesn't know how to marshal a list into a query string, so we do it ourselves by // emitting the same key for different userid values, so that Web Api query string // parsing will be able to re-create the List<int> flurl.RemoveQueryParam ("UserIdListFilter"); string url = flurl; foreach(var userid in requestParams.UserIdListFilter) { url += "&UserIdListFilter=" + userid; } using (var response = await _client.GetAsync(url)) { var content = response.Content != null ? await response.Content.ReadAsStringAsync() : null; if (response.IsSuccessStatusCode == false) { var errorResult = content != null ? JsonConvert.DeserializeObject<ErrorResult>(content) : null; Debug.WriteLine("Request failed: " + response.ToString()); return errorResult; } var parsedObject = JsonConvert.DeserializeObject<PaginatedPropertyList>(content); if (parsedObject == null) { Debug.WriteLine("Could not deserialize json(" + content + ")"); } return parsedObject; } }
private async Task<PaginatedPropertyList> GetPropertiesAsync(PropertyListRequest requestParams) { object response = null; using (_dialogService.Loading("Getting properties...")) response = await _propertyTrackerService.GetProperties(requestParams); if (response is PaginatedPropertyList) { return response as PaginatedPropertyList; } var msg = response is ErrorResult ? (response as ErrorResult).Message : "Failed to retreive properties"; _dialogService.Alert(msg, "Request Failed"); return null; }