public async Task FindConditions_ReturnsAllConditions()
        {
            //Act
            var result = await _conditionService.FindConditions(resourceParameters);

            //Assert
            Assert.NotNull(result);
            Assert.Equal(4, result.Count());
        }
Exemple #2
0
        public async Task <IActionResult> GetConditions(ConditionsResourceParameters resourceParameters,
                                                        [FromHeader(Name = "Accept")] string mediaType)
        {
            if (!PropertyMappingService.ValidMappingExistsFor <Condition>(resourceParameters.OrderBy))
            {
                return(BadRequest());
            }

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

            var retrievedConditions = await _conditionService.FindConditions(resourceParameters);

            var conditions       = _mapper.Map <IEnumerable <ConditionDto> >(retrievedConditions);
            var shapedConditions = conditions.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  = retrievedConditions.TotalCount,
                    pageSize    = retrievedConditions.PageSize,
                    currentPage = retrievedConditions.CurrentPage,
                    totalPages  = retrievedConditions.TotalPages
                };

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

                var links = CreateConditionsLinks(resourceParameters,
                                                  retrievedConditions.HasNext, retrievedConditions.HasPrevious);

                var linkedConditions = shapedConditions.Select(condition =>
                {
                    var conditionAsDictionary = condition as IDictionary <string, object>;
                    var conditionLinks        = CreateConditionLinks((Guid)conditionAsDictionary["Id"],
                                                                     resourceParameters.Fields);

                    conditionAsDictionary.Add("links", conditionLinks);

                    return(conditionAsDictionary);
                });

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

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

                var nextPageLink = retrievedConditions.HasNext ?
                                   CreateConditionsResourceUri(resourceParameters,
                                                               ResourceUriType.NextPage) : null;

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

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

                return(Ok(shapedConditions));
            }
        }