Example #1
0
        // PUT api/ApartmentItem/5
        public HttpResponseMessage PutApartmentItem(int id, ApartmentItem apartmentitem)
        //public HttpResponseMessage PutApartmentItem(int id)
        {
            if (!ModelState.IsValid)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
            }

            if (id != apartmentitem.Id)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest));
            }

            db.Entry(apartmentitem).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.NotFound, ex));
            }

            return(Request.CreateResponse(HttpStatusCode.OK));
        }
Example #2
0
        // GET api/ApartmentItem/5
        public ApartmentItem GetApartmentItem(int id)
        {
            ApartmentItem apartmentitem = db.ApartmentItems.Find(id);

            if (apartmentitem == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            return(apartmentitem);
        }
Example #3
0
 public static void AddFavorite(ApartmentItem apartment)
 {
     if (apartment.IsFavorite)
     {
         DataBase.Delete(apartment.Id, DBTable.FavoriteApartment);
         apartment.IsFavorite = false;
     }
     else
     {
         DataBase.Insert(apartment, DBTable.FavoriteApartment);
         apartment.IsFavorite = true;
     }
 }
Example #4
0
        // POST api/ApartmentItem
        public HttpResponseMessage PostApartmentItem(ApartmentItem apartmentitem)
        {
            if (ModelState.IsValid)
            {
                db.ApartmentItems.Add(apartmentitem);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, apartmentitem);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = apartmentitem.Id }));
                return(response);
            }
            else
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
            }
        }
Example #5
0
        // DELETE api/ApartmentItem/5
        public HttpResponseMessage DeleteApartmentItem(int id)
        {
            ApartmentItem apartmentitem = db.ApartmentItems.Find(id);

            if (apartmentitem == null)
            {
                return(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            db.ApartmentItems.Remove(apartmentitem);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.NotFound, ex));
            }

            return(Request.CreateResponse(HttpStatusCode.OK, apartmentitem));
        }
Example #6
0
        public override async Task <List <ApartmentItem> > GetApartments(SearchArgumentModel argumentModel)
        {
            List <ApartmentItem> apartaments = new List <ApartmentItem>();

            if (argumentModel == null)
            {
                return(apartaments);
            }
            var url = argumentModel.SearchType == SearchType.Buy? "https://variant-nk.ru/object/search/novokuznetsk/kvartiry/prodam?page=1" : "https://variant-nk.ru/object/search/novokuznetsk/kvartiry/arenda?page=1";

            var    httpClient = new HttpClient();
            string html       = await httpClient.GetStringAsync(url);

            HtmlDocument document = new HtmlDocument();

            document.LoadHtml(html);

            var all_variants = document.DocumentNode.SelectNodes("//article[contains(@class, 'object-list')]");

            foreach (var variant in all_variants)
            {
                try
                {
                    string price = variant.SelectSingleNode(".//div[contains(@class, 'col-12 col-md-3')]").SelectSingleNode("./h4").InnerText.Trim();
                    price = Regex.Replace(price, "[^0-9]", "");
                    if (string.IsNullOrEmpty(price))
                    {
                        continue;
                    }

                    var root = variant.SelectSingleNode(".//div[contains(@class, 'col-12 col-md-7 object-to-link')]");

                    string[] info = root.SelectSingleNode("./h4").InnerText.Trim().Split(',');

                    if (info.Length != 3)
                    {
                        continue;
                    }
                    string roomCount = Regex.Replace(info[0], "[^0-9]", "");
                    if (string.IsNullOrEmpty(roomCount))
                    {
                        continue;
                    }

                    string area = Regex.Replace(info[1], "[^0-9.]", "").Replace('.', ',');
                    area = area.Remove(area.Length - 1, 1);

                    string[] floors = info[2].Split('/');

                    string floor   = Regex.Replace(floors[0], "[^0-9]", "");
                    string storeys = Regex.Replace(floors[1], "[^0-9]", "");
                    if (string.IsNullOrEmpty(floor) || string.IsNullOrEmpty(storeys))
                    {
                        continue;
                    }

                    string[] address = root.SelectSingleNode("./h5").InnerText.Trim().Split(',');

                    if (address.Length != 3)
                    {
                        continue;
                    }

                    var    district_vel = address[0].Trim().Split(' ');
                    string district     = string.Join(" ", district_vel.Where(v => v[0].ToString().ToUpper() == v[0].ToString()));
                    if (district == "Бл.Куйбышево")
                    {
                        district = "Куйбышевский";
                    }
                    string street = address[1];
                    string num    = address[2];

                    string page_url = root.GetAttributeValue("href", "");
                    string url_img  = variant.SelectSingleNode(".//img[contains(@class, 'img-responsive')]").GetAttributeValue("src", "/RieltorApp;component/Resource/newBg.jpg");

                    var apart = new ApartmentItem()
                    {
                        Area      = float.Parse(area),
                        RoomCount = int.Parse(roomCount),
                        Floor     = int.Parse(floor),
                        Storeys   = int.Parse(storeys),
                        Price     = int.Parse(price),
                        Address   = new Address()
                        {
                            Street   = street,
                            Num      = num,
                            District = district,
                        },
                        PageUrl  = "https://variant-nk.ru" + page_url,
                        ImageUrl = url_img
                    };

                    if (argumentModel.MaxPrice * 1000 >= apart.Price && argumentModel.MinPrice * 1000 <= apart.Price &&
                        argumentModel.MaxFloor >= apart.Floor && argumentModel.MinFloor <= apart.Floor &&
                        argumentModel.MaxStoreys >= apart.Storeys && argumentModel.MinStoreys <= apart.Storeys &&
                        (argumentModel.District == "Любой" || apart.Address.District.Contains(argumentModel.District)) &&
                        (argumentModel.RoomCount == RoomCount.Any || (argumentModel.RoomCount == RoomCount.Many && apart.RoomCount >= 4) || (int)argumentModel.RoomCount == apart.RoomCount))
                    {
                        apartaments.Add(apart);
                    }
                }

                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                    continue;
                }
            }
            return(apartaments);
        }
