public async Task <ActionResult <IEnumerable <AgencyDto> > > GetAgencies(
            [FromQuery] AgenciesParam agenciesParameters)
        {
            if (!_propertyMappingService.ValidMappingExistsFor <AgencyDto, Agency>
                    (agenciesParameters.OrderBy))
            {
                return(BadRequest());
            }

            var agenciesFromRepo = await _AgenciesRepo.GetAgencies(agenciesParameters);

            var previousPageLink = agenciesFromRepo.HasPrevious ?
                                   CreateAgenciesResourceUri(agenciesParameters,
                                                             ResourceUriType.PreviousPage) : null;

            var nextPageLink = agenciesFromRepo.HasNext ?
                               CreateAgenciesResourceUri(agenciesParameters,
                                                         ResourceUriType.NextPage) : null;

            Response.AddPagination(agenciesFromRepo.CurrentPage, agenciesFromRepo.PageSize, agenciesFromRepo.TotalCount,
                                   agenciesFromRepo.TotalPages, previousPageLink, nextPageLink);


            return(Ok(_mapper.Map <IEnumerable <AgencyDto> >(agenciesFromRepo)));
        }
Exemple #2
0
        public async Task <PagedList <Agency> > GetAgencies(AgenciesParam agenciesParameters)
        {
            if (agenciesParameters == null)
            {
                throw new ArgumentNullException(nameof(agenciesParameters));
            }

            var collection = _context.Agencies as IQueryable <Agency>;

            if (!string.IsNullOrWhiteSpace(agenciesParameters.Ministry))
            {
                var ministry = agenciesParameters.Ministry.Trim();
                collection = collection.Where(a => a.Ministry.Contains(ministry));
            }

            if (!string.IsNullOrWhiteSpace(agenciesParameters.SearchQuery))
            {
                var searchQuery = agenciesParameters.SearchQuery.Trim();
                collection = collection.Where(a => a.Ministry.Contains(searchQuery) ||
                                              a.Name.Contains(searchQuery));
            }

            if (!string.IsNullOrWhiteSpace(agenciesParameters.OrderBy))
            {
                // get property mapping dictionary
                var agencyPropertyMappingDictionary =
                    _propertyMappingService.GetPropertyMapping <AgencyDto, Agency>();

                collection = collection.ApplySort(agenciesParameters.OrderBy,
                                                  agencyPropertyMappingDictionary);
            }

            return(await PagedList <Agency> .CreateAsync(collection, agenciesParameters.PageNumber, agenciesParameters.PageSize));
        }
        private string CreateAgenciesResourceUri(
            AgenciesParam agenciesParameters,
            ResourceUriType type)
        {
            switch (type)
            {
            case ResourceUriType.PreviousPage:
                return(Url.Link("GetAgencies",
                                new
                {
                    orderBy = agenciesParameters.OrderBy,
                    pageNumber = agenciesParameters.PageNumber - 1,
                    pageSize = agenciesParameters.PageSize,
                    mainCategory = agenciesParameters.Ministry,
                    searchQuery = agenciesParameters.SearchQuery
                }));

            case ResourceUriType.NextPage:
                return(Url.Link("GetAgencies",
                                new
                {
                    orderBy = agenciesParameters.OrderBy,
                    pageNumber = agenciesParameters.PageNumber + 1,
                    pageSize = agenciesParameters.PageSize,
                    mainCategory = agenciesParameters.Ministry,
                    searchQuery = agenciesParameters.SearchQuery
                }));

            default:
                return(Url.Link("GetAgencies",
                                new
                {
                    orderBy = agenciesParameters.OrderBy,
                    pageNumber = agenciesParameters.PageNumber,
                    pageSize = agenciesParameters.PageSize,
                    mainCategory = agenciesParameters.Ministry,
                    searchQuery = agenciesParameters.SearchQuery
                }));
            }
        }