コード例 #1
0
        public async Task FindCoins_ReturnsAllCoins()
        {
            //Act
            var result = await _coinService.FindCoins(resourceParameters);

            //Assert
            Assert.NotNull(result);
            Assert.Equal(6, result.Count());
        }
        public CoinManipulationDtoValidator(ICoinService service, IMapper mapper)
        {
            _service = service;
            _mapper  = mapper;

            var coins = _service.FindCoins(new CurrenciesResourceParameters()).Result;

            RuleFor(b => b)
            .IsDuplicate(coins, new CurrencyComparer(), _mapper, "Coin must be unique");

            RuleFor(c => c.FaceValue)
            .NotEmpty().WithMessage("Face value is a required field");

            RuleFor(c => c.Type)
            .NotEmpty().WithMessage("Type is a required field")
            .MaximumLength(100).WithMessage("Type shouldn't contain more than 100 characters");

            RuleFor(c => c.ReleaseDate)
            .NotEmpty().WithMessage("ReleaseDate is a required field")
            .MaximumLength(100).WithMessage("ReleaseDate shouldn't contain more than 100 characters");

            RuleFor(c => c.CountryId)
            .NotEmpty().WithMessage("CountryId is a required field");

            RuleFor(c => c.Metal)
            .MaximumLength(100).WithMessage("Metal shouldn't contain more than 100 characters");

            RuleFor(c => c.Note)
            .MaximumLength(250).WithMessage("Note shouldn't contain more than 250 characters");

            RuleFor(c => c.Subject)
            .MaximumLength(250).WithMessage("Subject shouldn't contain more than 250 characters");

            RuleFor(c => c.ObverseDescription)
            .MaximumLength(250).WithMessage("ObverseDescription shouldn't contain more than 250 characters");

            RuleFor(c => c.ObverseInscription)
            .MaximumLength(100).WithMessage("ObverseInscription shouldn't contain more than 100 characters");

            RuleFor(c => c.ObverseLegend)
            .MaximumLength(100).WithMessage("ObverseLegend shouldn't contain more than 100 characters");

            RuleFor(c => c.ReverseDescription)
            .MaximumLength(250).WithMessage("ReverseDescription shouldn't contain more than 250 characters");

            RuleFor(c => c.ReverseInscription)
            .MaximumLength(100).WithMessage("ReverseInscription shouldn't contain more than 100 characters");

            RuleFor(c => c.ReverseLegend)
            .MaximumLength(100).WithMessage("ReverseLegend shouldn't contain more than 100 characters");

            RuleFor(c => c.EdgeType)
            .MaximumLength(50).WithMessage("EdgeType shouldn't contain more than 50 characters");

            RuleFor(c => c.EdgeLegend)
            .MaximumLength(100).WithMessage("EdgeLegend shouldn't contain more than 100 characters");

            RuleFor(c => c.Designer)
            .MaximumLength(250).WithMessage("Designer shouldn't contain more than 250 characters");

            RuleFor(c => c.HeadOfState)
            .MaximumLength(250).WithMessage("HeadOfState shouldn't contain more than 250 characters");

            RuleFor(c => c.MintMark)
            .MaximumLength(50).WithMessage("MintMark shouldn't contain more than 50 characters");

            RuleFor(c => c.FrontImagePath)
            .MaximumLength(250).WithMessage("FrontImagePath shouldn't contain more than 250 characters");

            RuleFor(c => c.BackImagePath)
            .MaximumLength(250).WithMessage("BackImagePath shouldn't contain more than 250 characters");
        }
コード例 #3
0
        public async Task <IActionResult> GetCoins(CurrenciesResourceParameters resourceParameters,
                                                   [FromHeader(Name = "Accept")] string mediaType)
        {
            if (!PropertyMappingService.ValidMappingExistsFor <Coin>(resourceParameters.OrderBy))
            {
                return(BadRequest());
            }

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

            var retrievedCoins = await _coinService.FindCoins(resourceParameters);

            var coins       = _mapper.Map <IEnumerable <CoinDto> >(retrievedCoins);
            var shapedCoins = coins.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  = retrievedCoins.TotalCount,
                    pageSize    = retrievedCoins.PageSize,
                    currentPage = retrievedCoins.CurrentPage,
                    totalPages  = retrievedCoins.TotalPages
                };

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

                var links = CreateCoinsLinks(resourceParameters,
                                             retrievedCoins.HasNext, retrievedCoins.HasPrevious);

                var linkedCoins = shapedCoins.Select(coin =>
                {
                    var coinAsDictionary = coin as IDictionary <string, object>;
                    var coinLinks        = CreateCoinLinks((Guid)coinAsDictionary["Id"],
                                                           resourceParameters.Fields);

                    coinAsDictionary.Add("links", coinLinks);

                    return(coinAsDictionary);
                });

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

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

                var nextPageLink = retrievedCoins.HasNext ?
                                   CreateCoinsResourceUri(resourceParameters,
                                                          ResourceUriType.NextPage) : null;

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

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

                return(Ok(shapedCoins));
            }
        }