public async Task <IEnumerable <ProductViewModel> > SearchFoodProducts(string searchTagValue,
                                                                               string searchTagType = "ingredients", SearchClause searchClause = SearchClause.Contains)
        {
            var searchInput = new ProductSearchInput(searchTagType, searchClause, searchTagValue);

            return(await _openFoodFactsProxyService.SearchFoodProducts(searchInput));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Search(ProductSearchInput input)
        {
            var result = new Result <PagedResultDto <SmallProductDto> >();

            result.data = await _productAppService.SearchApi(input, new ProductSpecification(input.Keyword, input.HeadId, input.IsEnable));

            return(Json(result));
        }
Ejemplo n.º 3
0
        public void TestGetSearchWithScreenSize()
        {
            ProductSearchInput productSearchInput = new ProductSearchInput();

            productSearchInput.ScreenSize = "55";
            Task <ProductOutput> productOutput = _productService.GetSearchAsync(productSearchInput);

            productOutput.Wait();
            Assert.AreEqual(productOutput.Result.ProductElasticIndexDtoList.Count, 4);
        }
Ejemplo n.º 4
0
        public string Build(ProductSearchInput input)
        {
            string baseUrl = _options.UsOpenFoodFacts;

            QueryBuilder builder = new QueryBuilder
            {
                { "action", "process" },
                { "tagtype_0", input.CriteriaType },
                { "tag_contains_0", input.CriteriaClause.ToString().ToLower() },
                { "tag_0", input.CriteriaValue },
                { "json", "true" }
            };

            return($"{baseUrl}{builder}");
        }
Ejemplo n.º 5
0
 public void TestGetSearchException()
 {
     try
     {
         ProductSearchInput productSearchInput = new ProductSearchInput();
         productSearchInput.Brand      = string.Empty;
         productSearchInput.ScreenSize = string.Empty;
         Task <ProductOutput> productOutput = _productService.GetSearchAsync(productSearchInput);
         productOutput.Wait();
         Assert.Fail();
     }
     catch (Exception ex)
     {
         Assert.Pass();
     }
 }
Ejemplo n.º 6
0
        public ActionResult ProductSearch([FromBody] ProductSearchInput input)
        {
            Product product = (from x in dataP.Data
                               where x.Barcode != null && input.Barcode.ToUpper().Equals(x.Barcode.ToUpper())
                               select x).FirstOrDefault();

            if (product == null)
            {
                return(Json(null, JsonRequestBehavior.AllowGet));
            }

            ProductSearchOutput res = new ProductSearchOutput()
            {
                AmountDefault = product.AmountDefault,
                CategoryID    = product.CategoryID,
                Name          = product.Name,
                ProductID     = product.ProductID,
                UnitMeasureID = product.UnitMeasureID
            };

            return(Json(res, JsonRequestBehavior.AllowGet));
        }
Ejemplo n.º 7
0
        public async Task <ProductOutput> GetSearchAsync(ProductSearchInput productSearchInput)
        {
            if (string.IsNullOrEmpty(productSearchInput.Brand) && string.IsNullOrEmpty(productSearchInput.ScreenSize))
            {
                throw new InvalidArgumentException();
            }
            try
            {
                var    indexName   = ElasticSearchItemsConst.ProductIndexName;
                var    searchQuery = new Nest.SearchDescriptor <ProductElasticIndexDto>();
                string term        = "";
                if (productSearchInput.ScreenSize != null)
                {
                    term = "screensizes";
                    searchQuery
                    .Query(x => x
                           .Terms(c => c
                                  .Verbatim()
                                  .Field(p => p.ScreenSize)
                                  .Terms(productSearchInput.ScreenSize.ToLower())
                                  ))
                    .Aggregations(a => a
                                  .Terms(term, t => t
                                         .Field("screenSize.keyword")
                                         .MinimumDocumentCount(1))
                                  );
                }
                if (!String.IsNullOrEmpty(productSearchInput.Brand))
                {
                    term = "searchingArea";
                    searchQuery
                    .Query(x => x.Match(m =>
                                        m.Field(f => f.SearchingArea)
                                        .Query(productSearchInput.Brand.ToLower())
                                        .Analyzer("ngram_analyzer")
                                        )
                           )
                    .Aggregations(a => a
                                  .Terms(term, t => t
                                         .Field("screenSize.keyword")
                                         .MinimumDocumentCount(1))
                                  );;
                }

                var searchResultData = await _elasticSearchService.SimpleSearchAsync <ProductElasticIndexDto, int>(indexName, searchQuery);

                var aggregationResponse = searchResultData.Aggregations.Terms(term);


                var aggregationList = new List <Aggregation>();

                foreach (var item in aggregationResponse.Buckets)
                {
                    var aggregation = new Aggregation();
                    aggregation.Key      = item.Key;
                    aggregation.DocCount = item.DocCount;
                    aggregationList.Add(aggregation);
                }
                var productElasticIndexList = from opt in searchResultData.Documents
                                              select new ProductElasticIndexDto
                {
                    SearchingArea = opt.SearchingArea,
                    Id            = opt.Id,
                    Brand         = opt.Brand,
                    ModelName     = opt.ModelName,
                    ScreenSize    = opt.ScreenSize,
                    Price         = opt.Price,
                    Stock         = opt.Stock,
                };

                var output = new ProductOutput();
                output.AggregationList            = aggregationList;
                output.ProductElasticIndexDtoList = productElasticIndexList.ToList();
                return(await Task.FromResult(output));
            }
            catch (Exception ex)
            {
                return(await Task.FromException <ProductOutput>(ex));
            }
        }
Ejemplo n.º 8
0
        public async Task <IEnumerable <ProductViewModel> > SearchFoodProducts(ProductSearchInput input)
        {
            string url = _httpQueryBuilder.Build(input);

            var request = new HttpRequestMessage(HttpMethod.Get, url);

            // create http client for downloading the food products
            var client = _clientFactory.CreateClient();

            // timeout 20 seconds
            client.Timeout = TimeSpan.FromSeconds(20);

            try
            {
                var response = await client.SendAsync(request);

                if (!response.IsSuccessStatusCode)
                {
                    return(Enumerable.Empty <ProductViewModel>());
                }

                var responseStream = await response.Content.ReadAsStringAsync();

                var productDetails = JsonSerializer.Deserialize <FoodProductModel>(responseStream);

                // validate if the product details is null or 0 or no products
                if (!IsValid(productDetails))
                {
                    return(Enumerable.Empty <ProductViewModel>());
                }

                if (!_cache.TryGetValue("PaginationCount", out CacheSetting paginationCounter))
                {
                    // create service hit pagination counter with 1
                    CreateOrUpdateCache("PaginationCount", _cacheSettingProvider.CreateCacheSetting(int.MaxValue, 0));

                    paginationCounter = _cache.Get <CacheSetting>("PaginationCount");
                }

                // this condition to reset the counter as we reached the end of food products.
                if (productDetails.Products.Length < paginationCounter.Value)
                {
                    paginationCounter.Value = 0;
                }

                // skipping the already served foo products & taking only 5
                var foodToReturn = productDetails.Products.Skip(paginationCounter.Value).Take(5);

                // increment the pagination counter by 5
                paginationCounter.Value += 5;

                // updating the counter value
                CreateOrUpdateCache("PaginationCount", paginationCounter);

                return(_mapper.Map <List <ProductViewModel> >(foodToReturn));
            }
            catch (Exception ex)
            {
                //todo third party api is taking too much time.
                _logger.LogError(ex.Message, request);
            }
            return(Enumerable.Empty <ProductViewModel>());
        }
Ejemplo n.º 9
0
 public async Task <ProductOutput> GetSearchAsync(ProductSearchInput productSearchInput)
 {
     return(await Task.FromResult(_productService.GetSearchAsync(productSearchInput).Result));
 }
Ejemplo n.º 10
0
        public async Task <JsonResult> Search(ProductSearchInput input)
        {
            var paged = await _productAppService.Search <ProductDto>(input, new ProductSpecification(input.Keyword, input.HeadId, input.IsEnable));

            return(Json(new { code = 0, count = paged.TotalCount, data = paged.Items }));
        }