public static IList <AdvertItem> GetAdvertItems(string url) { List <AdvertItem> advertItems = new List <AdvertItem>(); var doc = HtmlAgilityPackHelper.GetHtmlDocumentFromURL(url); //Get div of ad collection //var adItemsDiv = doc.DocumentNode.Descendants("div") // .Where(x => x.Attributes["class"].Value == "panel search-results-page__main-ads-wrapper user-ad-collection user-ad-collection--row") // .FirstOrDefault(); // Get all the ad items var adItems = doc.DocumentNode.Descendants("a") .Where(x => x.Attributes["class"].Value == "user-ad-row link link--base-color-inherit link--hover-color-none link--no-underline") .ToList(); foreach (var item in adItems) { var advertItem = new AdvertItem { Title = GetAdvertTitle(item), Description = GetAdvertDescription(item), Price = GetAdvertPrice(item), AdvertUrl = GetAdvertUrl(item), GeneralArea = GetAdvertGeneralArea(item), Suburb = GetAdvertSuburb(item), GumtreeAdvertId = GetGumtreeAdvertId(item) }; advertItems.Add(advertItem); } return(advertItems); }
public async Task <IActionResult> CreateAdvert([FromForm] NewAdvertModel data) { if (!ModelState.IsValid) { return(Json(new BaseResponseModel() { Success = false, Error = string.Join('\n', ModelState.Values.SelectMany(x => x.Errors)) })); } var newAdvert = new AdvertItem() { Title = data.Title, Descirption = data.Description, DateCreated = DateTime.Now, Price = data.Price, CategoryId = data.CategoryId }; var identity = HttpContext.User.Identity as ClaimsIdentity; if (identity != null) { newAdvert.UserId = long.Parse(identity.FindFirst("UserId").Value); } _dbContext.AdvertItems.Add(newAdvert); if (data.Images != null) { if (data.Images.Count > 10) { return(Json(new BaseResponseModel() { Success = false, Error = "Atļauts pievienot ne vairāk ka 10 attēlus" })); } string imageStorageDir = Path.Combine(_configuration["FileStorageDirectory"], "AdvertImages"); if (!Directory.Exists(imageStorageDir)) { Directory.CreateDirectory(imageStorageDir); } foreach (var image in data.Images) { if (image.Length > 0) { string filePath = Path.Combine(imageStorageDir, Guid.NewGuid().ToString() + image.FileName); using (Stream fileStream = new FileStream(filePath, FileMode.Create)) { await image.CopyToAsync(fileStream); } _dbContext.AdvertImages.Add(new AdvertImage() { Advert = newAdvert, Path = filePath.Substring(_configuration["FileStorageDirectory"].Length) }); } } } await _dbContext.SaveChangesAsync(); return(Json(new BaseResponseModel())); }