From 4a4fc708c7772305e06120739a92f3748aaa2230 Mon Sep 17 00:00:00 2001 From: s459315 Date: Wed, 27 Jul 2022 14:57:33 +0200 Subject: [PATCH] Update Cart Quantites Add AutoMapper and INotifyPropertyChanged to enable cart quantity changes to be reflected --- RMWPFInterfaceLibrary/Models/CartItemModel.cs | 8 ---- RMWPFUserInterface/BootStrapper.cs | 20 ++++++++- .../Models/AuthenticatedUser.cs | 14 ------- .../Models/CartItemDisplayModel.cs | 41 +++++++++++++++++++ .../Models/ProductDisplayModel.cs | 36 ++++++++++++++++ RMWPFUserInterface/RMWPFUserInterface.csproj | 7 +++- .../ViewModels/SalesViewModel.cs | 31 ++++++++------ RMWPFUserInterface/packages.config | 1 + 8 files changed, 121 insertions(+), 37 deletions(-) delete mode 100644 RMWPFUserInterface/Models/AuthenticatedUser.cs create mode 100644 RMWPFUserInterface/Models/CartItemDisplayModel.cs create mode 100644 RMWPFUserInterface/Models/ProductDisplayModel.cs diff --git a/RMWPFInterfaceLibrary/Models/CartItemModel.cs b/RMWPFInterfaceLibrary/Models/CartItemModel.cs index 80292a1..f03acda 100644 --- a/RMWPFInterfaceLibrary/Models/CartItemModel.cs +++ b/RMWPFInterfaceLibrary/Models/CartItemModel.cs @@ -10,13 +10,5 @@ namespace RMWPFInterfaceLibrary.Models { public ProductModel Product { get; set; } public int QuantityInCart { get; set; } - - public string DisplayText - { - get - { - return $"{Product.ProductName} ({QuantityInCart})"; - } - } } } diff --git a/RMWPFUserInterface/BootStrapper.cs b/RMWPFUserInterface/BootStrapper.cs index a4072b3..be8c622 100644 --- a/RMWPFUserInterface/BootStrapper.cs +++ b/RMWPFUserInterface/BootStrapper.cs @@ -1,8 +1,10 @@ -using Caliburn.Micro; +using AutoMapper; +using Caliburn.Micro; using RMWPFInterfaceLibrary.Api; using RMWPFInterfaceLibrary.Helpers; using RMWPFInterfaceLibrary.Models; using RMWPFUserInterface.Helpers; +using RMWPFUserInterface.Models; using RMWPFUserInterface.ViewModels; using System; using System.Collections.Generic; @@ -28,6 +30,8 @@ namespace RMWPFUserInterface } protected override void Configure() { + _container.Instance(ConfigureAutoMapper()); + _container.Instance(_container) .PerRequest() .PerRequest(); @@ -46,6 +50,20 @@ namespace RMWPFUserInterface .ForEach(viewModelType => _container.RegisterPerRequest( viewModelType, viewModelType.ToString(), viewModelType)); } + + private IMapper ConfigureAutoMapper() + { + var config = new MapperConfiguration(cfg => + { + cfg.CreateMap(); + cfg.CreateMap(); + }); + + var output = config.CreateMapper(); + + return output; + } + protected override void OnStartup(object sender, StartupEventArgs e) { DisplayRootViewForAsync(); diff --git a/RMWPFUserInterface/Models/AuthenticatedUser.cs b/RMWPFUserInterface/Models/AuthenticatedUser.cs deleted file mode 100644 index a91f84c..0000000 --- a/RMWPFUserInterface/Models/AuthenticatedUser.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace RMWPFUserInterface.Models -{ - public class AuthenticatedUser - { - public string Access_Token { get; set; } - public string UserName { get; set; } - } -} diff --git a/RMWPFUserInterface/Models/CartItemDisplayModel.cs b/RMWPFUserInterface/Models/CartItemDisplayModel.cs new file mode 100644 index 0000000..480567d --- /dev/null +++ b/RMWPFUserInterface/Models/CartItemDisplayModel.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RMWPFUserInterface.Models +{ + public class CartItemDisplayModel : INotifyPropertyChanged + { + public ProductDisplayModel Product { get; set; } + private int _quantityInCart; + + public int QuantityInCart + { + get { return _quantityInCart; } + set + { + _quantityInCart = value; + CallPropertyChanges(nameof(QuantityInCart)); + CallPropertyChanges(nameof(DisplayText)); + } + } + + public string DisplayText + { + get + { + return $"{Product.ProductName} ({QuantityInCart})"; + } + } + + public event PropertyChangedEventHandler PropertyChanged; + public void CallPropertyChanges(string propertyName) + { + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); + } + } +} + diff --git a/RMWPFUserInterface/Models/ProductDisplayModel.cs b/RMWPFUserInterface/Models/ProductDisplayModel.cs new file mode 100644 index 0000000..22a3e06 --- /dev/null +++ b/RMWPFUserInterface/Models/ProductDisplayModel.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RMWPFUserInterface.Models +{ + public class ProductDisplayModel : INotifyPropertyChanged + { + public int Id { get; set; } + public string ProductName { get; set; } + public string Description { get; set; } + public decimal RetailPrice { get; set; } + private int _quantityInStock; + + public int QuantityInStock + { + get { return _quantityInStock; } + set + { + _quantityInStock = value; + CallPropertyChanges(nameof(QuantityInStock)); + } + } + + public bool IsTaxable { get; set; } + + public event PropertyChangedEventHandler PropertyChanged; + public void CallPropertyChanges(string propertyName) + { + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); + } + } +} diff --git a/RMWPFUserInterface/RMWPFUserInterface.csproj b/RMWPFUserInterface/RMWPFUserInterface.csproj index 9f5201d..67826c8 100644 --- a/RMWPFUserInterface/RMWPFUserInterface.csproj +++ b/RMWPFUserInterface/RMWPFUserInterface.csproj @@ -35,6 +35,9 @@ 4 + + ..\packages\AutoMapper.8.1.1\lib\net461\AutoMapper.dll + ..\packages\Caliburn.Micro.Core.4.0.210\lib\netstandard2.0\Caliburn.Micro.Core.dll @@ -83,7 +86,8 @@ - + + @@ -146,5 +150,6 @@ RMWPFInterfaceLibrary + \ No newline at end of file diff --git a/RMWPFUserInterface/ViewModels/SalesViewModel.cs b/RMWPFUserInterface/ViewModels/SalesViewModel.cs index fbcd142..4b97ced 100644 --- a/RMWPFUserInterface/ViewModels/SalesViewModel.cs +++ b/RMWPFUserInterface/ViewModels/SalesViewModel.cs @@ -1,7 +1,9 @@ -using Caliburn.Micro; +using AutoMapper; +using Caliburn.Micro; using RMWPFInterfaceLibrary.Api; using RMWPFInterfaceLibrary.Helpers; using RMWPFInterfaceLibrary.Models; +using RMWPFUserInterface.Models; using System; using System.Collections.Generic; using System.ComponentModel; @@ -16,12 +18,15 @@ namespace RMWPFUserInterface.ViewModels IProductEndPoint _productEndPoint; IConfigHelper _configHelper; ISaleEndPoint _saleEndPoint; + IMapper _mapper; - public SalesViewModel(IProductEndPoint productEndPoint, IConfigHelper configHelper, ISaleEndPoint saleEndPoint) + public SalesViewModel(IProductEndPoint productEndPoint, IConfigHelper configHelper, + ISaleEndPoint saleEndPoint, IMapper mapper) { _productEndPoint = productEndPoint; _configHelper = configHelper; _saleEndPoint = saleEndPoint; + _mapper = mapper; } protected override async void OnViewLoaded(object view) @@ -32,13 +37,14 @@ namespace RMWPFUserInterface.ViewModels private async Task LoadProducts() { - var prods = await _productEndPoint.GetAll(); - Products = new BindingList(prods); + var products_lsit = await _productEndPoint.GetAll(); + var products = _mapper.Map>(products_lsit); + Products = new BindingList(products); } - private BindingList _products; + private BindingList _products; - public BindingList Products + public BindingList Products { get { return _products; } set @@ -48,9 +54,9 @@ namespace RMWPFUserInterface.ViewModels } } - private ProductModel _selectedProduct; + private ProductDisplayModel _selectedProduct; - public ProductModel SelectedProduct + public ProductDisplayModel SelectedProduct { get { return _selectedProduct; } set @@ -62,9 +68,9 @@ namespace RMWPFUserInterface.ViewModels } - private BindingList _cart = new BindingList(); + private BindingList _cart = new BindingList(); - public BindingList Cart + public BindingList Cart { get { return _cart; } set @@ -152,7 +158,7 @@ namespace RMWPFUserInterface.ViewModels } public void AddToCart() { - CartItemModel existingItem = Cart.FirstOrDefault(x => x.Product == SelectedProduct); + CartItemDisplayModel existingItem = Cart.FirstOrDefault(x => x.Product == SelectedProduct); if (existingItem != null) { @@ -160,7 +166,7 @@ namespace RMWPFUserInterface.ViewModels } else { - CartItemModel item = new CartItemModel + CartItemDisplayModel item = new CartItemDisplayModel { Product = SelectedProduct, QuantityInCart = ItemQuantity @@ -174,7 +180,6 @@ namespace RMWPFUserInterface.ViewModels NotifyOfPropertyChange(() => Tax); NotifyOfPropertyChange(() => Total); NotifyOfPropertyChange(() => CanCheckOut); - Cart.ResetBindings(); } public bool CanRemoveFromCart diff --git a/RMWPFUserInterface/packages.config b/RMWPFUserInterface/packages.config index 8e41d12..35db186 100644 --- a/RMWPFUserInterface/packages.config +++ b/RMWPFUserInterface/packages.config @@ -1,5 +1,6 @@  +