예제 #1
0
        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/Museums" + ".txt");
            var lines       = System.IO.File.ReadAllLines(combinePath);

            var items = new List <Museum>();

            foreach (var line in lines)
            {
                var splitLine = line.Split(';');
                var name      = splitLine[0];
                var img       = splitLine[1];
                var des       = splitLine[2];

                var item = new Museum
                {
                    MuseumName  = name,
                    ImageUrl    = img,
                    Descripiton = des
                };
                items.Add(item);
            }

            using (var db = new PointsOfInterestContext())
            {
                foreach (var item in items)
                {
                    var existItem = db.Museums.SingleOrDefault(x => x.MuseumName == item.MuseumName);

                    if (existItem == null)
                    {
                        db.Museums.Add(item);
                    }
                }

                db.SaveChanges();
            }

            var itemsToReturn = new List <Museum>();

            using (var db = new PointsOfInterestContext())
            {
                itemsToReturn = db.Museums
                                .Where(x => (x.IsDeleted == false || x.IsDeleted == null))
                                .ToList();

                foreach (var item in itemsToReturn)
                {
                    var ratesMuseums = db.Rates_Users_Museums
                                       .Where(x => x.MuseumId == item.Id).ToList();

                    item.AverageRate = this.CalculateAverageRate(ratesMuseums);
                }
            }

            museums.ItemsSource = itemsToReturn;
            museums.Items.Refresh();
        }
예제 #2
0
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            var musName   = MusName.Text;
            var musDes    = MusDes.Text;
            var imagePath = MusImageName.Text;

            if (!String.IsNullOrEmpty(musName) && !String.IsNullOrEmpty(musDes) &&
                !String.IsNullOrEmpty(imagePath))
            {
                var imageName = ImageSaver.Save("Museums", imagePath);

                var museum = new Museum();
                museum.MuseumName  = musName;
                museum.Descripiton = musDes;
                museum.ImageUrl    = imageName;

                using (var db = new PointsOfInterestContext())
                {
                    db.Museums.Add(museum);

                    db.SaveChanges();
                }

                MusName.Text      = "";
                MusDes.Text       = "";
                MusImageName.Text = "";

                museums.ItemsSource = this.LoadCollectionData();
                museums.Items.Refresh();

                ErrrorMessage.Content = "";
            }
            else
            {
                ErrrorMessage.Content = "Name, Description, Image Name cannot be empty";
            }
        }