public async Task <IActionResult> OnGetAsync(string categoryName, int pageNumber)
        {
            if (!string.IsNullOrEmpty(categoryName))
            {
                ProductList = await _productApi.GetProductByCategory(categoryName);

                CategoryList     = ProductList.Select(p => p.Category).Distinct();
                SelectedCategory = categoryName;
                return(Page());
            }
            else
            {
                ProductList = await _productApi.GetProducts();

                CategoryList = ProductList.Select(p => p.Category).Distinct();
            }
            if (pageNumber > 0)
            {
                ProductList = await _productApi.GetProductByPage(pageNumber);
            }
            return(Page());
        }
        public async Task <IActionResult> OnGetAsync()
        {
            ProductList = await _productApi.GetProducts();

            return(Page());
        }
示例#3
0
        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));
        }