public ConfigModel() { /// all the objects in the class have been initialized with an empty object to avoid /// throwing Object reference error even in all circumstances. /// these objects SHOULD be initialized with some default values - possibly from web.config for worst case scenarios. ConfigSettings = new List <ConfigSettingModel>(); BasketSettings = new BasketSettings(); B2BSettings = new B2BSettings(); CatalogSettings = new CatalogSettings(); DomainSettings = new DomainSettings(); OrderSettings = new OrderSettings(); SearchSettings = new SearchSettings(); SeoSettings = new SeoSettings(); ShippingSettings = new ShippingSettings(); SocialSettings = new SocialSettings(); RecommendationSettings = new RecommendationSettings(); Currencies = new List <CurrencyModel>(); ShippingCountries = new List <CountryModel>(); BillingCountries = new List <CountryModel>(); Languages = new List <LanguageModel>(); RegionalSettings = new RegionalSettings(); ReviewSettings = new List <ProductReviewSection>(); GeoLocators = new List <GeoLocatorModel>(); Snippets = new List <SnippetModel>(); FeatureToggles = new FeatureToggleSettings(); }
public JsonResult GetItemRecommendations(string itemId, List <string> recentViewedProductList, string recommedType, string modelId, int noOfItems, string userId) { var resp = new List <RecommendationResult>(); if (recommedType == RecommendationTypes.RecentView.ToString()) { if (recentViewedProductList != null && !recentViewedProductList.Any()) { return(JsonSuccess("", JsonRequestBehavior.AllowGet)); } resp = (from o in recentViewedProductList select new RecommendationResult { RecommendedItemId = o }).ToList(); } else { _recommendationSettings = _sessionContext.CurrentSiteConfig.RecommendationSettings; _recommendationClient = new RecommendationsAPI(new Uri(_recommendationSettings.ApiEndPoint)); _recommendationClient.HttpClient.DefaultRequestHeaders.Add("x-api-key", _recommendationSettings.RecommederKey); // string userId = string.Empty; string visitorId = string.Empty; if (string.IsNullOrEmpty(userId)) { userId = string.Empty; if (_sessionContext.CurrentUser != null) { userId = _sessionContext.CurrentUser.UserId.ToString(); } else { visitorId = _sessionContext.DeviceId; } } if (string.IsNullOrEmpty(modelId)) { modelId = GetRecommendationModelId(recommedType); } var modelGuId = Guid.Empty; Guid.TryParse(modelId, out modelGuId); var usageEvent = new List <UsageEvent>(); if (recommedType == RecommendationTypes.Basket.ToString()) { var basket = _basketApi.GetBasketData("")?.Result; if (basket != null && basket.LineItems != null && basket.LineItems.Any()) { itemId = string.Join(",", basket.LineItems.Select(x => x.ProductId)); } } if (!string.IsNullOrEmpty(itemId)) { if (modelGuId == Guid.Empty) { resp = _recommendationClient.Models.GetItemRecommendationsFromDefaultModel(itemId, noOfItems)?.ToList(); } else { resp = _recommendationClient.Models.GetItemRecommendations(modelGuId, itemId, noOfItems)?.ToList(); } } else { if (modelGuId == Guid.Empty) { resp = _recommendationClient.Models.GetPersonalizedRecommendationsFromDefaultModel(usageEvent, userId != string.Empty ? userId : visitorId, noOfItems)?.ToList(); } else { resp = _recommendationClient.Models.GetPersonalizedRecommendations(modelGuId, usageEvent, userId != string.Empty ? userId : visitorId, noOfItems)?.ToList(); } } } if (resp != null && resp.Count > 0) { SearchRequestModel criteria = new SearchRequestModel { Filters = new List <SearchFilter>() }; foreach (var data in resp) { var searchFilter = new SearchFilter { Key = "recordId", Value = data.RecommendedItemId }; criteria.Filters.Add(searchFilter); } var response = _productApi.GetProducts(criteria); if (response != null && response.Result != null) { return(JsonSuccess(response.Result.Results, JsonRequestBehavior.AllowGet)); } } return(JsonSuccess("", JsonRequestBehavior.AllowGet)); }