Ejemplo n.º 1
0
        //updates the prices of imported products on the database
        private void OnTimedEvent(object source, System.Timers.ElapsedEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("--- updating amazon items --- ");            //333

            //get all products from the database
            var allProducts = _productService.SearchProducts(showHidden: true);

            var   products = new List <Product>();
            var   ASINs    = new List <string>();
            Regex r        = new Regex(@"ASIN=(\w+);");

            //add all amazon products and their ASINs to Lists
            foreach (var product in allProducts)
            {
                if (product.AdminComment != null)
                {
                    Match m = r.Match(product.AdminComment);
                    if (m.Success)
                    {
                        products.Add(product);
                        ASINs.Add(m.Groups[1].ToString());
                    }
                }
            }

            //use ASINs to get information on all of the products using the Amazon API
            AmazonItem[] items = AmazonSearcher.GetItems(ASINs.ToArray());

            //if item prices have changed, update them on the database
            for (int i = 0; i < products.Count; i++)
            {
                if (items[i] == null)                 // caused if item ASIN was not found or there was a duplicate
                {
                    //System.Diagnostics.Debug.WriteLine("problem occured when updating " + products[i].Name);
                    products[i].Published    = false;
                    products[i].UpdatedOnUtc = DateTime.UtcNow;
                    _productService.UpdateProduct(products[i]);
                }
                else if (items[i].Price == 0)                 // there were no unused items or the price was unavailable
                {
                    //System.Diagnostics.Debug.WriteLine("price for item " + items[i].Title + " was null");
                    //products[i].Price = 0;
                    products[i].Published    = false;
                    products[i].UpdatedOnUtc = DateTime.UtcNow;
                    _productService.UpdateProduct(products[i]);
                }
                else if (products[i].Price != items[i].Price)                 // item was found, update price if different
                {
                    products[i].Price        = items[i].Price;
                    products[i].UpdatedOnUtc = DateTime.UtcNow;
                    _productService.UpdateProduct(products[i]);
                }
            }            //end for
        }
        public ActionResult SearchAmazon(GridCommand command, AmazonImportModel.AmazonKeywordSearchModel model)
        {
            //allow this page to been seen only after logging in
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageProducts))
            {
                return(null);
            }

            //create gridModel for returning data
            var gridModel = new GridModel();

            //search on amazon for keywords and display results
            if (model.Keywords != null)
            {
                AmazonSearchResults results = AmazonSearcher.Search(model.Keywords, command.PageSize, command.Page - 1);

                //display the results on a table
                gridModel      = new GridModel();
                gridModel.Data = results.Items.Select(x =>
                {
                    AmazonImportModel.AmazonSearchResultModel resultModel = new AmazonImportModel.AmazonSearchResultModel();
                    resultModel.ASIN             = x.ASIN;
                    resultModel.Description      = x.Description;
                    resultModel.DetailPageURL    = x.DetailPageURL;
                    resultModel.ImageUrl         = x.ImageUrl;
                    resultModel.Price            = x.Price;
                    resultModel.EditorialReviews = x.Reviews;
                    resultModel.Title            = x.Title;
                    return(resultModel);
                }
                                                      );
                gridModel.Total = System.Math.Min(50, results.TotalItems);
            }
            else             //model.keywords == null
            {
                gridModel.Total = 0;
            }

            return(new JsonResult
            {
                Data = gridModel
            });
        }
        public ActionResult CreateFromASIN(AmazonImportModel.CreateFromASINModel model)
        {
            //allow this page to been seen only after logging in
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageProducts))
            {
                return(null);
            }

            if (ModelState.IsValid)
            {
                var allASINs = GetAllAmazonProductASINs();
                if (GetAllAmazonProductASINs().Contains(model.ASIN))                 //product already exists, show error
                {
                    ModelState["ASIN"].Errors.Add("A product with this ASIN already exists");
                }
                else
                {
                    AmazonItem item = AmazonSearcher.GetItem(model.ASIN);

                    if (item != null)                    //item was found on Amazon
                    {
                        //add the created Product to the database
                        int productId = AddAmazonItem(item);

                        //open "Edit product" page for the newly created product
                        return(RedirectToAction("Edit", "Product", new { id = productId }));
                    }
                    else                     //item was not found
                    {
                        ModelState["ASIN"].Errors.Add("An item with this ASIN was not found on Amazon");
                    }
                }         //if (GetAllAmazonProductASINs().Contains(model.ASIN))
            }             //if ModelState.IsValid

            //problem occured, display the form again with an appropriate error message
            return(View("Nop.Plugin.Misc.AmazonImport.Views.MiscAmazonImport.CreateFromASIN", model));
        }
        public ActionResult CreateFromASIN(string ASIN)
        {
            //allow this page to been seen only after logging in
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageProducts))
            {
                return(null);
            }

            if (ASIN == null)             //no ASIN supplied, show form
            {
                var model = new AmazonImportModel.CreateFromASINModel();
                return(View("Nop.Plugin.Misc.AmazonImport.Views.MiscAmazonImport.CreateFromASIN", model));
            }

            //if product already exists, show error
            if (GetAllAmazonProductASINs().Contains(ASIN))
            {
                return(View("Nop.Plugin.Misc.AmazonImport.Views.MiscAmazonImport.ItemAlreadyExists"));
            }

            //search for product form amazon
            AmazonItem item = AmazonSearcher.GetItem(ASIN);

            if (item != null)             //add product to database and open the product editing form
            {
                int productId = AddAmazonItem(item);

                //open "Edit product" page for the newly created product
                return(RedirectToAction("Edit", "Product", new { id = productId }));
            }
            else             //product could no longer be found on amazon
            {
                _logger.Error("Adding product failed: product could no longer be found on Amazon");
                return(null);
            }
        }