Esempio n. 1
0
        public async Task <ActionResult <Product> > Add(
            [FromServices] IRepository <Models.Product, int> productRepository,
            [FromServices] IEntityScrapper <Models.Product> entityScrapper,
            [FromBody] Uri uri)
        {
            var absoluteUrl = uri.AbsoluteUri;

            var storedProduct = productRepository.Find(product => product.Url == absoluteUrl).FirstOrDefault();

            if (storedProduct == null)
            {
                var dateTime = DateTime.Now;
                var product  = await entityScrapper.GetAsync(absoluteUrl);

                if (product != null)
                {
                    product.Added   = dateTime;
                    product.Updated = dateTime;
                    product.Read    = dateTime;
                    var transaction = PolicyHelper.WaitAndRetry().Execute(
                        () => productRepository.BeginTransaction(IsolationLevel.Serializable));

                    productRepository.Add(product);
                    await productRepository.SaveChangesAsync();

                    await transaction.CommitAsync();

                    return(product.ToDataTransferObject(true));
                }
            }

            return(storedProduct?.ToDataTransferObject());
        }
 public InspectStoreDepartmentsScrapper(
     IBrowsingContext browsingContext,
     IEntityScrapper <Department> entityScrapper,
     HttpClient httpClient)
 {
     this.browsingContext = browsingContext;
     this.entityScrapper  = entityScrapper;
     this.httpClient      = httpClient;
 }
 public InspectDepartmentProductsScrapper(
     IBrowsingContext browsingContext,
     IEntityScrapper <Product> productScrapper,
     HttpClient httpClient)
 {
     this.browsingContext = browsingContext;
     this.productScrapper = productScrapper;
     this.httpClient      = httpClient;
 }
Esempio n. 4
0
 public ProductScrapper(
     IBrowsingContext browsingContext,
     IEntityScrapper <Store> storeScrapper,
     IEntityScrapper <Department> departmentScrapper,
     ICacheStorage <string, Product> cacheStorage,
     HttpClient webPageHttpClient)
 {
     this.browsingContext    = browsingContext;
     this.storeScrapper      = storeScrapper;
     this.departmentScrapper = departmentScrapper;
     this.cacheStorage       = cacheStorage;
     this.webPageHttpClient  = webPageHttpClient;
 }
Esempio n. 5
0
 public DepartmentScrapper(
     IBrowsingContext browsingContext,
     IEntityScrapper <Store> storeScrapper,
     ICacheStorage <string, Department> cacheStorage,
     HttpClient webPageHttpClient,
     CookieContainer cookieContainer,
     IServiceProvider serviceProvider)
 {
     this.browsingContext   = browsingContext;
     this.storeScrapper     = storeScrapper;
     this.cacheStorage      = cacheStorage;
     this.webPageHttpClient = webPageHttpClient;
     this.serviceProvider   = serviceProvider;
 }
        public async Task <ActionResult <Department> > Add(
            [FromServices] IRepository <Models.Department, int> departmentRepository,
            [FromServices] IEntityScrapper <Models.Department> departmentScrapper,
            [FromBody] Uri uri)
        {
            var absoluteUrl      = uri.AbsoluteUri;
            var storedDepartment =
                departmentRepository.Find(department => department.Url == absoluteUrl).FirstOrDefault();

            if (storedDepartment == null)
            {
                var department = await departmentScrapper.GetAsync(absoluteUrl);

                if (department != null)
                {
                    var dateTime = DateTime.Now;
                    department.Added   = dateTime;
                    department.Updated = dateTime;
                    department.Read    = dateTime;

                    var transaction = PolicyHelper.WaitAndRetry().Execute(
                        () => departmentRepository.BeginTransaction(IsolationLevel.Serializable));
                    departmentRepository.Add(department);
                    await departmentRepository.SaveChangesAsync();

                    await transaction.CommitAsync();

                    return(department.ToDataTransferObject(true));
                }
            }

            if (storedDepartment == null)
            {
                return(this.NotFound());
            }

            return(storedDepartment?.ToDataTransferObject());
        }
Esempio n. 7
0
 public StoreService(IRepository <Store, int> storesRepository, IEntityScrapper <Store> entityScrapper)
 {
     this.storesRepository = storesRepository;
     this.entityScrapper   = entityScrapper;
 }