/// <summary> /// Asynchronously gets a range of locations which names contain the string given by the filter object. /// </summary> /// <param name="filter">Filter instance of type <see cref="IFilter"/></param> /// <param name="paging">A class instance that implements <see cref="IPagingParameters"/>, holds paging data.</param> /// <returns>Returns <see cref="Task{IPagedList{ILocation}}"/></returns> /// <exception cref="ArgumentNullException">Thrown if <paramref name="paging"/> is null.</exception> public async Task<IPagedList<ILocation>> GetAsync(IPagingParameters paging, ISortingParameters sorting, IFilter filter) { if (paging == null) { throw new ArgumentNullException("paging"); } if (sorting == null) { IList<ISortingPair> sortPair = new List<ISortingPair>() { new SortingPair("Name") }; sorting = new SortingParameters(sortPair); } var query = repository.GetAll<LocationEntity>() .OrderBy(sorting.GetJoinedSortExpressions()); if(filter != null && !string.IsNullOrEmpty(filter.SearchString)) { query = query.Where(e => e.Name.ToLower().Contains(filter.SearchString.ToLower())); } return Mapper.Map<IPagedList<ILocation>>(await query.ToPagedListAsync(paging.PageNumber, paging.PageSize)); }
/// <summary> /// Gets a collection of locations that meet the criteria. /// </summary> /// <param name="q">The search query.</param> /// <param name="page">Page number</param> /// <param name="size">Page size, max. amount of results returned</param> /// <param name="sort">Order by field</param> /// <param name="asc">Ascending sort direction</param> /// <returns></returns> public async Task<HttpResponseMessage> Get(string q = "", int page = 1, int size = 20, string sort = "", bool asc = true) { IFilter filter = string.IsNullOrWhiteSpace(q) ? null : new Filter(q); ISortingParameters sortParams = null; if (!string.IsNullOrWhiteSpace(sort)) { sortParams = new SortingParameters( new List<ISortingPair>() { new SortingPair(sort, asc) } ); } PagedListViewModel<LocationModel> locations = Mapper.Map<PagedListViewModel<LocationModel>>( Mapper.Map<IPagedList<LocationModel>>( await locationService.GetAsync(new PagingParameters(page, size), sortParams, filter) )); if (locations != null) { return Request.CreateResponse(HttpStatusCode.OK, locations); } else { return new HttpResponseMessage(HttpStatusCode.NotFound); } }