public async Task<ActionResult<IEnumerable<CompanyDto>>> GetCompanyCollection(
            [FromRoute]
            [ModelBinder(BinderType =typeof(ArrayModelBinder))]
            IEnumerable<Guid> ids)
        {
            if (ids == null)
            {
                return BadRequest();
            }
            var entities = await _companyRepositroy.GetCompaniesAsync(ids);
            if (ids.Count() != entities.Count())
            {
                return NotFound();
            }

            var dtoToReturn = _mapper.Map<IEnumerable<CompanyDto>>(entities);

            return Ok(dtoToReturn);
        }
Exemplo n.º 2
0
        public async Task <IActionResult> GetCompanies([FromQuery] CompanyDtoParameters parameters, [FromHeader(Name = "Accept")] string mediaType)  //ActionResult<IEnumerable<DtoCompany>
        {
            if (!MediaTypeHeaderValue.TryParse(mediaType, out MediaTypeHeaderValue parsedMediaType))
            {
                return(BadRequest());
            }

            if (!_propertyMappingService.ValidMappingExistsFor <CompanyDto, Company>(parameters.OrderBy))
            {
                return(BadRequest());
            }
            if (!_propertyCheckerService.TypeHasProperites <CompanyDto>(parameters.Fields))
            {
                return(BadRequest());
            }

            var companies = await _companyRepositroy.GetCompaniesAsync(parameters);


            var companyDtos = _mapper.Map <IEnumerable <CompanyDto> >(companies);

            var shapeData = companyDtos.ShapeData(parameters.Fields);


            if (parsedMediaType.MediaType == "application/vnd.company.hateoas+json")
            {
                var paginationMetadata_hateoas = new
                {
                    totalCount  = companies.TotalCount,
                    pageSize    = companies.PageSize,
                    currentPage = companies.CurrentPage,
                    totalPage   = companies.TotalPages,
                    //privousPageLink = companies.HasPrevious ? CreateCompaniesResourceUri(parameters, ResourceUriType.PreviousPage) : null,
                    //nextPageLink = companies.HasNext ? CreateCompaniesResourceUri(parameters, ResourceUriType.NextPage) : null
                };

                Response.Headers.Add("X-Pagination",
                                     JsonSerializer.Serialize(paginationMetadata_hateoas, new JsonSerializerOptions {
                    Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
                })
                                     );

                var links = CreateLinksForCompany(parameters, companies.HasPrevious, companies.HasNext);

                var shapedCompaniesWithLinks = shapeData.Select(c =>
                {
                    var companyDict  = c as IDictionary <string, object>;
                    var companyLinks = CreateLinksForCompany((Guid)companyDict["Id"], null);
                    companyDict.Add("links", companyLinks);
                    return(companyDict);
                });

                var linkedCollectionResource = new
                {
                    value = shapedCompaniesWithLinks,
                    links = links
                };
                return(Ok(linkedCollectionResource));
            }

            var paginationMetadata = new
            {
                totalCount      = companies.TotalCount,
                pageSize        = companies.PageSize,
                currentPage     = companies.CurrentPage,
                totalPage       = companies.TotalPages,
                privousPageLink = companies.HasPrevious ? CreateCompaniesResourceUri(parameters, ResourceUriType.PreviousPage) : null,
                nextPageLink    = companies.HasNext ? CreateCompaniesResourceUri(parameters, ResourceUriType.NextPage) : null
            };

            Response.Headers.Add("X-Pagination",
                                 JsonSerializer.Serialize(paginationMetadata, new JsonSerializerOptions {
                Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
            })
                                 );

            return(Ok(shapeData));
        }