Ejemplo n.º 1
0
        public async Task <MenuLinkList[]> LoadAllStoreLinkListsAsync(string storeId)
        {
            var retVal    = new List <MenuLinkList>();
            var linkLists = await _cmsApi.MenuGetListsAsync(storeId);

            if (linkLists != null)
            {
                retVal.AddRange(linkLists.Select(x => x.ToWebModel()));
                var allMenuLinks  = retVal.SelectMany(x => x.MenuLinks);
                var productLinks  = allMenuLinks.OfType <ProductMenuLink>();
                var categoryLinks = allMenuLinks.OfType <CategoryMenuLink>();
                Task <Product[]>  productsLoadingTask   = null;
                Task <Category[]> categoriesLoadingTask = null;

                //Parallel loading associated objects
                var productIds = productLinks.Select(x => x.AssociatedObjectId).ToArray();
                if (productIds.Any())
                {
                    productsLoadingTask = _catalogSearchService.GetProductsAsync(productIds, ItemResponseGroup.ItemSmall | ItemResponseGroup.Seo);
                }
                var categoriesIds = categoryLinks.Select(x => x.AssociatedObjectId).ToArray();
                if (categoriesIds.Any())
                {
                    categoriesLoadingTask = _catalogSearchService.GetCategoriesAsync(categoriesIds, CategoryResponseGroup.Info | CategoryResponseGroup.WithImages | CategoryResponseGroup.WithSeo);
                }
                //Populate link by associated product
                if (productsLoadingTask != null)
                {
                    var products = await productsLoadingTask;
                    foreach (var productLink in productLinks)
                    {
                        productLink.Product = products.FirstOrDefault(x => x.Id == productLink.AssociatedObjectId);
                    }
                }
                //Populate link by associated category
                if (categoriesLoadingTask != null)
                {
                    var categories = await categoriesLoadingTask;
                    foreach (var categoryLink in categoryLinks)
                    {
                        categoryLink.Category = categories.FirstOrDefault(x => x.Id == categoryLink.AssociatedObjectId);
                    }
                }
            }
            return(retVal.ToArray());
        }
        public override async Task Invoke(IOwinContext context)
        {
            var workContext = _container.Resolve <WorkContext>();
            var urlBuilder  = _container.Resolve <IStorefrontUrlBuilder>();

            // Initialize common properties
            workContext.RequestUrl   = context.Request.Uri;
            workContext.AllCountries = _allCountries;

            workContext.AllStores = await _cacheManager.GetAsync("GetAllStores", "StoreRegion", async() => { return(await GetAllStoresAsync()); });

            if (workContext.AllStores != null && workContext.AllStores.Any())
            {
                var currentCustomerId = GetCurrentCustomerId(context);
                workContext.CurrentCustomer = await _cacheManager.GetAsync("GetCustomer-" + currentCustomerId, "ApiRegion", async() => { return(await GetCustomerAsync(context)); });

                MaintainAnonymousCustomerCookie(context, workContext);

                // Initialize request specific properties
                workContext.CurrentStore    = GetStore(context, workContext.AllStores);
                workContext.CurrentLanguage = GetLanguage(context, workContext.AllStores, workContext.CurrentStore);
                workContext.CurrentCurrency = GetCurrency(context, workContext.CurrentStore);

                //Do not load shopping cart and other for resource requests
                if (!IsAssetRequest(context.Request.Uri))
                {
                    //Shopping cart
                    await _cartBuilder.GetOrCreateNewTransientCartAsync(workContext.CurrentStore, workContext.CurrentCustomer, workContext.CurrentLanguage, workContext.CurrentCurrency);

                    workContext.CurrentCart = _cartBuilder.Cart;

                    var linkLists = await _cacheManager.GetAsync("GetLinkLists-" + workContext.CurrentStore.Id, "ApiRegion", async() => { return(await _cmsApi.MenuGetListsAsync(workContext.CurrentStore.Id)); });

                    workContext.CurrentLinkLists = linkLists != null?linkLists.Select(ll => ll.ToWebModel(urlBuilder)).ToList() : null;

                    //Initialize catalog search criteria
                    workContext.CurrentCatalogSearchCriteria = GetSearchCriteria(workContext);

                    //Initialize blogs search criteria
                    //TODO: read from query string
                    workContext.CurrentBlogSearchCritera = new Model.StaticContent.BlogSearchCriteria();
                    //Pricelists
                    var priceListCachey = String.Join("-", "EvaluatePriceLists", workContext.CurrentStore.Id, workContext.CurrentCustomer.Id);
                    workContext.CurrentPriceListIds = await _cacheManager.GetAsync(priceListCachey, "ApiRegion", async() =>
                    {
                        var pricingResult = await _pricingModuleApi.PricingModuleEvaluatePriceListsAsync(
                            evalContextStoreId: workContext.CurrentStore.Id,
                            evalContextCatalogId: workContext.CurrentStore.Catalog,
                            evalContextCustomerId: workContext.CurrentCustomer.Id,
                            evalContextCurrency: workContext.CurrentCurrency.Code,
                            evalContextQuantity: 1);
                        return(pricingResult.Select(p => p.Id).ToList());
                    });
                }
            }

            await Next.Invoke(context);
        }
        public override async Task Invoke(IOwinContext context)
        {
            if (IsStorefrontRequest(context.Request))
            {
                var workContext = _container.Resolve <WorkContext>();
                var urlBuilder  = _container.Resolve <IStorefrontUrlBuilder>();

                // Initialize common properties
                workContext.RequestUrl   = context.Request.Uri;
                workContext.AllCountries = _allCountries;
                workContext.AllStores    = await _cacheManager.GetAsync("GetAllStores", "ApiRegion", async() => { return(await GetAllStoresAsync()); });

                if (workContext.AllStores != null && workContext.AllStores.Any())
                {
                    // Initialize request specific properties
                    workContext.CurrentStore    = GetStore(context, workContext.AllStores);
                    workContext.CurrentLanguage = GetLanguage(context, workContext.AllStores, workContext.CurrentStore);
                    workContext.AllCurrencies   = await _cacheManager.GetAsync("GetAllCurrencies-" + workContext.CurrentLanguage.CultureName, "ApiRegion", async() => { return((await _commerceApi.CommerceGetAllCurrenciesAsync()).Select(x => x.ToWebModel(workContext.CurrentLanguage)).ToArray()); });

                    //Sync store currencies with avail in system
                    foreach (var store in workContext.AllStores)
                    {
                        store.SyncCurrencies(workContext.AllCurrencies, workContext.CurrentLanguage);
                        store.CurrentSeoInfo = store.SeoInfos.FirstOrDefault(x => x.Language == workContext.CurrentLanguage);
                    }

                    //Set current currency
                    workContext.CurrentCurrency = GetCurrency(context, workContext.CurrentStore);

                    var qs = HttpUtility.ParseQueryString(workContext.RequestUrl.Query);
                    //Initialize catalog search criteria
                    workContext.CurrentCatalogSearchCriteria           = new CatalogSearchCriteria(qs);
                    workContext.CurrentCatalogSearchCriteria.CatalogId = workContext.CurrentStore.Catalog;
                    workContext.CurrentCatalogSearchCriteria.Currency  = workContext.CurrentCurrency;
                    workContext.CurrentCatalogSearchCriteria.Language  = workContext.CurrentLanguage;

                    workContext.CurrentOrderSearchCriteria = new Model.Order.OrderSearchCriteria(qs);
                    workContext.CurrentQuoteSearchCriteria = new Model.Quote.QuoteSearchCriteria(qs);

                    //Current customer
                    workContext.CurrentCustomer = await GetCustomerAsync(context);

                    MaintainAnonymousCustomerCookie(context, workContext);

                    //Do not load shopping cart and other for resource requests
                    if (!IsAssetRequest(context.Request.Uri))
                    {
                        //Shopping cart
                        await _cartBuilder.GetOrCreateNewTransientCartAsync(workContext.CurrentStore, workContext.CurrentCustomer, workContext.CurrentLanguage, workContext.CurrentCurrency);

                        workContext.CurrentCart = _cartBuilder.Cart;

                        if (workContext.CurrentStore.QuotesEnabled)
                        {
                            await _quoteRequestBuilder.GetOrCreateNewTransientQuoteRequestAsync(workContext.CurrentStore, workContext.CurrentCustomer, workContext.CurrentLanguage, workContext.CurrentCurrency);

                            workContext.CurrentQuoteRequest = _quoteRequestBuilder.QuoteRequest;
                        }

                        var linkLists = await _cacheManager.GetAsync("GetLinkLists-" + workContext.CurrentStore.Id, "ApiRegion", async() => { return(await _cmsApi.MenuGetListsAsync(workContext.CurrentStore.Id) ?? new List <VirtoCommerceContentWebModelsMenuLinkList>()); });

                        workContext.CurrentLinkLists = linkLists != null?linkLists.Select(ll => ll.ToWebModel(urlBuilder)).ToList() : null;


                        //Initialize blogs search criteria
                        //TODO: read from query string
                        workContext.CurrentBlogSearchCritera = new Model.StaticContent.BlogSearchCriteria(qs);

                        //Pricelists
                        var pricelistCacheKey = string.Join("-", "EvaluatePriceLists", workContext.CurrentStore.Id, workContext.CurrentCustomer.Id);
                        workContext.CurrentPricelists = await _cacheManager.GetAsync(pricelistCacheKey, "ApiRegion", async() =>
                        {
                            var evalContext = new VirtoCommerceDomainPricingModelPriceEvaluationContext
                            {
                                StoreId    = workContext.CurrentStore.Id,
                                CatalogId  = workContext.CurrentStore.Catalog,
                                CustomerId = workContext.CurrentCustomer.Id,
                                Quantity   = 1
                            };
                            var pricingResult = await _pricingModuleApi.PricingModuleEvaluatePriceListsAsync(evalContext);
                            return(pricingResult.Select(p => p.ToWebModel()).ToList());
                        });
                    }
                }
            }

            await Next.Invoke(context);
        }