/// <summary>
 /// Creates an instance of the AddCustomerViewModel 
 /// that displays a customer
 /// </summary>
 /// <param name="customer">The customer that is to be edited.</param>
 public AddCustomerViewModel(Customer customer)
 {
     this.SelectedCustomer = customer;
     this.SaveCommand = new SaveCommand(this);
     this.CancelCommand = new CancelCommand(this);
     this.cancelSaveOperation = new CancellationTokenSource();
 }
 public void IfUserListEmptyShowStatsCommandDoesNotExecute()
 {
     Customer customer = new Customer();
     MainViewModel viewModel = new MainViewModel();
     Assert.IsFalse(viewModel.ShowStatsCommand.CanExecute(null));
     viewModel.Customers = new List<Customer> { customer };
     Assert.IsTrue(viewModel.ShowStatsCommand.CanExecute(null));
 }
 public void IfNoUserIsSelectedUpdateCommandDoesNotExecute()
 {
     Customer customer = new Customer();
     MainViewModel viewModel = new MainViewModel();
     Assert.IsFalse(viewModel.UpdateCustomerCommand.CanExecute(null));
     viewModel.SelectedCustomer = customer;
     Assert.IsTrue(viewModel.UpdateCustomerCommand.CanExecute(null));
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Helper method to get a sample customer
        /// </summary>
        /// <returns>A sample customer</returns>
        internal static Customer GetSampleCustomer()
        {
            Customer customer = new Customer()
            {
                Category = Category.A,
                DateOfBirth = new DateTime(1985, 7, 8),
                Gender = Gender.Unknown,
                HouseNumber = "323A",
                Name = "Tom",
                AddressLineOne = "Test address",
                State = "Alaska",
                Country = "United States",
            };

            return customer;
        }
            /// <summary>
            /// Sends the customer to the database and tries to update it with the newly created id.
            /// New customer is added to the local cache.
            /// </summary>
            /// <param name="customer">The customer to be saved in the database.</param>
            /// <param name="cancellationToken">Cancellation token to cancel save.</param>
            /// <returns>If successful returns datetime of the add operation, otherwise null.</returns>
            public async Task<Nullable<DateTime>> AddCustomer(Customer customer, CancellationToken cancellationToken)
            {
                // Successful if datetiem of the operation.
                // Failed if null.
                Nullable<DateTime> result = await this.dataAccess.AddCustomer(customer, cancellationToken);

                // Add customer to the cache
                if (result != null)
                {
                    // Update time only if successful.
                    // If failed cache will need an update.
                    this.clientLastDbModification = (DateTime)result;
                    if (cachedCustomers != null)
                    {
                        cachedCustomers.Add(customer);
                    }
                }
                return result;
            }
Ejemplo n.º 6
0
        /// <summary>
        /// Retries the customers data from the database.
        /// </summary>
        /// <returns>Collection of the customers.</returns>
        public async Task<List<Customer>> GetCustomers()
        {
            try
            {
                using (CustomersContext context = new CustomersContext())
                {
                    List<CustomerDb> customersDb = await context.CustomerDbs.ToListAsync();
                    List<Customer> customers = customersDb
                        .Select(cdb =>
                        {
                            Customer c = new Customer()
                            {
                                Id = cdb.Id,
                                AddressLineOne = cdb.AddressLineOne,
                                Category = cdb.Category,
                                Country = cdb.Country,
                                DateOfBirth = cdb.DateOfBirth,
                                Gender = cdb.Gender,
                                HouseNumber = cdb.HouseNumber,
                                Name = cdb.Name,
                                State = cdb.State,
                            };
                            return c;
                        }
                    ).ToList();

                    return customers;
                }

            }
            catch (InvalidCastException e)
            {
                DbLog.LogException(e, "DataProxy.cs");
                throw;
            }
            catch (InvalidOperationException e)
            {
                DbLog.LogException(e, "DataProxy.cs");
                throw;
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Removes the customer from the database.
        /// </summary>
        /// <param name="customer">The customer to be removed.</param>
        /// <returns>If successful returns datetime of the remove operation, otherwise null.</returns>
        public async Task<Nullable<DateTime>> RemoveCustomer(Customer customer)
        {
            try
            {
                if (customer == null)
                {
                    throw new ArgumentNullException("Null Customer cannot be updated.");
                }
                using (CustomersContext context = new CustomersContext())
                {
                    CustomerDb customerDb = context.CustomerDbs.FirstOrDefault(c => c.Id == customer.Id);

                    if (customerDb == null)
                    {
                        return null;
                    }

                    context.CustomerDbs.Remove(customerDb);

                    await context.SaveChangesAsync();
                    await EnterModificationHistory(context, customer, ModificationType.Remove);

                    this.lastDataBaseModificationTime = DateTime.Now;

                    return this.lastDataBaseModificationTime;
                }
            }
            catch (InvalidOperationException e)
            {
                DbLog.LogException(e, "DataProxy.cs");
                throw;
            }
            catch (NullReferenceException e)
            {
                DbLog.LogException(e, "DataProxy.cs");
                throw;
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Updates the customer in the database
        /// </summary>
        /// <param name="customer">The customer to be updated.</param>
        /// <param name="cancellationToken">Cancellation token to cancel save.</param>
        /// <returns>If successful returns datetime of the update operation, otherwise null.</returns>
        public async Task<Nullable<DateTime>> UpdateCustomer(Customer customer, CancellationToken cancellationToken = new CancellationToken())
        {
            try
            {
                if (customer == null)
                {
                    throw new ArgumentNullException("Null Customer cannot be updated.");
                }
                if (!customer.IsValid)
                {
                    return null;
                }
                using (CustomersContext context = new CustomersContext())
                {
                    CustomerDb customerDb = context.CustomerDbs.Find(customer.Id);

                    if (customerDb == null)
                    {
                        return null;
                    }

                    customerDb.AddressLineOne = customer.AddressLineOne;
                    customerDb.Category = customer.Category;
                    customerDb.Country = customer.Country;
                    customerDb.DateOfBirth = customer.DateOfBirth;
                    customerDb.Gender = customer.Gender;
                    customerDb.HouseNumber = customer.HouseNumber;
                    customerDb.Name = customer.Name;
                    customerDb.State = customer.State;

                    await context.SaveChangesAsync(cancellationToken);
                    if (!cancellationToken.IsCancellationRequested)
                    {
                        await EnterModificationHistory(context, customer, ModificationType.Update);
                        this.lastDataBaseModificationTime = DateTime.Now;
                        return this.lastDataBaseModificationTime;
                    }
                    return null;
                }
            }
            catch (InvalidOperationException e)
            {
                DbLog.LogException(e, "DataProxy.cs");
                throw;
            }
            catch (NullReferenceException e)
            {
                DbLog.LogException(e, "DataProxy.cs");
                throw;
            }
            catch (OperationCanceledException)
            {
                throw;
            }
            catch (Exception)
            {
                // Cancelletion exception can be wrapped in several other exceptions.
                // Instead of unwrapping it check if cancellation requested.
                if (cancellationToken.IsCancellationRequested)
                {
                    throw new OperationCanceledException();
                }
                throw;
            }
        }
Ejemplo n.º 9
0
 /// <summary>
 /// Makes a copy of all properties from the source customer to current customer
 /// </summary>
 /// <param name="sourceCustomer">The source customer to copy properties from</param>
 public void Copy(Customer sourceCustomer)
 {
     this.Id = sourceCustomer.Id;
     this.AddressLineOne = sourceCustomer.AddressLineOne;
     this.Category = sourceCustomer.Category;
     this.Country = sourceCustomer.Country;
     this.DateOfBirth = sourceCustomer.DateOfBirth;
     this.Gender = sourceCustomer.Gender;
     this.HouseNumber = sourceCustomer.HouseNumber;
     this.Name = sourceCustomer.Name;
     this.State = sourceCustomer.State;
 }
 /// <summary>
 /// Removes the customer from the database.
 /// </summary>
 /// <param name="customer">The customer to be removed.</param>
 /// <returns>If successful returns datetime of the remove operation, otherwise null.</returns>
 public new async Task<DateTime?> RemoveCustomer(Customer customer)
 {
     return await base.RemoveCustomer(customer);
 }
Ejemplo n.º 11
0
 /// <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");
     }
 }
            /// <summary>
            /// Removes the customer from the database.
            /// </summary>
            /// <param name="customer">The customer to be removed.</param>
            /// <returns>If successful returns datetime of the remove operation, otherwise null.</returns>
            public async Task<Nullable<DateTime>> RemoveCustomer(Customer customer)
            {
                // Successful if datetiem of the operation.
                // Failed if null.
                Nullable<DateTime> result = await this.dataAccess.RemoveCustomer(customer);

                // If db remove successful, remove the customer from the local cache.
                if (result != null)
                {
                    // Update time only if successful.
                    // If failed cache will need an update.
                    this.clientLastDbModification = (DateTime)result;
                    if (cachedCustomers != null &&
                        cachedCustomers.Contains(customer))
                    {
                        cachedCustomers.Remove(customer);
                    }
                }

                return result;
            }
 /// <summary>
 /// Request customer update on the database.
 /// </summary>
 /// <param name="customer">The customer to be updated.</param>
 /// <param name="cancellationToken">Cancellation token to cancel save.</param>
 /// <returns>If successful returns datetime of the update operation, otherwise null.</returns>
 public async Task<Nullable<DateTime>> UpdateCustomer(Customer customer, CancellationToken cancellationToken)
 {
     // Successful if datetiem of the operation.
     // Failed if null.
     Nullable<DateTime> result = await this.dataAccess.UpdateCustomer(customer, cancellationToken);
     // Update local cache.
     if (this.clientLastDbModification != null)
     {
         if (result != null)
         {
             // Update time only if successful.
             // If failed cache will need an update.
             this.clientLastDbModification = (DateTime)result;
             Customer cachedCustomer = cachedCustomers.FirstOrDefault(c => c.Id == customer.Id);
             if (cachedCustomer != null)
             {
                 cachedCustomer.Copy(customer);
             }
         }
     }
     return result;
 }
 public void SettingCustomerToMainViewModelTriggersPropertyChanged()
 {
     Customer customer = new Customer();
     MainViewModel viewModel = new MainViewModel();
     viewModel.PropertyChanged += (s, e) =>
     {
         Assert.AreEqual("SelectedCustomer", e.PropertyName);
         Assert.AreEqual(customer, viewModel.SelectedCustomer);
     };
     viewModel.SelectedCustomer = customer;
 }
Ejemplo n.º 15
0
        /// <summary>
        /// Enteres a modification history entry based on the submitted customer and the modification type.
        /// </summary>
        /// <param name="context">The context in which the modification entry will be performed.</param>
        /// <param name="customer">The customer object that has been modified. Original details are saved in the history table.</param>
        /// <param name="modificationType">The modofication type: Add, Remove, Update, Other</param>
        /// <returns></returns>
        private async Task EnterModificationHistory(CustomersContext context, Customer customer, ModificationType modificationType)
        {
            CustomerDbHistory customerHistory = new CustomerDbHistory()
            {
                // log modification type
                ModificationType = modificationType,
                // and customer details so it's easier to reproduce changes
                Id = customer.Id,
                AddressLineOne = customer.AddressLineOne,
                Category = customer.Category,
                Country = customer.Country,
                DateOfBirth = customer.DateOfBirth,
                Gender = customer.Gender,
                HouseNumber = customer.HouseNumber,
                Name = customer.Name,
                State = customer.State,
                ChangedOn = DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff tt")
            };

            try
            {
                context.CustomerDbHistories.Add(customerHistory);
                await context.SaveChangesAsync();
            }
            catch (Exception e)
            {
                DbLog.LogException(e, "DataProxy.cs");
            }
        }
Ejemplo n.º 16
0
        /// <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");
            }
        }
Ejemplo n.º 17
0
        /// <summary>
        /// Adds the customer to the database.
        /// </summary>
        /// <param name="customer">The customer to be added.</param>
        /// <param name="cancellationToken">Cancellation token to cancel save.</param>
        /// <returns>If successful returns datetime of the add operation, otherwise null.</returns>
        public async Task<Nullable<DateTime>> AddCustomer(Customer customer, CancellationToken cancellationToken = new CancellationToken())
        {
            try
            {
                if (customer == null)
                {
                    throw new ArgumentNullException("Null Customer cannot be added");
                }

                if (!customer.IsValid)
                {
                    return null;
                }

                using (CustomersContext context = new CustomersContext())
                {
                    CustomerDb customerDb = new CustomerDb()
                    {
                        AddressLineOne = customer.AddressLineOne,
                        Category = customer.Category,
                        Country = customer.Country,
                        DateOfBirth = customer.DateOfBirth,
                        Gender = customer.Gender,
                        HouseNumber = customer.HouseNumber,
                        Name = customer.Name,
                        State = customer.State
                    };

                    context.CustomerDbs.Add(customerDb);

                    await context.SaveChangesAsync(cancellationToken);
                    if (!cancellationToken.IsCancellationRequested)
                    {
                        // The Id is created by the database so before
                        // logging modification history entry update the customer with the new Id.
                        customer.Id = customerDb.Id;
                        await EnterModificationHistory(context, customer, ModificationType.Add);
                        this.lastDataBaseModificationTime = DateTime.Now;
                        return this.lastDataBaseModificationTime;
                    }
                    return null;
                }
            }
            catch (InvalidOperationException e)
            {
                DbLog.LogException(e, "DataProxy.cs");
                throw;
            }
            catch (NullReferenceException e)
            {
                DbLog.LogException(e, "DataProxy.cs");
                throw;
            }
            catch (OperationCanceledException)
            {
                throw;
            }
            catch (Exception)
            {
                // Cancelletion exception can be wrapped in several other exceptions.
                // Instead of unwrapping it check if cancellation requested.
                if (cancellationToken.IsCancellationRequested)
                {
                    throw new OperationCanceledException();
                }
                throw;
            }
        }
 /// <summary>
 /// Updates the customer in the database
 /// </summary>
 /// <param name="customer">The customer to be updated.</param>
 /// <param name="cancellationToken">Cancellation token to cancel update.</param>
 /// <returns>If successful returns datetime of the update operation, otherwise null.</returns>
 public new async Task<DateTime?> UpdateCustomer(Customer customer, CancellationToken cancellationToken)
 {
     return await base.UpdateCustomer(customer, cancellationToken);
 }