public async Task <Guid> AddUser(User user) { //validate model if (!user.IsValid()) { throw new ArgumentException("invalid user", "user"); } if (_context.Users.Any(u => u.Email == user.Email)) { throw new ArgumentException("already exists", "email"); //todo: custom exception } user.CreatedOn = DateTime.Now; var entity = (await _context.Users.AddAsync(user)).Entity; await _context.SaveChangesAsync(); return(entity.Id); }
public async void ProductScrape() { string price = "7.250 TL"; string price1 = "100 TL"; string price2 = "350 TL"; string Url = "https://www.arabam.com/ilan/rent-a-car-dan-kiralik-skoda-superb/skoda-superb-2018-dizel-otomatik-avansis-oto-kiralama/15772963"; string Url1 = "https://www.arabam.com/ilan/rent-a-car-dan-kiralik-citroen-c-elysee/dedeoglu-dan-kiralik-dizel-araclar/13058389"; string Url2 = "https://www.arabam.com/ilan/rent-a-car-dan-kiralik-bmw-3-serisi/ador-motors-bmw-320-m-plus-otomatik-gunluk-350-tl/14951347"; string XPath = "//*[@id='wrapper']/div[5]/div[3]/div/div[1]/p"; string XPath1 = "//*[@id='js-hook-for-observer-detail']/div[2]/ul/li[1]/span[2]"; string XPath2 = "//*[@id='js-hook-for-observer-detail']/div[2]/div[1]/div/span"; url = new Uri(Url); url1 = new Uri(Url1); url2 = new Uri(Url2); WebClient client = new WebClient(); client.Encoding = Encoding.UTF8; html = client.DownloadString(url); html1 = client.DownloadString(url1); html2 = client.DownloadString(url2); HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument(); document.LoadHtml(html); var firstAdsName = document.DocumentNode.SelectSingleNode(XPath).InnerText; var firstAdsNumber = document.DocumentNode.SelectSingleNode(XPath1).InnerText; var firstAdsPrice = document.DocumentNode.SelectSingleNode(XPath2).InnerText; document.LoadHtml(html1); var secondAdsName = document.DocumentNode.SelectSingleNode(XPath).InnerText; var secondAdsNumber = document.DocumentNode.SelectSingleNode(XPath1).InnerText; var secondAdsPrice = document.DocumentNode.SelectSingleNode(XPath2).InnerText; document.LoadHtml(html2); var thirdAdsName = document.DocumentNode.SelectSingleNode(XPath).InnerText; var thirdAdsNumber = document.DocumentNode.SelectSingleNode(XPath1).InnerText; var thirdAdsPrice = document.DocumentNode.SelectSingleNode(XPath2).InnerText; Product adsProduct = new Product { Name = firstAdsName, AdsNumber = Convert.ToInt32(firstAdsNumber), Price = firstAdsPrice, Date = DateTime.Now }; Product adsProduct1 = new Product { Name = secondAdsName, AdsNumber = Convert.ToInt32(secondAdsNumber), Price = secondAdsPrice, Date = DateTime.Now }; Product adsProduct2 = new Product { Name = thirdAdsName, AdsNumber = Convert.ToInt32(thirdAdsNumber), Price = thirdAdsPrice, Date = DateTime.Now }; InterviewContext context = new InterviewContext(); if (adsProduct.Price != price) { priceChange = price + "" + adsProduct.Price; await _mailer.SendEmailAsync("*****@*****.**", "Change", "ChangeAdsProduct" + priceChange); } if (adsProduct1.Price != price1) { priceChange = price + "" + adsProduct1.Price; await _mailer.SendEmailAsync("*****@*****.**", "Change", "ChangeAdsProduct" + priceChange); } if (adsProduct2.Price != price2) { priceChange = price + "" + adsProduct2.Price; await _mailer.SendEmailAsync("*****@*****.**", "Change", "ChangeAdsProduct" + priceChange); } await context.Products.AddAsync(adsProduct); await context.Products.AddAsync(adsProduct1); await context.Products.AddAsync(adsProduct2); await context.SaveChangesAsync(); }