public CollectionsControllerTests()
        {
            _mockUserService       = new Mock <IUserService>();
            _mockCollectionService = new Mock <ICollectionService>();
            _mockCollectionService.Setup(c => c.Save()).ReturnsAsync(true);
            _mockUserService.Setup(u => u.UserExists(It.IsAny <Guid>())).ReturnsAsync(true);

            _controller = new CollectionsController(_mockCollectionService.Object, _mockUserService.Object, _mapper);
            SetupTestController(_controller);

            _builder                  = new CollectionTestBuilder();
            resourceParameters        = new CollectionsResourceParameters();
            resourceParameters.Fields = "Id, Type";
        }
Esempio n. 2
0
        public async Task <PagedList <Collection> > FindCollections(CollectionsResourceParameters resourceParameters)
        {
            var collections = await _unitOfWork.Collections.GetAll();

            if (!string.IsNullOrEmpty(resourceParameters.Type))
            {
                collections = await _unitOfWork.Collections.GetAll(new CollectionByType(resourceParameters.Type));
            }

            if (!string.IsNullOrEmpty(resourceParameters.Search))
            {
                collections = await _unitOfWork.Collections
                              .GetAll(new CollectionByType(resourceParameters.Type) || new CollectionBySearch(resourceParameters.Search));
            }

            collections = collections.OrderBy(resourceParameters.OrderBy,
                                              PropertyMappingService.CollectionPropertyMapping);

            return(PagedList <Collection> .Create(collections.ToList(), resourceParameters.Page, resourceParameters.PageSize));
        }
Esempio n. 3
0
        private string CreateCollectionsResourceUri(CollectionsResourceParameters resourceParameters,
                                                    ResourceUriType type)
        {
            switch (type)
            {
            case ResourceUriType.PreviousPage:
                return(Url.Link("GetCollections", new
                {
                    type = resourceParameters.Type,
                    search = resourceParameters.Search,
                    orderBy = resourceParameters.OrderBy,
                    fields = resourceParameters.Fields,
                    page = resourceParameters.Page - 1,
                    pageSize = resourceParameters.PageSize
                }));

            case ResourceUriType.NextPage:
                return(Url.Link("GetCollections", new
                {
                    type = resourceParameters.Type,
                    search = resourceParameters.Search,
                    orderBy = resourceParameters.OrderBy,
                    fields = resourceParameters.Fields,
                    page = resourceParameters.Page + 1,
                    pageSize = resourceParameters.PageSize
                }));

            default:
                return(Url.Link("GetCollections", new
                {
                    type = resourceParameters.Type,
                    search = resourceParameters.Search,
                    orderBy = resourceParameters.OrderBy,
                    fields = resourceParameters.Fields,
                    page = resourceParameters.Page,
                    pageSize = resourceParameters.PageSize
                }));
            }
        }
Esempio n. 4
0
        private IEnumerable <LinkDto> CreateCollectionsLinks
            (CollectionsResourceParameters resourceParameters,
            bool hasNext, bool hasPrevious)
        {
            var links = new List <LinkDto>
            {
                new LinkDto(CreateCollectionsResourceUri(resourceParameters,
                                                         ResourceUriType.Current), "self", "GET")
            };

            if (hasNext)
            {
                links.Add(new LinkDto(CreateCollectionsResourceUri(resourceParameters,
                                                                   ResourceUriType.NextPage), "nextPage", "GET"));
            }

            if (hasPrevious)
            {
                links.Add(new LinkDto(CreateCollectionsResourceUri(resourceParameters,
                                                                   ResourceUriType.PreviousPage), "previousPage", "GET"));
            }

            return(links);
        }
 public CollectionServiceTests()
 {
     _collectionService = new CollectionService(_unitOfWork);
     resourceParameters = new CollectionsResourceParameters();
 }
Esempio n. 6
0
        public async Task <IActionResult> GetCollections(CollectionsResourceParameters resourceParameters,
                                                         [FromHeader(Name = "Accept")] string mediaType)
        {
            if (!PropertyMappingService.ValidMappingExistsFor <Collection>(resourceParameters.OrderBy))
            {
                return(BadRequest());
            }

            if (!TypeHelper.TypeHasProperties <CollectionDto>(resourceParameters.Fields))
            {
                return(BadRequest());
            }

            var retrievedCollections = await _collectionService.FindCollections(resourceParameters);

            var collections       = _mapper.Map <IEnumerable <CollectionDto> >(retrievedCollections);
            var shapedCollections = collections.ShapeData(resourceParameters.Fields);

            if (mediaType == "application/json+hateoas")
            {
                if (!string.IsNullOrEmpty(resourceParameters.Fields) &&
                    !resourceParameters.Fields.ToLowerInvariant().Contains("id"))
                {
                    return(BadRequest("Field parameter 'id' is required"));
                }

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

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

                var links = CreateCollectionsLinks(resourceParameters,
                                                   retrievedCollections.HasNext, retrievedCollections.HasPrevious);

                var linkedCollections = shapedCollections.Select(collection =>
                {
                    var collectionAsDictionary = collection as IDictionary <string, object>;
                    var collectionLinks        = CreateCollectionLinks((Guid)collectionAsDictionary["Id"],
                                                                       resourceParameters.Fields);

                    collectionAsDictionary.Add("links", collectionLinks);

                    return(collectionAsDictionary);
                });

                var linkedCollectionResource = new LinkedCollectionResource
                {
                    Value = linkedCollections,
                    Links = links
                };

                return(Ok(linkedCollectionResource));
            }
            else
            {
                var previousPageLink = retrievedCollections.HasPrevious ?
                                       CreateCollectionsResourceUri(resourceParameters,
                                                                    ResourceUriType.PreviousPage) : null;

                var nextPageLink = retrievedCollections.HasNext ?
                                   CreateCollectionsResourceUri(resourceParameters,
                                                                ResourceUriType.NextPage) : null;

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

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

                return(Ok(shapedCollections));
            }
        }