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 linkListService = _container.Resolve <IMenuLinkListService>(); var cartBuilder = _container.Resolve <ICartBuilder>(); var catalogSearchService = _container.Resolve <ICatalogSearchService>(); // Initialize common properties workContext.RequestUrl = context.Request.Uri; workContext.AllCountries = _allCountries; workContext.AllStores = await _cacheManager.GetAsync("GetAllStores", "ApiRegion", async() => 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(workContext.CurrentLanguage, workContext.CurrentCurrency, qs) { CatalogId = workContext.CurrentStore.Catalog }; //This line make delay categories loading initialization (categories can be evaluated on view rendering time) workContext.Categories = new MutablePagedList <Category>((pageNumber, pageSize) => { var criteria = workContext.CurrentCatalogSearchCriteria.Clone(); criteria.PageNumber = pageNumber; criteria.PageSize = pageSize; var result = catalogSearchService.SearchCategories(criteria); foreach (var category in result) { category.Products = new MutablePagedList <Product>((pageNumber2, pageSize2) => { criteria.CategoryId = category.Id; criteria.PageNumber = pageNumber2; criteria.PageSize = pageSize2; var searchResult = catalogSearchService.SearchProducts(criteria); //Because catalog search products returns also aggregations we can use it to populate workContext using C# closure //now workContext.Aggregation will be contains preloaded aggregations for current category workContext.Aggregations = new MutablePagedList <Aggregation>(searchResult.Aggregations); return(searchResult.Products); }); } return(result); }); //This line make delay products loading initialization (products can be evaluated on view rendering time) workContext.Products = new MutablePagedList <Product>((pageNumber, pageSize) => { var criteria = workContext.CurrentCatalogSearchCriteria.Clone(); criteria.PageNumber = pageNumber; criteria.PageSize = pageSize; var result = catalogSearchService.SearchProducts(criteria); //Prevent double api request for get aggregations //Because catalog search products returns also aggregations we can use it to populate workContext using C# closure //now workContext.Aggregation will be contains preloaded aggregations for current search criteria workContext.Aggregations = new MutablePagedList <Aggregation>(result.Aggregations); return(result.Products); }); //This line make delay aggregation loading initialization (aggregation can be evaluated on view rendering time) workContext.Aggregations = new MutablePagedList <Aggregation>((pageNumber, pageSize) => { var criteria = workContext.CurrentCatalogSearchCriteria.Clone(); criteria.PageNumber = pageNumber; criteria.PageSize = pageSize; //Force to load products and its also populate workContext.Aggregations by preloaded values workContext.Products.Slice(pageNumber, pageSize); return(workContext.Aggregations); }); workContext.CurrentOrderSearchCriteria = new Model.Order.OrderSearchCriteria(qs); workContext.CurrentQuoteSearchCriteria = new Model.Quote.QuoteSearchCriteria(qs); //Get current customer workContext.CurrentCustomer = await GetCustomerAsync(context); //Validate that current customer has to store access ValidateUserStoreLogin(context, workContext.CurrentCustomer, workContext.CurrentStore); MaintainAnonymousCustomerCookie(context, workContext); // Gets the collection of external login providers var externalAuthTypes = context.Authentication.GetExternalAuthenticationTypes(); workContext.ExternalLoginProviders = externalAuthTypes.Select(at => new LoginProvider { AuthenticationType = at.AuthenticationType, Caption = at.Caption, Properties = at.Properties }).ToList(); workContext.ApplicationSettings = GetApplicationSettings(); //Do not load shopping cart and other for resource requests if (!IsAssetRequest(context.Request)) { //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("GetAllStoreLinkLists-" + workContext.CurrentStore.Id, "ApiRegion", async() => await linkListService.LoadAllStoreLinkListsAsync(workContext.CurrentStore.Id)); workContext.CurrentLinkLists = linkLists.Where(x => x.Language == workContext.CurrentLanguage).ToList(); // load all static content var staticContents = _cacheManager.Get(string.Join(":", "AllStoreStaticContent", workContext.CurrentStore.Id), "ContentRegion", () => { var allContentItems = _staticContentService.LoadStoreStaticContent(workContext.CurrentStore).ToList(); var blogs = allContentItems.OfType <Blog>().ToArray(); var blogArticlesGroup = allContentItems.OfType <BlogArticle>().GroupBy(x => x.BlogName, x => x).ToList(); foreach (var blog in blogs) { var blogArticles = blogArticlesGroup.FirstOrDefault(x => string.Equals(x.Key, blog.Name, StringComparison.OrdinalIgnoreCase)); if (blogArticles != null) { blog.Articles = new MutablePagedList <BlogArticle>(blogArticles); } } return(new { Pages = allContentItems, Blogs = blogs }); }); workContext.Pages = new MutablePagedList <ContentItem>(staticContents.Pages); workContext.Blogs = new MutablePagedList <Blog>(staticContents.Blogs); // Initialize blogs search criteria workContext.CurrentBlogSearchCritera = new 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); }
public override async Task Invoke(IOwinContext context) { if (IsStorefrontRequest(context.Request)) { var workContext = _container.Resolve <WorkContext>(); var urlBuilder = _container.Resolve <IStorefrontUrlBuilder>(); var linkListService = _container.Resolve <IMenuLinkListService>(); var cartBuilder = _container.Resolve <ICartBuilder>(); // 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); //Get current customer workContext.CurrentCustomer = await GetCustomerAsync(context); //Validate that current customer has to store access ValidateUserStoreLogin(context, workContext.CurrentCustomer, workContext.CurrentStore); MaintainAnonymousCustomerCookie(context, workContext); //Do not load shopping cart and other for resource requests if (!IsAssetRequest(context.Request)) { //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("GetAllStoreLinkLists-" + workContext.CurrentStore.Id, "ApiRegion", async() => await linkListService.LoadAllStoreLinkListsAsync(workContext.CurrentStore.Id)); workContext.CurrentLinkLists = linkLists.Where(x => x.Language == workContext.CurrentLanguage).ToList(); //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); }