示例#1
0
        public ProductionQuery(IBikeStoreRepository bikeStoreRepository)
        {
            Field <ListGraphType <BrandType> >(
                "brands",
                arguments: new QueryArguments(
                    new QueryArgument <IntGraphType> {
                Name = "brandId"
            }),
                resolve: context =>
            {
                var brandId = context.GetArgument <int?>("brandId");

                if (brandId.HasValue)
                {
                    return(Task.WhenAll(bikeStoreRepository.GetBrand(brandId.Value)));
                }
                return(bikeStoreRepository.GetBrands());
            });

            Field <ListGraphType <ProductType> >(
                "products",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <IntGraphType> > {
                Name = "page"
            },
                    new QueryArgument <NonNullGraphType <IntGraphType> > {
                Name = "itemsPerPage"
            }),
                resolve: context =>
            {
                var page         = context.GetArgument <int>("page");
                var itemsPerPage = context.GetArgument <int>("itemsPerPage");
                return(bikeStoreRepository.GetProducts(page, itemsPerPage));
            });
        }
        public async Task <IActionResult> Products(int page, int itemsPerPage)
        {
            if (page <= 0 || itemsPerPage <= 0)
            {
                return(BadRequest());
            }

            try
            {
                var products = await _bikeStoreRepository.GetProducts(page, itemsPerPage);

                return(Ok(products));
            }
            catch (Exception ex)
            {
                this._logger.LogError(ex, ex.Message);
                return(new StatusCodeResult(Status500InternalServerError));
            }
        }