private static async Task <object> ResolveProductsConnectionAsync(IMediator mediator, IResolveConnectionContext <object> context)
        {
            var first         = context.First;
            var skip          = Convert.ToInt32(context.After ?? 0.ToString());
            var includeFields = context.SubFields.Values.GetAllNodesPaths();

            //TODO: Need to be able get entire query from context and read all arguments to the query properties
            var query = context.GetCatalogQuery <SearchProductQuery>();

            query.IncludeFields = includeFields;

            var productIds = context.GetArgument <List <string> >("productIds");

            if (productIds.IsNullOrEmpty())
            {
                query.Skip       = skip;
                query.Take       = first ?? context.PageSize ?? 10;
                query.Query      = context.GetArgument <string>("query");
                query.Filter     = context.GetArgument <string>("filter");
                query.Facet      = context.GetArgument <string>("facet");
                query.Fuzzy      = context.GetArgument <bool>("fuzzy");
                query.FuzzyLevel = context.GetArgument <int?>("fuzzyLevel");
                query.Sort       = context.GetArgument <string>("sort");
            }
            else
            {
                query.ObjectIds = productIds.ToArray();
                query.Take      = productIds.Count;
            }

            var response = await mediator.Send(query);

            var currencyCode = context.GetArgumentOrValue <string>("currencyCode");

            if (string.IsNullOrWhiteSpace(currencyCode))
            {
                context.SetCurrency(response.Currency);
            }

            var result = new ProductsConnection <ExpProduct>(response.Results, skip, Convert.ToInt32(context.After ?? 0.ToString()), response.TotalCount)
            {
                Facets = response.Facets
            };

            return(result);
        }
Esempio n. 2
0
        private async Task <object> ResolveOrdersConnectionAsync(IMediator mediator, IResolveConnectionContext <object> context)
        {
            var first = context.First;
            var skip  = Convert.ToInt32(context.After ?? 0.ToString());

            var request = new SearchOrderQuery
            {
                Skip        = skip,
                Take        = first ?? context.PageSize ?? 10,
                Filter      = context.GetArgument <string>("filter"),
                Sort        = context.GetArgument <string>("sort"),
                CultureName = context.GetArgument <string>(nameof(Currency.CultureName).ToCamelCase()),
                CustomerId  = context.GetArgumentOrValue <string>("userId")
            };

            context.CopyArgumentsToUserContext();
            var allCurrencies = await _currencyService.GetAllCurrenciesAsync();

            //Store all currencies in the user context for future resolve in the schema types
            context.SetCurrencies(allCurrencies, request.CultureName);


            var authorizationResult = await _authorizationService.AuthorizeAsync(context.GetCurrentPrincipal(), request, new CanAccessOrderAuthorizationRequirement());

            if (!authorizationResult.Succeeded)
            {
                throw new ExecutionError($"Access denied");
            }

            var response = await mediator.Send(request);

            foreach (var customerOrderAggregate in response.Results)
            {
                context.SetExpandedObjectGraph(customerOrderAggregate);
            }

            return(new PagedConnection <CustomerOrderAggregate>(response.Results, skip, Convert.ToInt32(context.After ?? 0.ToString()), response.TotalCount));
        }