Exemple #1
0
        public ConditionsControllerTests()
        {
            _mockConditionService = new Mock <IConditionService>();
            _mockConditionService.Setup(c => c.Save()).ReturnsAsync(true);

            _controller = new ConditionsController(_mockConditionService.Object, _mapper);
            SetupTestController(_controller);

            _builder                  = new ConditionTestBuilder();
            resourceParameters        = new ConditionsResourceParameters();
            resourceParameters.Fields = "Id, Grade";
        }
Exemple #2
0
        private string CreateConditionsResourceUri(ConditionsResourceParameters resourceParameters,
                                                   ResourceUriType type)
        {
            switch (type)
            {
            case ResourceUriType.PreviousPage:
                return(Url.Link("GetConditions", new
                {
                    grade = resourceParameters.Grade,
                    languageCode = resourceParameters.LanguageCode,
                    search = resourceParameters.Search,
                    orderBy = resourceParameters.OrderBy,
                    fields = resourceParameters.Fields,
                    page = resourceParameters.Page - 1,
                    pageSize = resourceParameters.PageSize
                }));

            case ResourceUriType.NextPage:
                return(Url.Link("GetConditions", new
                {
                    grade = resourceParameters.Grade,
                    languageCode = resourceParameters.LanguageCode,
                    search = resourceParameters.Search,
                    orderBy = resourceParameters.OrderBy,
                    fields = resourceParameters.Fields,
                    page = resourceParameters.Page + 1,
                    pageSize = resourceParameters.PageSize
                }));

            default:
                return(Url.Link("GetConditions", new
                {
                    grade = resourceParameters.Grade,
                    languageCode = resourceParameters.LanguageCode,
                    search = resourceParameters.Search,
                    orderBy = resourceParameters.OrderBy,
                    fields = resourceParameters.Fields,
                    page = resourceParameters.Page,
                    pageSize = resourceParameters.PageSize
                }));
            }
        }
Exemple #3
0
        private IEnumerable <LinkDto> CreateConditionsLinks
            (ConditionsResourceParameters resourceParameters,
            bool hasNext, bool hasPrevious)
        {
            var links = new List <LinkDto>
            {
                new LinkDto(CreateConditionsResourceUri(resourceParameters,
                                                        ResourceUriType.Current), "self", "GET")
            };

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

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

            return(links);
        }
Exemple #4
0
        public async Task <PagedList <Condition> > FindConditions(ConditionsResourceParameters resourceParameters)
        {
            var conditions = await _unitOfWork.Conditions.GetAll();

            if (!string.IsNullOrEmpty(resourceParameters.Grade) || !string.IsNullOrEmpty(resourceParameters.LanguageCode))
            {
                conditions = await _unitOfWork.Conditions
                             .GetAll(new ConditionByGrade(resourceParameters.Grade) ||
                                     new ConditionByLanguageCode(resourceParameters.LanguageCode));
            }

            if (!string.IsNullOrEmpty(resourceParameters.Search))
            {
                conditions = await _unitOfWork.Conditions
                             .GetAll(new ConditionByGrade(resourceParameters.Grade) ||
                                     new ConditionByLanguageCode(resourceParameters.LanguageCode) ||
                                     new ConditionBySearch(resourceParameters.Search));
            }

            conditions = conditions.OrderBy(resourceParameters.OrderBy,
                                            PropertyMappingService.ConditionPropertyMapping);

            return(PagedList <Condition> .Create(conditions.ToList(), resourceParameters.Page, resourceParameters.PageSize));
        }
Exemple #5
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));
            }
        }
 public ConditionServiceTests()
 {
     _conditionService  = new ConditionService(_unitOfWork);
     resourceParameters = new ConditionsResourceParameters();
 }