예제 #1
0
        private string CreateResourceUri(MetadataResourceParameters parameters, ResourceUriType type)
        {
            switch (type)
            {
            case ResourceUriType.PreviousPage:
                return(_urlHelper.Link("GetComputers",
                                       new
                {
                    searchQuery = parameters.SearchQuery,
                    filter = parameters.Filter,
                    pageNumber = parameters.PageNumber - 1,
                    pageSize = parameters.PageSize
                }));

            case ResourceUriType.NextPage:
                return(_urlHelper.Link("GetComputers",
                                       new
                {
                    searchQuery = parameters.SearchQuery,
                    filter = parameters.Filter,
                    pageNumber = parameters.PageNumber + 1,
                    pageSize = parameters.PageSize
                }));

            default:
                return(_urlHelper.Link("GetComputers",
                                       new
                {
                    searchQuery = parameters.SearchQuery,
                    filter = parameters.Filter,
                    pageNumber = parameters.PageNumber,
                    pageSize = parameters.PageSize
                }));
            }
        }
        public async Task <IActionResult> Get(MetadataResourceParameters metadataParameters)
        {
            var responses = await _graphicsCardsService.Get(metadataParameters);

            return(new ObjectResult(responses.Data)
            {
                StatusCode = (int)responses.HttpStatusCode
            });
        }
        public async Task <BaseResponse <PagedList <ComputerDto> > > GetComputers(MetadataResourceParameters parameters)
        {
            var result = await _computerRepository.Get(parameters.PageNumber, parameters.PageSize, parameters.Filter, parameters.SearchQuery);

            if (!result.Any())
            {
                return(BaseResponse <PagedList <ComputerDto> > .NotFound("No computers found"));
            }


            return(new BaseResponse <PagedList <ComputerDto> >((int)HttpStatusCode.OK,
                                                               new PagedList <ComputerDto>(result.Select(x => _mapper.Map <ComputerDto>(x)).ToList(), result.TotalCount,
                                                                                           parameters.PageNumber, parameters.PageSize)));
        }
예제 #4
0
        public async Task <IActionResult> GetComputers(MetadataResourceParameters metadataResourceParameters,
                                                       [FromHeader(Name = "Accept")] string mediaType)
        {
            var result = await _computersService.GetComputers(metadataResourceParameters);

            var previousPageLink = result.Data.HasPrevious ? CreateResourceUri(metadataResourceParameters, ResourceUriType.PreviousPage) : null;
            var nextPageLink     = result.Data.HasNext ? CreateResourceUri(metadataResourceParameters, ResourceUriType.NextPage) : null;

            var a = 1;
            var b = a;

            var paginationMetaData = new
            {
                totalCount       = result.Data.TotalCount,
                pageSize         = result.Data.PageSize,
                currentPage      = result.Data.CurrentPage,
                totalPages       = result.Data.TotalPages,
                previousPageLink = previousPageLink,
                nextPageLink     = nextPageLink
            };

            Response.Headers.Add(new KeyValuePair <string, StringValues>("X-Pagination", JsonConvert.SerializeObject(paginationMetaData)));

            var response = result.Data.Select(x => new ComputerDto
            {
                Name         = x.Name,
                Id           = x.Id,
                GraphicCards = x.GraphicCards,
                Price        = x.Price,
                Rams         = x.Rams
            });

            if (mediaType.IsApplicationHal())
            {
                var wrapper = new LinkedCollectionResourceWrapperDto <ComputerDto>(response.Select(CreateLinkForComputer));

                return(new ObjectResult(CreateLinkForComputers(wrapper))
                {
                    StatusCode = (int)result.HttpStatusCode
                });
            }

            return(new ObjectResult(response)
            {
                StatusCode = (int)result.HttpStatusCode
            });
        }
예제 #5
0
        public async Task <BaseResponse <IEnumerable <GraphicsCardDto> > > Get(MetadataResourceParameters parameters)
        {
            var graphicsCards = await _graphicsCardRepository.Get(parameters.PageNumber, parameters.PageSize, parameters.Filter, parameters.SearchQuery);

            return(new BaseResponse <IEnumerable <GraphicsCardDto> >((int)HttpStatusCode.OK, _mapper.Map <IEnumerable <GraphicsCardDto> >(graphicsCards)));
        }