示例#1
0
        public void DeleteCollection(object param)
        {
            try
            {
                using (var db = new AliContext())
                {
                    // Select group and items for deletion
                    var gs = db.Groups.Where(g => g.Id == (Guid)param).Include(i => i.Items).ToList();

                    // Delete group
                    foreach (var g in gs)
                    {
                        db.Groups.Remove(g);
                    }
                    db.SaveChanges();

                    // Delete group from datagrid
                    // Get group by id
                    var group = AliGroups.First(g => g.Id == (Guid)param);
                    // Remove current group
                    AliGroups.Remove(group);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message,
                                "Error!",
                                MessageBoxButton.OK,
                                MessageBoxImage.Error);
            }
        }
示例#2
0
        public void LoadGroups()
        {
            // Show loading animation
            LoadingAnimationModel.Visibility = Visibility.Visible;

            Application.Current.Dispatcher.Invoke((Action)(() =>
            {
                try
                {
                    // Clear collection
                    AliGroups.Clear();

                    // Get groups from db
                    using (var db = new AliContext())
                    {
                        var query = from g in db.Groups
                                    select new
                        {
                            g.Id,
                            g.Name,
                            g.Created,
                            g.Items
                        };

                        if (query != null)
                        {
                            foreach (var group in query)
                            {
                                AliGroups.Add(new AliGroup
                                {
                                    Id = group.Id,
                                    Created = group.Created,
                                    Name = group.Name,
                                    Items = group.Items
                                });
                            }
                        }

                        LoadingAnimationModel.Visibility = Visibility.Hidden;
                    }
                }
                catch (SqlException ex)
                {
                    MessageBox.Show("Unable to connect to database. \n \n" +
                                    "Check the database instance MS SQL Server LocalDB on your computer.\n \n" +
                                    ex.Message,
                                    "Error!",
                                    MessageBoxButton.OK,
                                    MessageBoxImage.Error);
                    LoadingAnimationModel.Visibility = Visibility.Hidden;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message,
                                    "Error!",
                                    MessageBoxButton.OK,
                                    MessageBoxImage.Error);
                }
            }));
        }
示例#3
0
        private void DoSaveDb(object sender, DoWorkEventArgs e)
        {
            // Show progress bar
            ProgressBar.Value      = 0;
            ProgressBar.Content    = "";
            ProgressBar.Visibility = Visibility.Visible;
            // Send current progress to status view
            OnProgress?.Invoke(ProgressBar);
            // Disable buttons
            Buttons.IsEnabled = false;
            // Show progressbar
            ProgressBar.Visibility = Visibility.Visible;

            try
            {
                using (AliContext db = new AliContext())
                {
                    var group = new AliGroupModel();
                    group.Name    = CollectionTitle;
                    group.Created = DateTime.Now;
                    group.Items   = new ObservableCollection <AliItemModel>();

                    // Add all items
                    int counter    = 0;
                    int itemsCount = AliItems.Count;
                    foreach (var item in AliItems)
                    {
                        var itemModel = new AliItemModel
                        {
                            Title         = item.Title,
                            Price         = item.Price,
                            PriceCurrency = item.PriceCurrency,
                            Unit          = item.Unit,
                            Seller        = item.Seller,
                            Link          = item.Link,
                            Description   = item.Description,
                            Image         = item.Image
                        };
                        group.Items.Add(itemModel);

                        // Set progress bar value
                        counter++;
                        int percent = (int)(Convert.ToDouble(counter) / Convert.ToDouble(itemsCount) * 100);
                        _bw1.ReportProgress(percent, String.Format("Saving item(s) {0} of {1}", counter, itemsCount));
                    }
                    db.Groups.Add(group);
                    db.SaveChanges();
                }

                // Fire database changed event to refresh main window
                OnDbChanged?.Invoke();

                // Show alert
                MessageBox.Show("Items successfully saved!",
                                "Info",
                                MessageBoxButton.OK,
                                MessageBoxImage.Information);
            }
            catch (EntityException ex)
            {
                // Show message box
                MessageBox.Show("Unable to connect to database. \n \n" +
                                "Check the database instance MS SQL Server LocalDB on your computer.\n \n" +
                                ex.Message,
                                "Error!",
                                MessageBoxButton.OK,
                                MessageBoxImage.Error);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message,
                                "Error!",
                                MessageBoxButton.OK,
                                MessageBoxImage.Error);
            }
        }