Пример #1
0
        public async Task <IActionResult> GetHateaos(OrderQueryParameter parameter)
        {
            if (!_propertyMappingContainer.ValidateMappingExsitFor <OrderResourceModel, Order>(parameter.OrderBy))
            {
                return(BadRequest("Can't finds fields for sorting"));
            }

            if (!_typeHelperService.TypeHasProperties <OrderResourceModel>(parameter.Fields))
            {
                return(BadRequest("Fields not exist"));
            }

            var paginatedList = await _orderRepository.GetOrdersAsync(parameter);

            var orderResourceModel = _mapper.Map <IEnumerable <Order>, IEnumerable <OrderResourceModel> >(paginatedList);

            var previousPageLink = paginatedList.HasPrevious ? CreateOrderUrl(parameter, PaginatedUrlType.PreviousPage) : null;
            var nextPageLink     = paginatedList.HasNext ? CreateOrderUrl(parameter, PaginatedUrlType.NextPage) : null;
            var meta             = new
            {
                paginatedList.PageIndex,
                paginatedList.PageSize,
                paginatedList.TotalItemCount,
                paginatedList.PageCount,
            };

            Response.Headers.Add("X-Pagination", JsonConvert.SerializeObject(meta, new JsonSerializerSettings()
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            }));
            var shapeOrderResources = orderResourceModel.ToDynamicIEnumerable(parameter.Fields);

            _logger.LogInformation(JsonConvert.SerializeObject(shapeOrderResources));

            var shapedWithLinks = shapeOrderResources.Select(x =>
            {
                var dict       = x as IDictionary <string, object>;
                var orderLinks = CreateLinksForOrder((Guid)dict["Id"], parameter.Fields);
                dict.Add("links", orderLinks);
                return(dict);
            });
            var links  = CreateLinksForOrders(parameter, paginatedList.HasPrevious, paginatedList.HasNext);
            var result = new
            {
                value = shapedWithLinks,
                links
            };

            return(Ok(result));
        }