Example #7
0
        public override async Task <List <ApartmentItem> > GetApartments(SearchArgumentModel argumentModel)
        {
            List <ApartmentItem> apartaments = new List <ApartmentItem>();

            if (argumentModel == null)
            {
                return(apartaments);
            }

            var url = argumentModel.SearchType == SearchType.Buy ? "https://www.avito.ru/novokuznetsk/kvartiry/prodam-ASgBAgICAUSSA8YQ" : "https://www.avito.ru/novokuznetsk/kvartiry/sdam-ASgBAgICAUSSA8gQ";

            var    httpClient = new HttpClient();
            string html       = await httpClient.GetStringAsync(url);


            HtmlDocument document = new HtmlDocument();

            document.LoadHtml(html);

            var all_variants = document.DocumentNode.SelectNodes("//div[contains(@class, 'item item_table')]");

            foreach (var variant in all_variants)
            {
                try
                {
                    string price = variant.SelectSingleNode(".//span[contains(@class, 'snippet-price')]").InnerText.Trim();
                    price = Regex.Replace(price, "[^0-9]", "");
                    if (string.IsNullOrEmpty(price))
                    {
                        continue;
                    }

                    string[] info = variant.SelectSingleNode(".//a[contains(@class, 'snippet-link')]").InnerText.Trim().Split(',');
                    if (info.Length != 3)
                    {
                        continue;
                    }
                    string roomCount = Regex.Replace(info[0], "[^0-9]", "");
                    if (string.IsNullOrEmpty(roomCount))
                    {
                        continue;
                    }
                    string   area   = Regex.Replace(info[1], "[^0-9.]", "").Replace('.', ',');
                    string[] floors = info[2].Split('/');

                    string floor   = Regex.Replace(floors[0], "[^0-9]", "");
                    string storeys = Regex.Replace(floors[1], "[^0-9]", "");

                    string[] address = variant.SelectSingleNode(".//span[contains(@class, 'item-address__string')]").InnerText.Trim().Split(',');

                    if (address.Length < 2)
                    {
                        continue;
                    }

                    var street_vel = address[address.Length - 2].Split(' ');
                    if (street_vel.Length == 0)
                    {
                        continue;
                    }
                    string street = string.Join(" ", street_vel.Where(v => v != null && v.Length > 0 && v[0].ToString().ToUpper() == v[0].ToString()));
                    string num    = address[address.Length - 1];

                    if (variant.SelectSingleNode(".//span[contains(@class, 'item-address-georeferences')]") == null)
                    {
                        continue;
                    }

                    var    district_vel = variant.SelectSingleNode(".//span[contains(@class, 'item-address-georeferences')]").InnerText.Trim().Split(' ');
                    string district     = string.Join(" ", district_vel.Where(v => v[0].ToString().ToUpper() == v[0].ToString()));

                    string page_url = variant.SelectSingleNode(".//a[contains(@class, 'snippet-link')]").GetAttributeValue("href", "");

                    var img_src = variant.SelectSingleNode(".//img[contains(@class, 'large-picture-img')]");

                    string url_img = img_src == null ? "/RieltorApp;component/Resource/newBg.jpg" : img_src.GetAttributeValue("src", "/RieltorApp;component/Resource/newBg.jpg");

                    var apart = new ApartmentItem()
                    {
                        Area      = float.Parse(area),
                        RoomCount = int.Parse(roomCount),
                        Floor     = int.Parse(floor),
                        Storeys   = int.Parse(storeys),
                        Price     = int.Parse(price),
                        Address   = new Address()
                        {
                            Street   = street,
                            Num      = num,
                            District = district,
                        },
                        PageUrl  = "https://www.avito.ru" + page_url,
                        ImageUrl = url_img
                    };

                    if (argumentModel.MaxPrice * 1000 >= apart.Price && argumentModel.MinPrice * 1000 <= apart.Price &&
                        argumentModel.MaxArea >= apart.Area && argumentModel.MinArea <= apart.Area &&
                        argumentModel.MaxFloor >= apart.Floor && argumentModel.MinFloor <= apart.Floor &&
                        argumentModel.MaxStoreys >= apart.Storeys && argumentModel.MinStoreys <= apart.Storeys &&
                        (argumentModel.District == "Любой" || apart.Address.District.Contains(argumentModel.District) || apart.Address.District.Equals(argumentModel.District)) &&
                        (argumentModel.RoomCount == RoomCount.Any || (argumentModel.RoomCount == RoomCount.Many && apart.RoomCount >= 4) || (int)argumentModel.RoomCount == apart.RoomCount))
                    {
                        apartaments.Add(apart);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                    continue;
                }
            }
            return(apartaments);
        }