Skip to content

Instantly share code, notes, and snippets.

@BrianJVarley
Last active December 17, 2015 22:44
Show Gist options
  • Save BrianJVarley/bc32f1c02cad82fa97ea to your computer and use it in GitHub Desktop.
Save BrianJVarley/bc32f1c02cad82fa97ea to your computer and use it in GitHub Desktop.
namespace MongoDBApp.ViewModels
{
[ImplementPropertyChanged]
public class CustomerOrdersViewModel
{
public ICommand EditCommand { get; set; }
private IDialogService _dialogService;
public CustomerOrdersViewModel(IDataService<OrderModel> orderDataService, IDialogService dialogservice)
{
this._orderDataService = orderDataService;
this._dialogService = dialogservice;
LoadCommands();
}
#region properties
public string SelectedCustomerEmail { get; set; }
public ObservableCollection<OrderModel> CustomerOrders { get; set; }
public OrderModel SelectedOrder { get; set; }
public ProductModel SelectedProduct { get; set; }
public bool IsEnabled { get; set; }
#endregion
#region methods
private void LoadCommands()
{
EditCommand = new CustomCommand(EditOrder, CanModifyOrder);
}
private bool CanModifyOrder(object obj)
{
if (SelectedOrder != null && SelectedOrder.Email != null && SelectedOrder.Date != null &&
SelectedOrder.Id != null && SelectedProduct != null )
{
return true;
}
return false;
}
private void EditOrder(object obj)
{
Messenger.Default.Send<ProductModel>(SelectedProduct);
_dialogService.ShowDetailDialog();
}
private bool CanLoadWindow(object obj)
{
return true;
}
private void WindowLoaded(object obj)
{
Messenger.Default.Register<ProductModel>(this, OnUpdateProductMessageReceived);
}
private void OnUpdateOrderMessageReceived(CustomerModel customer)
{
SelectedCustomerEmail = customer.Email;
LoadCustomerOrdersAsync(SelectedCustomerEmail);
IsEnabled = true;
}
public async Task LoadCustomerOrdersAsync(string email)
{
var ordersResult = await _orderDataService.GetAllByEmailAsync(email);
CustomerOrders = ordersResult.ToObservableCollection();
}
#endregion
}
}
using MongoDB.Bson;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MongoDBApp.Models;
using System.Threading.Tasks;
using System.ComponentModel;
using MongoDB.Bson.Serialization.Attributes;
using PropertyChanged;
namespace MongoDBApp.Models
{
[ImplementPropertyChanged]
public class OrderModel
{
[BsonId]
public ObjectId Id { get; set; }
[BsonElement("email")]
public string Email { get; set; }
[BsonElement("date")]
public BsonDateTime Date { get; set; }
[BsonElement("ship_status")]
public Boolean Status { get; set; }
[BsonElement("products")]
public List<ProductModel> Products { get; set; }
}
[ImplementPropertyChanged]
public class ProductModel
{
[BsonElement("productId")]
public string ProductId { get; set; }
[BsonElement("price")]
public float Price { get; set; }
[BsonElement("description")]
public string Description { get; set; }
[BsonElement("quantity")]
public int Quantity { get; set; }
}
}
<Window x:Class="MongoDBApp.Views.ProductView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
d:DesignHeight="300"
d:DesignWidth="300"
mc:Ignorable="d">
<ScrollViewer>
<Grid Margin="5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="150" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="10" />
<RowDefinition Height="20" />
<RowDefinition Height="30" />
<RowDefinition Height="80" />
<RowDefinition Height="30" />
<RowDefinition Height="30" />
<RowDefinition Height="30" />
<RowDefinition Height="30" />
</Grid.RowDefinitions>
<Label Grid.Row="2"
Content="Id"
FontWeight="Bold" />
<TextBox Grid.Row="2"
Grid.Column="1"
Text="{Binding SelectedProduct.ProductId,
Mode=TwoWay}" />
<Label Grid.Row="3"
VerticalAlignment="Center"
Content="Description"
FontWeight="Bold" />
<TextBox Grid.Row="3"
Grid.Column="1"
Height="75"
AcceptsReturn="True"
Text="{Binding SelectedProduct.Description,
Mode=TwoWay}" />
<Label Grid.Row="4"
Content="Price"
FontWeight="Bold" />
<TextBox Grid.Row="4"
Grid.Column="1"
Text="{Binding SelectedProduct.Price,
Mode=TwoWay}" />
<Label Grid.Row="5"
Content="Order amount"
FontWeight="Bold" />
<TextBox Grid.Row="5"
Grid.Column="1"
Text="{Binding SelectedProduct.Quantity,
Mode=TwoWay}" />
<StackPanel Grid.Row="9"
Grid.ColumnSpan="2"
Orientation="Horizontal">
<Button Margin="5"
Command="{Binding SaveCommand}"
Content="Save" />
<Button Margin="5"
Command="{Binding DeleteCommand}"
Content="Delete" />
</StackPanel>
</Grid>
</ScrollViewer>
</Window>
using MongoDBApp.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace MongoDBApp.Views
{
/// <summary>
/// Interaction logic for ProductView.xaml
/// </summary>
public partial class ProductView : Window
{
private ProductViewModel ViewModel { get; set; }
public ProductView()
{
InitializeComponent();
ViewModel = new ProductViewModel();
this.DataContext = ViewModel;
}
}
}
namespace MongoDBApp.ViewModels
{
[ImplementPropertyChanged]
public class ProductViewModel
{
public ProductViewModel()
{
Messenger.Default.Register<ProductModel>(this, OnSelectedProductReceived);
}
#region properties
public ProductModel SelectedProduct { get; set; }
#endregion
#region methods
public void OnSelectedProductReceived(ProductModel product)
{
SelectedProduct = product;
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment