Skip to content

Instantly share code, notes, and snippets.

@BrianJVarley
Last active December 13, 2015 21:39
Show Gist options
  • Save BrianJVarley/44c74ab0663abe3cacb4 to your computer and use it in GitHub Desktop.
Save BrianJVarley/44c74ab0663abe3cacb4 to your computer and use it in GitHub Desktop.
using MongoDBApp.Services;
using MongoDBApp.ViewModels;
using MongoDBApp.Views;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
namespace MongoDBApp
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
private static IDialogService dialogService = new DialogService();
private static IAuthenticationService authoService = new AuthenticationService();
private LoginViewModel loginViewModel { get; set; }
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
// Login
var login = new LoginView();
var loginVM = new LoginViewModel(dialogService, authoService);
login.DataContext = loginVM;
login.ShowDialog();
if (!login.DialogResult.GetValueOrDefault())
{
// Error is handled in login class, not here
Environment.Exit(0);
}
// If login is successful, show main application
ApplicationView app = new ApplicationView();
ApplicationViewModel context = new ApplicationViewModel();
app.DataContext = context;
app.Show();
}
}
}
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;
using MongoDBApp.Messages;
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 IDataService<CustomerModel> _customerDataService;
private IDataService<Country> _countryDataService;
private IDialogService _dialogService;
private const string NullObjectId = "000000000000000000000000";
public CustomerDetailsViewModel(IDataService<CustomerModel> customerDataService, IDataService<Country> countryDataService, IDialogService dialogService)
{
this._customerDataService = customerDataService;
this._countryDataService = countryDataService;
this._dialogService = dialogService;
Messenger.Default.Register<string>(this, OnLoggedInMessageReceived);
GetAllCustomersAsync();
IsEnabled = true;
LoadCommands();
}
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(), CanSaveCustomer);
AddCommand = new RelayCommand(AddCustomerLocal);
}
private void OnLoggedInMessageReceived(string username)
{
_dialogService.CloseDialog();
UserName = username;
}
#region Properties
public CustomerModel SelectedCustomer { get; set; }
public bool IsEnabled { get; set; }
public ObservableCollection<CustomerModel> Customers { get; set; }
public ObservableCollection<Country> Countries { get; set; }
public Boolean ButtonEnabled { get; set; }
public string UserName { get; set; }
public string Name
{
get
{
return "Customer Details";
}
}
#endregion
private bool CanModifyCustomer(object obj)
{
if (SelectedCustomer != null && SelectedCustomer.FirstName != null && SelectedCustomer.Country != null &&
SelectedCustomer.LastName != null && SelectedCustomer.Email != null
&& SelectedCustomer.Address != null && SelectedCustomer.Id.ToString() != NullObjectId)
{
return true;
}
return false;
}
private bool CanSaveCustomer(object obj)
{
if (SelectedCustomer != null && SelectedCustomer.FirstName != null && SelectedCustomer.Country != null &&
SelectedCustomer.LastName != null && SelectedCustomer.Email != null
&& SelectedCustomer.Address != null && SelectedCustomer.Id.ToString() == NullObjectId)
{
return true;
}
return false;
}
public void OnSelectedCustomerChanged()
{
Messenger.Default.Send<CustomerModel>(SelectedCustomer);
Messenger.Default.Send<ObservableCollection<CustomerModel>>(Customers);
}
#region persistence methods
private async Task GetAllCustomersAsync()
{
var customerResult = await _customerDataService.GetAllAsync();
Customers = customerResult.ToObservableCollection();
var countryResult = await _countryDataService.GetAllAsync();
Countries = countryResult.ToObservableCollection();
}
private async Task UpdateCustomerAsync(object customer) {
ButtonEnabled = true;
await Task.Run(() => _customerDataService.UpdateAsync(SelectedCustomer));
ButtonEnabled = false;
GetAllCustomersAsync();
}
private async Task DeleteCustomerAsync(object customer)
{
ButtonEnabled = true;
await Task.Run(() => _customerDataService.DeleteAsync(SelectedCustomer));
ButtonEnabled = false;
GetAllCustomersAsync();
}
private async Task SaveCustomerAsync(object customer)
{
if(Customers.Any(str => String.Compare(str.Email, SelectedCustomer.Email, true) == -1))
{
ButtonEnabled = true;
await Task.Run(() => _customerDataService.AddAsync(SelectedCustomer));
ButtonEnabled = false;
GetAllCustomersAsync();
}
return;
}
private void AddCustomerLocal(object customer)
{
ButtonEnabled = true;
//create new customer and add to data grid, set as selected customer
CustomerModel newCustomer = new CustomerModel();
Customers.Add(newCustomer);
SelectedCustomer = newCustomer;
ButtonEnabled = false;
}
#endregion
}
}
using Auth0.Windows;
using MongoDBApp.Common;
using MongoDBApp.Messages;
using MongoDBApp.Services;
using MongoDBApp.Utility;
using PropertyChanged;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Security;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows;
namespace MongoDBApp.ViewModels
{
[ImplementPropertyChanged]
class LoginViewModel : IPageViewModel
{
public string Name { get; set; }
public bool IsEnabled { get; set; }
public ICommand LoginCommand { get; set; }
private IDialogService _dialogService;
private IAuthenticationService _authService;
public LoginViewModel(IDialogService dialogService, IAuthenticationService authService)
{
this._dialogService = dialogService;
this._authService = authService;
LoadCommands();
}
public string UserName { get; set; }
public SecureString Password { get; set; }
private void LoadCommands()
{
LoginCommand = new RelayCommand(OnLogin);
}
private async void OnLogin(object obj)
{
var result = await _authService.LoginAsync(UserName, Password);
if (result)
{
System.Windows.MessageBox.Show("You are logged in");
Messenger.Default.Send<string>(UserName);
}
else
{
System.Windows.MessageBox.Show("Unknown username or password.");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment