示例#1
0
        public ProjectQueryDTOValidator(IPropertyMappingContainer propertyMappingContainer, ITypeHelperService typeHelperService)
        {
            RuleFor(e => e.OrderBy).Must(order => propertyMappingContainer.ValidMappingExistsFor <ProductDTO, Product>(order))
            .WithMessage("Can't find the fields in {PropertyName} for sorting.");

            RuleFor(e => e.Fields).Must(fields => typeHelperService.TypeHasProperties <ProductDTO>(fields))
            .WithMessage("Can't find the fields in {PropertyName} on DTO.");
        }
        public async Task <IActionResult> GetProducts(ResourceParameter parameter)
        {
            log.Info("获取所有的产品list");
            List <Product> products = new List <Product>();
            var            query    = products.AsQueryable();
            int            count    = 0;

            try {
                if (!_propertyMappingContainer.ValidMappingExistsFor <ResourceParameter, Product>(parameter.OrderBy))
                {
                    return(BadRequest("找不到要排序的字段"));
                }
                ProductWebApiContext productWebApiContext = new ProductWebApiContext(conn.DefaultConnection);
                var propertiesMap = new Dictionary <string, Expression <Func <Product, object> > >
                {
                    { "Id", p => p.Id },
                    { "Name", p => p.Name },
                    { "Price", p => p.Price }
                };
                if (!string.IsNullOrEmpty(parameter.Name))
                {
                    query = (from p in productWebApiContext.Product where p.Name.Contains(parameter.Name) select p)
                            .AsQueryable();
                    count = await(from p in productWebApiContext.Product where p.Name.Contains(parameter.Name) select p).CountAsync();
                }
                else
                {
                    query = (from p in productWebApiContext.Product select p)
                            .AsQueryable();
                    count = await(from p in productWebApiContext.Product select p).CountAsync();
                }

                query    = query.ApplySort(parameter.OrderBy, _propertyMappingContainer.Resolve <ResourceParameter, Product>());
                products = query.Skip(parameter.PageIndex * parameter.PageSize).Take(parameter.PageSize).ToList();
                var returnlist = new PaginatedList <Product>(parameter.PageIndex, parameter.PageSize, count, products);
                var preLink    = returnlist.HasPrevious
                    ? new CreateProductResourceUrl(_urlHelper).CreateResouceUrl(parameter, PaginationResourceUriType.PreviousPage,
                                                                                "GetProducts"):null;
                var nextLink = returnlist.HasNext
                    ? new CreateProductResourceUrl(_urlHelper).CreateResouceUrl(parameter, PaginationResourceUriType.NextPage,
                                                                                "GetProducts") : null;
                var mata = new
                {
                    returnlist.TotalItemsCount,
                    returnlist.PaginationBase.PageSize,
                    returnlist.PaginationBase.PageIndex,
                    returnlist.PageCount,
                    preLink,
                    nextLink
                };
                Response.Headers.Add("X-Pagination", JsonConvert.SerializeObject(mata));
            }
            catch (Exception e) {
                log.Error(e);
            }

            return(Ok(products));
        }
示例#3
0
        public async Task <IActionResult> GetCountries(CountryDtoParamters countryDtoParamters, [FromHeader(Name = "Accept")] string mediaType)
        {
            if (!_propertyMappingContainer.ValidMappingExistsFor <CountryDto, Country>(countryDtoParamters.OrderBy))
            {
                return(BadRequest("Can't find the fields for sorting"));
            }

            if (!_typeHelperService.TypeHasProperties <CountryDto>(countryDtoParamters.Fields))
            {
                return(BadRequest("Can't find the fields on Resource."));
            }
            var pagedList = await _countryRepository.GetCountriesAsync(countryDtoParamters);

            var countryDto = _mapper.Map <List <CountryDto> >(pagedList);

            return(Ok());
        }
示例#4
0
        public async Task <IActionResult> GetCountries(CountryResourceParameters countryResourceParameters,
                                                       [FromHeader(Name = "Accept")] string mediaType)
        {
            foreach (var claim in User.Claims)
            {
                Console.WriteLine(claim.Type + ", " + claim.Value);
            }

            if (!_propertyMappingContainer.ValidMappingExistsFor <CountryResource, Country>(countryResourceParameters.OrderBy))
            {
                return(BadRequest("Can't find the fields for sorting."));
            }

            if (!_typeHelperService.TypeHasProperties <CountryResource>(countryResourceParameters.Fields))
            {
                return(BadRequest("Can't find the fields on Resource."));
            }

            var pagedList = await _countryRepository.GetCountriesAsync(countryResourceParameters);

            var countryResources = _mapper.Map <List <CountryResource> >(pagedList);

            if (mediaType == "application/vnd.solenovex.hateoas+json")
            {
                var meta = new
                {
                    pagedList.TotalItemsCount,
                    pagedList.PaginationBase.PageSize,
                    pagedList.PaginationBase.PageIndex,
                    pagedList.PageCount
                };

                Response.Headers.Add("X-Pagination", JsonConvert.SerializeObject(meta, new JsonSerializerSettings
                {
                    ContractResolver = new CamelCasePropertyNamesContractResolver()
                }));

                var links                    = CreateLinksForCountries(countryResourceParameters, pagedList.HasPrevious, pagedList.HasNext);
                var shapedResources          = countryResources.ToDynamicIEnumerable(countryResourceParameters.Fields);
                var shapedResourcesWithLinks = shapedResources.Select(country =>
                {
                    var countryDict  = country as IDictionary <string, object>;
                    var countryLinks = CreateLinksForCountry((int)countryDict["Id"], countryResourceParameters.Fields);
                    countryDict.Add("links", countryLinks);
                    return(countryDict);
                });
                var linkedCountries = new
                {
                    value = shapedResourcesWithLinks,
                    links
                };

                return(Ok(linkedCountries));
            }
            else
            {
                var previousPageLink = pagedList.HasPrevious ?
                                       CreateCountryUri(countryResourceParameters,
                                                        PaginationResourceUriType.PreviousPage) : null;

                var nextPageLink = pagedList.HasNext ?
                                   CreateCountryUri(countryResourceParameters,
                                                    PaginationResourceUriType.NextPage) : null;

                var meta = new
                {
                    pagedList.TotalItemsCount,
                    pagedList.PaginationBase.PageSize,
                    pagedList.PaginationBase.PageIndex,
                    pagedList.PageCount,
                    previousPageLink,
                    nextPageLink
                };

                Response.Headers.Add("X-Pagination", JsonConvert.SerializeObject(meta, new JsonSerializerSettings
                {
                    ContractResolver = new CamelCasePropertyNamesContractResolver()
                }));

                return(Ok(countryResources.ToDynamicIEnumerable(countryResourceParameters.Fields)));
            }
        }