public IActionResult GetCenters([FromQuery] CentersResourceParameters centersResourceParameters, [FromHeader(Name = "Accept")] string mediaType)
        {
            if (!MediaTypeHeaderValue.TryParse(mediaType, out MediaTypeHeaderValue parsedMediaType))
            {
                return(BadRequest(new
                {
                    message = "Accept header mediaType is not allowed"
                }));
            }

            if (!_propertyMappingService.ValidMappingExistsFor <CenterDto, Center> (centersResourceParameters.OrderBy))
            {
                return(BadRequest());
            }

            if (!_propertyCheckerService.TypeHasProperties <CenterDto> (centersResourceParameters.Fields))
            {
                return(BadRequest());
            }

            var centers = _centerRepository.GetCenters(centersResourceParameters);

            var paginationMetadata = new
            {
                totalCount  = centers.TotalCount,
                pageSize    = centers.PageSize,
                currentPage = centers.CurrentPage,
                totalPages  = centers.TotalPages
            };

            Response.Headers.Add("X-Pagination", JsonSerializer.Serialize(paginationMetadata));

            var links        = CreateLinksForCenters(centersResourceParameters, centers.HasNext, centers.HasPrevious);
            var shapeCenters = _mapper.Map <IEnumerable <CenterDto> > (centers).ShapeData(centersResourceParameters.Fields);

            if (parsedMediaType.MediaType == "application/vnd.marvin.hateoas+json")
            {
                var shapeCentersWithLinks = shapeCenters.Select(center =>
                {
                    var centerAsDictionary = center as IDictionary <string, object>;
                    var centerLinks        = CreateLinksForCenter((Guid)centerAsDictionary["CenterId"], null);

                    centerAsDictionary.Add("links", centerLinks);

                    return(centerAsDictionary);
                });

                var linkedCollectionResource = new
                {
                    value = shapeCentersWithLinks,
                    links,
                };

                return(Ok(linkedCollectionResource));
            }

            return(Ok(shapeCenters));
        }
Beispiel #2
0
 public List <Center> GetCenters()
 {
     return(_centerRepository.GetCenters());
 }