Example #1
0
        public async Task <IActionResult> PutAsync(int id, [FromBody] SaveAdvertResource resource)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState.GetErrorMessages()));
            }

            var advert = _mapper.Map <SaveAdvertResource, Advert>(resource);
            var result = await _advertService.UpdateAsync(id, advert);

            if (!result.Success)
            {
                return(BadRequest(result.Message));
            }

            var advertResource = _mapper.Map <Advert, AdvertResource>(result.Advert);

            return(Ok(advertResource));
        }
Example #2
0
        public async Task <(SaveAdvertResource, SaveCarResource)> GetCarAdData(string url)
        {
            var config   = Configuration.Default.WithDefaultLoader();
            var context  = BrowsingContext.New(config);
            var document = await context.OpenAsync(url);

            if (document.QuerySelector("title").TextContent == "craigslist | Page Not Found")
            {
                return(null, null);
            }

            SaveAdvertResource advert = new SaveAdvertResource();
            SaveCarResource    car    = new SaveCarResource();

            advert.Url = url;

            var dateTime = document.QuerySelector("time.date.timeago");

            if (dateTime != null)
            {
                advert.DatePosted = DateTime.Parse(dateTime.GetAttribute("datetime"));
            }

            var description = document.QuerySelector("#postingbody");

            if (description != null)
            {
                description.QuerySelector("div.print-information.print-qrcode-container").Remove();
                advert.Description = description.TextContent.Trim();
            }

            var price = document.QuerySelector("span.price");

            if (price != null)
            {
                car.Price = uint.Parse(price.TextContent, System.Globalization.NumberStyles.AllowThousands | System.Globalization.NumberStyles.AllowCurrencySymbol);
            }

            var attributeGroup = document.QuerySelectorAll("p.attrgroup");

            if (attributeGroup != null && attributeGroup.Length > 0)
            {
                MatchCollection matches = Regex.Matches(attributeGroup[0].TextContent, @"\d{4}");
                var             year    = uint.Parse(matches[0].ToString());
                car.Year = year;

                matches       = Regex.Matches(attributeGroup[0].TextContent, @"[^\s\d].+");
                car.MakeModel = matches[0].ToString();

                var attributes = attributeGroup[1].QuerySelectorAll("span");

                foreach (var row in attributes)
                {
                    string[] rowSplit = row.TextContent.Split(": ");
                    switch (rowSplit[0])
                    {
                    case "odometer":
                        car.Miles = uint.Parse(rowSplit[1]);
                        break;

                    case "paint color":
                        car.Color = rowSplit[1];
                        break;

                    case "condition":
                        car.Condition = rowSplit[1];
                        break;
                    }
                }
            }

            return(advert, car);
        }