Exemplo n.º 1
0
        public void LoadData()
        {
            var          url             = GLOBAL_COLLECTION_URL;
            HtmlWeb      website         = new HtmlWeb();
            HtmlDocument htmlDocument    = website.Load(url);
            var          nodesCollection = htmlDocument.DocumentNode.SelectNodes(X_PATH);
            var          nodesArray      = nodesCollection.ToArray();
            int          saved           = 0;
            int          total           = 0;
            int          exists          = 0;
            int          failed          = 0;
            int          skiped          = 0;

            foreach (HtmlNode item in nodesArray)
            {
                try
                {
                    var dataString = item.InnerText.Trim();
                    var products   = JsonConvert.DeserializeObject <List <DarazProductModel> >(dataString);
                    total = products.Count;
                    foreach (var product in products)
                    {
                        if (string.IsNullOrEmpty(product.ItemCode))
                        {
                            skiped++;
                            continue;
                        }
                        var prod = _advertisementService.GetByCode(product.ItemCode);
                        if (prod != null)
                        {
                            exists++;
                            continue;
                        }
                        var advert = new Advertisement()
                        {
                            Title  = product.ItemTitle,
                            Price  = product.ItemPrice,
                            Images = new List <Entities.Common.Image>()
                            {
                                new Entities.Common.Image()
                                {
                                    Url = product.ItemImg
                                }
                            },
                            Detail = new AdvertisementDetail()
                            {
                                ItemCode  = product.ItemCode,
                                CreatedOn = DateTime.Now,
                                ExpiredOn = DateTime.Now
                            },
                            CategoryId = 2
                        };
                        _advertisementService.Add(advert);
                        saved++;
                        Console.WriteLine("Success...");
                    }
                }
                catch (Exception ex)
                {
                    failed++;
                    Console.WriteLine("failed...");
                }
            }
            Console.WriteLine($"\n" +
                              $"Process Summary\n" +
                              $"Total   : {total}\n" +
                              $"Added   : {saved}\n" +
                              $"Failed  : {failed}\n" +
                              $"Skipped : {skiped}\n" +
                              $"Exists  : {exists}\n");
            Console.ReadKey();
        }
 public ActionResult Add(Advertisement data)
 {
     _advertisementService.Add(data);
     return(Redirect("/Admin/Saying/List"));
 }