Last active
December 2, 2015 17:41
-
-
Save BrianJVarley/9839faf25800461c25c3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Practices.Prism.Commands; | |
using MongoDBApp.Common; | |
using MongoDBApp.Models; | |
using MongoDBApp.Services; | |
using System; | |
using System.Collections.Generic; | |
using System.Collections.ObjectModel; | |
using System.ComponentModel; | |
using System.Linq; | |
using System.Runtime.CompilerServices; | |
using System.Text; | |
using System.Threading.Tasks; | |
using MongoDBApp.Extensions; | |
using MongoDB.Bson; | |
using System.Windows.Input; | |
using MongoDBApp.Utility; | |
using PropertyChanged; | |
namespace MongoDBApp.ViewModels | |
{ | |
[ImplementPropertyChanged] | |
public class CustomerDetailsViewModel : IPageViewModel | |
{ | |
public ICommand UpdateCommand { get; set; } | |
public ICommand SaveCommand { get; set; } | |
public ICommand DeleteCommand { get; set; } | |
public ICommand AddCommand { get; set; } | |
public ICommand RefreshCommand { get; set; } | |
private ICustomerDataService _customerDataService; | |
public CustomerDetailsViewModel(ICustomerDataService customerDataService) | |
{ | |
this._customerDataService = customerDataService; | |
QueryDataFromPersistence(); | |
LoadCommands(); | |
IsEnabled = true; | |
} | |
private void LoadCommands() | |
{ | |
UpdateCommand = new CustomCommand((c) => UpdateCustomerAsync(c).FireAndLogErrors(), CanModifyCustomer); | |
DeleteCommand = new CustomCommand((c) => DeleteCustomerAsync(c).FireAndLogErrors(), CanModifyCustomer); | |
SaveCommand = new CustomCommand((c) => SaveCustomerAsync(c).FireAndLogErrors(), CanModifyCustomer); | |
AddCommand = new RelayCommand(AddCustomer); | |
//RefreshCommand = new RelayCommand(QueryDataFromPersistence()); //debugging... | |
} | |
#region Properties | |
public CustomerModel SelectedCustomer { get; set; } | |
public bool IsEnabled { get; set; } | |
public ObservableCollection<CustomerModel> Customers { get; set; } | |
public Boolean ButtonEnabled { get; set; } | |
public string Name | |
{ | |
get | |
{ | |
return "Customer Details"; | |
} | |
} | |
#endregion | |
private bool CanModifyCustomer(object obj) | |
{ | |
if (SelectedCustomer != null && SelectedCustomer.FirstName != null && | |
SelectedCustomer.LastName != null && SelectedCustomer.Email != null && SelectedCustomer.Address != null) | |
{ | |
return true; | |
} | |
return false; | |
} | |
public void OnSelectedCustomerChanged() | |
{ | |
Messenger.Default.Send<CustomerModel>(SelectedCustomer); | |
} | |
#region persistence methods | |
private void QueryDataFromPersistence() | |
{ | |
Customers = _customerDataService.GetAllCustomers().ToObservableCollection(); | |
} | |
private async Task UpdateCustomerAsync(object customer) { | |
ButtonEnabled = true; | |
await Task.Run(() => _customerDataService.UpdateCustomer(SelectedCustomer)); | |
ButtonEnabled = false; | |
QueryDataFromPersistence(); | |
} | |
private async Task DeleteCustomerAsync(object customer) | |
{ | |
ButtonEnabled = true; | |
await Task.Run(() => _customerDataService.DeleteCustomer(SelectedCustomer)); | |
ButtonEnabled = false; | |
QueryDataFromPersistence(); | |
} | |
private async Task SaveCustomerAsync(object customer) | |
{ | |
if(!Customers.Any(str => String.Compare(str.Email, SelectedCustomer.Email, true) == -1)) | |
{ | |
ButtonEnabled = true; | |
await Task.Run(() => _customerDataService.AddCustomer(SelectedCustomer)); | |
ButtonEnabled = false; | |
QueryDataFromPersistence(); | |
} | |
return; | |
} | |
private void AddCustomer(object customer) | |
{ | |
ButtonEnabled = true; | |
//create new customer and add, set as selected customer | |
CustomerModel newCustomer = new CustomerModel(); | |
Customers.Add(newCustomer); | |
SelectedCustomer = newCustomer; | |
ButtonEnabled = false; | |
} | |
#endregion | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment