public async Task SaveGiftFromArukeresoAsync(AddGiftWithUrlRequest addGiftWithUrlRequest) { try { HttpClient client = new HttpClient(); var response = await client.GetAsync(addGiftWithUrlRequest.GiftUrl); var pageContents = await response.Content.ReadAsStringAsync(); HtmlDocument pageDocument = new HtmlDocument(); pageDocument.LoadHtml(pageContents); var gift = new Gift(); gift.Name = pageDocument.DocumentNode.SelectSingleNode("(//div[contains(@class,'product-details')]//h1)").InnerText.Trim(); var priceString = pageDocument.DocumentNode.SelectSingleNode("(//span[contains(@itemprop,'lowPrice')])").Attributes["content"].Value; gift.Price = Int32.Parse(priceString.Substring(0, priceString.IndexOf("."))); gift.PhotoUrl = pageDocument.DocumentNode.SelectSingleNode("(/html[1]/body[1]/div[1]/div[2]/div[2]/div[1]/a[1]/img[1])").Attributes["src"].Value; gift.Quantity = addGiftWithUrlRequest.Quantity; gift.Event = await applicationDbContext.Events.Include(e => e.Gifts).Include(e => e.Invitations) .FirstOrDefaultAsync(e => e.EventId == addGiftWithUrlRequest.EventId); gift.GiftUrl = addGiftWithUrlRequest.GiftUrl; await applicationDbContext.Gifts.AddAsync(gift); await applicationDbContext.SaveChangesAsync(); } catch (Exception e) { } }
public async Task <IActionResult> AddWithUrl(AddGiftWithUrlRequest addGiftWithUrlRequest) { if (!await eventService.ValidateAccessAsync(addGiftWithUrlRequest.EventId, User)) { return(RedirectToAction(nameof(EventController.Show), "Event")); } if (ModelState.IsValid) { await giftService.SaveGiftFromArukeresoAsync(addGiftWithUrlRequest); return(RedirectToAction(nameof(EventController.Show), "Event", new { id = addGiftWithUrlRequest.EventId })); } return(View(addGiftWithUrlRequest)); }
public async Task <IActionResult> AddWithUrl(long id) { if (!await eventService.ValidateAccessAsync(id, User)) { return(RedirectToAction(nameof(HomeController.Index), "Home")); } AddGiftWithUrlRequest addGiftWithUrlRequest = new AddGiftWithUrlRequest() { EventId = id, Quantity = 1 }; return(View(addGiftWithUrlRequest)); }