コード例 #1
0
        public IActionResult GetStaffMembers(StaffMemberResourceParameters staffMemberResourceParameters,
                                             [FromHeader(Name = "Accept")] string mediaType)
        {
            // check mappings are valid
            if (!_staffMemberMappingService.ValidMappingExistsFor <StaffMemberDTO, StaffMember>
                    (staffMemberResourceParameters.OrderBy))
            {
                return(BadRequest());
            }

            // check all fields are valid
            if (!_typeHelperService.TypeHasProperties <StaffMemberDTO>
                    (staffMemberResourceParameters.Fields))
            {
                return(BadRequest());
            }

            var staffMembersFromRepo = _supermarketRepository.GetAllStaffMembers(staffMemberResourceParameters);

            var staffMembers = Mapper.Map <IEnumerable <StaffMemberDTO> >(staffMembersFromRepo);

            if (mediaType == "application/vnd.idp.hateoas+json")
            {
                var paginationMetadata = new
                {
                    totalCount  = staffMembersFromRepo.TotalCount,
                    pageSize    = staffMembersFromRepo.PageSize,
                    currentPage = staffMembersFromRepo.CurrentPage,
                    totalPages  = staffMembersFromRepo.TotalPages,
                };

                Response.Headers.Add("X-Pagination",
                                     JsonConvert.SerializeObject(paginationMetadata));

                var links = CreateLinksForStaffMembers(staffMemberResourceParameters,
                                                       staffMembersFromRepo.HasNext, staffMembersFromRepo.HasPrevious);

                var shapedStaffMembers = staffMembers.ShapeData(staffMemberResourceParameters.Fields);

                var shapedStaffMembersWithLinks = shapedStaffMembers.Select(staffMember =>
                {
                    var staffMemberAsDictionary = staffMember as IDictionary <string, object>;
                    var staffMemberLinks        = CreateLinksForStaffMember(
                        (int)staffMemberAsDictionary["Id"], staffMemberResourceParameters.Fields);

                    staffMemberAsDictionary.Add("links", staffMemberLinks);

                    return(staffMemberAsDictionary);
                });

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

                return(Ok(linkedCollectionResource));
            }
            else
            {
                var previousPageLink = staffMembersFromRepo.HasPrevious ?
                                       CreateStaffMemberResourceUri(staffMemberResourceParameters,
                                                                    ResourceUriType.PreviousPage) : null;

                var nextPageLink = staffMembersFromRepo.HasNext ?
                                   CreateStaffMemberResourceUri(staffMemberResourceParameters,
                                                                ResourceUriType.NextPage) : null;

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

                Response.Headers.Add("X-Pagination",
                                     JsonConvert.SerializeObject(paginationMetadata));

                return(Ok(staffMembers.ShapeData(staffMemberResourceParameters.Fields)));
            }
        }