Ejemplo n.º 1
0
        public async Task <IEnumerable <Product> > Search(
            [FromServices] IRepository <Models.Store, int> storeRepository,
            [FromServices] IMultiEntityScrapper <Models.Product> productsScrapper,
            string keywords)
        {
            var keywordList = keywords.Split(',', ';').Select(s => s.Trim()).Where(s => !string.IsNullOrWhiteSpace(s))
                              .ToList();
            var products = new List <Product>();

            foreach (var storedStore in storeRepository.All())
            {
                if (storedStore.IsEnabled)
                {
                    foreach (var keyword in keywordList)
                    {
                        var url = storedStore.Url.Replace(
                            "/Products?depPid=0",
                            $"/Search.aspx?keywords={keyword}&depPid=0");
                        await foreach (var product in productsScrapper.GetAsync(url, true))
                        {
                            products.Add(product.ToDataTransferObject(false, false));
                        }
                    }
                }
            }

            return(products);
        }
        public async Task <IEnumerable <Product> > GetProducts(
            [FromServices] IRepository <Models.Department, int> departmentRepository,
            [FromServices] IRepository <Models.Product, int> productRepository,
            [FromServices] IMultiEntityScrapper <Models.Product> productsScrapper,
            int id)
        {
            var products = new List <Product>();

            var storedDepartment = departmentRepository.Find(department => department.Id == id).FirstOrDefault();

            if (storedDepartment != null)
            {
                var scrappedProducts = productsScrapper.GetAsync(storedDepartment.Url);
                await foreach (var product in scrappedProducts)
                {
                    var productUrl = product.Url;
                    var stored     = productRepository.Contains(p => p.Url == productUrl);
                    products.Add(product.ToDataTransferObject(stored: stored));
                }
            }

            return(products);
        }