private IEnumerable <LinkDTO> CreateLinksForStaffMembers(StaffMemberResourceParameters staffMemberResourceParameters,
                                                                 bool hasNext, bool hasPrevious)
        {
            var links = new List <LinkDTO>();

            // self
            links.Add(
                new LinkDTO(CreateStaffMemberResourceUri(staffMemberResourceParameters,
                                                         ResourceUriType.Current)
                            , "self", "GET"));

            if (hasNext)
            {
                links.Add(
                    new LinkDTO(CreateStaffMemberResourceUri(staffMemberResourceParameters,
                                                             ResourceUriType.NextPage),
                                "nextPage", "GET"));
            }

            if (hasPrevious)
            {
                links.Add(
                    new LinkDTO(CreateStaffMemberResourceUri(staffMemberResourceParameters,
                                                             ResourceUriType.PreviousPage),
                                "previousPage", "GET"));
            }

            return(links);
        }
        public PagedList <StaffMember> GetAllStaffMembers(StaffMemberResourceParameters staffMemberResourceParameters)
        {
            var collectionBeforePaging =
                _context.StaffMembers.ApplySort(staffMemberResourceParameters.OrderBy,
                                                _staffMemberPropertyMappingService.GetPropertyMapping <StaffMemberDTO, StaffMember>());

            if (!string.IsNullOrEmpty(staffMemberResourceParameters.SearchQuery))
            {
                // trim & ignore casing
                var searchQueryForWhereClause = staffMemberResourceParameters.SearchQuery
                                                .Trim().ToLowerInvariant();

                collectionBeforePaging = collectionBeforePaging
                                         .Where(s => s.Name.ToLowerInvariant().Contains(searchQueryForWhereClause));
            }

            return(PagedList <StaffMember> .Create(collectionBeforePaging,
                                                   staffMemberResourceParameters.PageNumber,
                                                   staffMemberResourceParameters.PageSize));
        }
        private string CreateStaffMemberResourceUri(
            StaffMemberResourceParameters staffMemberResourceParameters,
            ResourceUriType type)
        {
            switch (type)
            {
            case ResourceUriType.PreviousPage:
                return(_urlHelper.Link("GetStaffMembers",
                                       new
                {
                    fields = staffMemberResourceParameters.Fields,
                    orderBy = staffMemberResourceParameters.OrderBy,
                    searchQuery = staffMemberResourceParameters.SearchQuery,
                    pageNumber = staffMemberResourceParameters.PageNumber - 1,
                    pageSize = staffMemberResourceParameters.PageSize
                }));

            case ResourceUriType.NextPage:
                return(_urlHelper.Link("GetStaffMembers",
                                       new
                {
                    fields = staffMemberResourceParameters.Fields,
                    orderBy = staffMemberResourceParameters.OrderBy,
                    searchQuery = staffMemberResourceParameters.SearchQuery,
                    pageNumber = staffMemberResourceParameters.PageNumber + 1,
                    pageSize = staffMemberResourceParameters.PageSize
                }));

            default:
                return(_urlHelper.Link("GetStaffMembers",
                                       new
                {
                    fields = staffMemberResourceParameters.Fields,
                    orderBy = staffMemberResourceParameters.OrderBy,
                    searchQuery = staffMemberResourceParameters.SearchQuery,
                    pageNumber = staffMemberResourceParameters.PageNumber,
                    pageSize = staffMemberResourceParameters.PageSize
                }));
            }
        }
        public IActionResult GetStaffMembersFromSupermarket(int supermarketId, StaffMemberResourceParameters staffMemberResourceParameters,
                                                            [FromHeader(Name = "Accept")] string mediaType)
        {
            // check mappings are valid
            if (!_staffMemberPropertyMappingService.ValidMappingExistsFor <StaffMemberDTO, StaffMember>
                    (staffMemberResourceParameters.OrderBy))
            {
                return(BadRequest());
            }

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

            var staffMembersFromRepo = _supermarketRepository.GetAllStaffMembersFromSupermarket(supermarketId, 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)));
            }
        }