private void SeedFromFile_Click(object sender, RoutedEventArgs e) { var dir = System.IO.Directory.GetParent(Environment.CurrentDirectory).ToString(); var path = System.IO.Path.GetDirectoryName(dir); var combinePath = System.IO.Path.Combine(path + "/Files/Hotels" + ".txt"); var lines = System.IO.File.ReadAllLines(combinePath); var items = new List <Hotel>(); foreach (var line in lines) { var splitLine = line.Split(';'); var name = splitLine[0]; var img = splitLine[1]; var des = splitLine[2]; var price = decimal.Parse(splitLine[3]); var place = splitLine[4]; var item = new Hotel { HotelName = name, ImageUrl = img, Price = price, Place = place, Descripiton = des }; items.Add(item); } using (var db = new PointsOfInterestContext()) { foreach (var item in items) { var existItem = db.Hotels.SingleOrDefault(x => x.HotelName == item.HotelName); if (existItem == null) { db.Hotels.Add(item); } } db.SaveChanges(); } var itemsToReturn = new List <Hotel>(); using (var db = new PointsOfInterestContext()) { itemsToReturn = db.Hotels .Where(x => (x.IsDeleted == false || x.IsDeleted == null)) .ToList(); foreach (var item in itemsToReturn) { var ratesHotel = db.Rates_Users_Hotels .Where(x => x.HotelId == item.Id).ToList(); item.Rate = this.CalculateAverageRate(ratesHotel); } } hotels.ItemsSource = itemsToReturn; hotels.Items.Refresh(); }
private void Button_Click_1(object sender, RoutedEventArgs e) { var name = HotelName.Text; var des = HotelDes.Text; var imagePath = HotelImageName.Text; var place = HotelPlace.Text; var price = HotelPrice.Text; var parsedPriceNumber = 0.00m; if (!String.IsNullOrEmpty(name) && !String.IsNullOrEmpty(des) && !String.IsNullOrEmpty(imagePath) && !String.IsNullOrEmpty(place) && !String.IsNullOrEmpty(price)) { var parsedPrice = Decimal.TryParse(price, out parsedPriceNumber); if (parsedPrice) { if (parsedPriceNumber < 0) { ErrrorMessage.Content = "Price cannot be a negative number"; } else { var imageName = ImageSaver.Save("Hotels", imagePath); var hotel = new Hotel(); hotel.HotelName = name; hotel.Descripiton = des; hotel.ImageUrl = imageName; hotel.Place = place; hotel.Price = parsedPriceNumber; using (var db = new PointsOfInterestContext()) { db.Hotels.Add(hotel); db.SaveChanges(); } HotelName.Text = ""; HotelDes.Text = ""; HotelImageName.Text = ""; HotelPlace.Text = ""; HotelPrice.Text = ""; hotels.ItemsSource = this.LoadCollectionData(); hotels.Items.Refresh(); ErrrorMessage.Content = ""; } } else { ErrrorMessage.Content = "Price must be a number"; } } else { ErrrorMessage.Content = "Name, Description, Image Name, Place and Price cannot be empty"; } }