/// <summary>
        /// Deletes the customer.
        /// </summary>
        internal async void DeleteCustomer()
        {
            try
            {
                DateTime?result = await Task.Run(() => ClientDataAccessManager.DataAccessInstance.RemoveCustomer(this.SelectedCustomer));

                if (result != null)
                {
                    string message = string.Format("Customer {0} has been removed", this.SelectedCustomer.Name);
                    InfoDialogViewModel.ShowDialog(message, "Remove successful");
                }
                else
                {
                    string message = "Remove customer failed";
                    InfoDialogViewModel.ShowDialog(message, "Remove failed");
                }
                this.SelectedCustomer = null;
                this.GetData();
            }
            catch (Exception ex)
            {
                InfoDialogViewModel.ShowDialog(ex.Message, "Unhandled Exception");
                Log.LogException(ex, "MainViewModel.cs");
            }
        }
        /// <summary>
        /// Invokes the update customer window.
        /// </summary>
        internal void UpdateCustomer()
        {
            try
            {
                // Make a copy of the current customer to avoid directly editing the customer in the data grid.
                // The customer will be modified in the cache and in the database
                // and the grid view will be updated from the cache or the database.
                Customer c = new Customer();
                c.Copy(this.SelectedCustomer);

                AddCustomerView      addCustomerView      = new AddCustomerView();
                AddCustomerViewModel addCustomerViewModel = new AddCustomerViewModel(c);
                addCustomerViewModel.SaveCompleted += (s, e) =>
                {
                    // Get latest data after save.
                    this.SelectedCustomer = null;
                    addCustomerView.Close();
                    this.GetData();
                };
                addCustomerViewModel.Cancelled += (s, e) =>
                {
                    this.SelectedCustomer = null;
                    addCustomerView.Close();
                };
                addCustomerView.DataContext = addCustomerViewModel;
                addCustomerView.ShowDialog();
                this.SelectedCustomer = null;
            }
            catch (Exception ex)
            {
                InfoDialogViewModel.ShowDialog(ex.Message, "Unhandled Exception");
                Log.LogException(ex, "MainViewModel.cs");
            }
        }
 /// <summary>
 /// Invokes the add new customer window .
 /// </summary>
 internal void AddCustomer()
 {
     try
     {
         AddCustomerView addCustomerView = new AddCustomerView();
         // Set the date of birth to today, so it's not set to default 01/01/0001
         Customer customer = new Customer()
         {
             DateOfBirth = DateTime.Now
         };
         AddCustomerViewModel addCustomerViewModel = new AddCustomerViewModel(customer);
         addCustomerViewModel.SaveCompleted += (s, e) =>
         {
             // Get latest data after save.
             this.SelectedCustomer = null;
             addCustomerView.Close();
             this.GetData();
         };
         addCustomerViewModel.Cancelled += (s, e) =>
         {
             this.SelectedCustomer = null;
             addCustomerView.Close();
         };
         addCustomerView.DataContext = addCustomerViewModel;
         addCustomerView.ShowDialog();
     }
     catch (Exception ex)
     {
         InfoDialogViewModel.ShowDialog(ex.Message, "Unhandled Exception");
         Log.LogException(ex, "MainViewModel.cs");
     }
 }
示例#4
0
        /// <summary>
        /// Saves the customer selected on the view model.
        /// </summary>
        internal async void SaveCustomer()
        {
            try
            {
                string message;

                // There is an id so send update request.
                // New user has no Id. It will be autogenerated by the data base.
                this.BusyStart();
                if (this.selectedCustomer.Id > 0)
                {
                    // Update the customer.
                    DateTime?updateResult = await Task.Run(() =>
                                                           ClientDataAccessManager.DataAccessInstance.UpdateCustomer(this.selectedCustomer, cancelSaveOperation.Token));

                    this.BusyStop();
                    if (updateResult != null)
                    {
                        message = string.Format("Customer {0} has been updated", this.selectedCustomer.Name);
                        InfoDialogViewModel.ShowDialog(message, "Update successful");
                    }
                    else
                    {
                        message = string.Format("Customer {0} - update failed", this.selectedCustomer.Name);
                        InfoDialogViewModel.ShowDialog(message, "Update failed");
                    }
                }
                else
                {
                    // Inser a new customer.
                    DateTime?addResult = await Task.Run(() =>
                                                        ClientDataAccessManager.DataAccessInstance.AddCustomer(this.selectedCustomer, cancelSaveOperation.Token));

                    this.BusyStop();
                    if (addResult != null)
                    {
                        message = string.Format("Customer {0} has been added", this.selectedCustomer.Name);
                        InfoDialogViewModel.ShowDialog(message, "Save successful");
                    }
                    else
                    {
                        message = string.Format("Customer {0} - add operation failed", this.selectedCustomer.Name);
                        InfoDialogViewModel.ShowDialog(message, "Save failed");
                    }
                }

                this.OnSaveCompleted();
            }
            catch (OperationCanceledException e)
            {
                // Don't log the cancellation exception as it is requested by the user.
                InfoDialogViewModel.ShowDialog(e.Message, "Unhandled exception");
            }
            catch (Exception e)
            {
                InfoDialogViewModel.ShowDialog(e.Message, "Unhandled exception");
                Log.LogException(e, "AddCustomerViewModel.cs");
            }
        }
 /// <summary>
 /// Invokes the window with statistical data for the users.
 /// </summary>
 internal void ShowStats()
 {
     try
     {
         StatsView      statsView      = new StatsView();
         StatsViewModel statsViewModel = new StatsViewModel(this.customers);
         statsView.DataContext = statsViewModel;
         statsView.ShowDialog();
     }
     catch (Exception ex)
     {
         InfoDialogViewModel.ShowDialog(ex.Message, "Unhandled Exception");
         Log.LogException(ex, "MainViewModel.cs");
     }
 }
        /// <summary>
        /// Invokes the window displaying the modification history in the csv format.
        /// </summary>
        internal async void DisplayModificationHistory()
        {
            try
            {
                string modificationHistoryText = await Task.Run(() => ClientDataAccessManager.DataAccessInstance.GetModificationHistory());

                ModificationHistoryView      modificationHistoryView      = new ModificationHistoryView();
                ModificationHistoryViewModel modificationHistoryViewModel = new ModificationHistoryViewModel(modificationHistoryText);
                modificationHistoryView.DataContext = modificationHistoryViewModel;
                modificationHistoryView.ShowDialog();
            }
            catch (Exception ex)
            {
                InfoDialogViewModel.ShowDialog(ex.Message, "Unhandled Exception");
                Log.LogException(ex, "MainViewModel.cs");
            }
        }
        /// <summary>
        /// Requests data from the data base and refreshes data grid.
        /// </summary>
        internal async void GetData()
        {
            try
            {
                this.IsRequestingData = true;
                this.customersBuckup  = await Task.Run(() => ClientDataAccessManager.DataAccessInstance.GetCustomers());

                // Assignment to Customers is redundant as the filter will do it
                // But if filter is already empty it won't change the state of the
                // DataGrid therefore refresh the data grid here.
                this.Customers = null;
                this.Customers = this.customersBuckup;
                // Clear filter in case a user typed anything in.
                this.Filter           = string.Empty;
                this.IsRequestingData = false;
                // Forcing the CommandManager to raise the RequerySuggested event
                CommandManager.InvalidateRequerySuggested();
            }
            catch (Exception ex)
            {
                InfoDialogViewModel.ShowDialog(ex.Message, "Unhandled Exception");
                Log.LogException(ex, "MainViewModel.cs");
            }
        }