Update Cart Quantites

Add AutoMapper and INotifyPropertyChanged to enable cart quantity changes to be reflected
This commit is contained in:
s459315 2022-07-27 14:57:33 +02:00
parent f0bea75284
commit 4a4fc708c7
8 changed files with 121 additions and 37 deletions

View File

@ -10,13 +10,5 @@ namespace RMWPFInterfaceLibrary.Models
{ {
public ProductModel Product { get; set; } public ProductModel Product { get; set; }
public int QuantityInCart { get; set; } public int QuantityInCart { get; set; }
public string DisplayText
{
get
{
return $"{Product.ProductName} ({QuantityInCart})";
}
}
} }
} }

View File

@ -1,8 +1,10 @@
using Caliburn.Micro; using AutoMapper;
using Caliburn.Micro;
using RMWPFInterfaceLibrary.Api; using RMWPFInterfaceLibrary.Api;
using RMWPFInterfaceLibrary.Helpers; using RMWPFInterfaceLibrary.Helpers;
using RMWPFInterfaceLibrary.Models; using RMWPFInterfaceLibrary.Models;
using RMWPFUserInterface.Helpers; using RMWPFUserInterface.Helpers;
using RMWPFUserInterface.Models;
using RMWPFUserInterface.ViewModels; using RMWPFUserInterface.ViewModels;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -28,6 +30,8 @@ namespace RMWPFUserInterface
} }
protected override void Configure() protected override void Configure()
{ {
_container.Instance(ConfigureAutoMapper());
_container.Instance(_container) _container.Instance(_container)
.PerRequest<IProductEndPoint, ProductEndPoint>() .PerRequest<IProductEndPoint, ProductEndPoint>()
.PerRequest<ISaleEndPoint, SaleEndPoint>(); .PerRequest<ISaleEndPoint, SaleEndPoint>();
@ -46,6 +50,20 @@ namespace RMWPFUserInterface
.ForEach(viewModelType => _container.RegisterPerRequest( .ForEach(viewModelType => _container.RegisterPerRequest(
viewModelType, viewModelType.ToString(), viewModelType)); viewModelType, viewModelType.ToString(), viewModelType));
} }
private IMapper ConfigureAutoMapper()
{
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<ProductModel, ProductDisplayModel>();
cfg.CreateMap<CartItemModel, CartItemDisplayModel>();
});
var output = config.CreateMapper();
return output;
}
protected override void OnStartup(object sender, StartupEventArgs e) protected override void OnStartup(object sender, StartupEventArgs e)
{ {
DisplayRootViewForAsync<ShellViewModel>(); DisplayRootViewForAsync<ShellViewModel>();

View File

@ -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; }
}
}

View File

@ -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));
}
}
}

View File

@ -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));
}
}
}

View File

@ -35,6 +35,9 @@
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="AutoMapper, Version=8.1.1.0, Culture=neutral, PublicKeyToken=be96cd2c38ef1005, processorArchitecture=MSIL">
<HintPath>..\packages\AutoMapper.8.1.1\lib\net461\AutoMapper.dll</HintPath>
</Reference>
<Reference Include="Caliburn.Micro.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=8e5891231f2ed21f, processorArchitecture=MSIL"> <Reference Include="Caliburn.Micro.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=8e5891231f2ed21f, processorArchitecture=MSIL">
<HintPath>..\packages\Caliburn.Micro.Core.4.0.210\lib\netstandard2.0\Caliburn.Micro.Core.dll</HintPath> <HintPath>..\packages\Caliburn.Micro.Core.4.0.210\lib\netstandard2.0\Caliburn.Micro.Core.dll</HintPath>
</Reference> </Reference>
@ -83,7 +86,8 @@
<Compile Include="BootStrapper.cs" /> <Compile Include="BootStrapper.cs" />
<Compile Include="EventModels\LogOnEvent.cs" /> <Compile Include="EventModels\LogOnEvent.cs" />
<Compile Include="Helpers\PasswordBoxHelper.cs" /> <Compile Include="Helpers\PasswordBoxHelper.cs" />
<Compile Include="Models\AuthenticatedUser.cs" /> <Compile Include="Models\CartItemDisplayModel.cs" />
<Compile Include="Models\ProductDisplayModel.cs" />
<Compile Include="ViewModels\LoginViewModel.cs" /> <Compile Include="ViewModels\LoginViewModel.cs" />
<Compile Include="ViewModels\SalesViewModel.cs" /> <Compile Include="ViewModels\SalesViewModel.cs" />
<Compile Include="ViewModels\ShellViewModel.cs" /> <Compile Include="ViewModels\ShellViewModel.cs" />
@ -146,5 +150,6 @@
<Name>RMWPFInterfaceLibrary</Name> <Name>RMWPFInterfaceLibrary</Name>
</ProjectReference> </ProjectReference>
</ItemGroup> </ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project> </Project>

View File

@ -1,7 +1,9 @@
using Caliburn.Micro; using AutoMapper;
using Caliburn.Micro;
using RMWPFInterfaceLibrary.Api; using RMWPFInterfaceLibrary.Api;
using RMWPFInterfaceLibrary.Helpers; using RMWPFInterfaceLibrary.Helpers;
using RMWPFInterfaceLibrary.Models; using RMWPFInterfaceLibrary.Models;
using RMWPFUserInterface.Models;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
@ -16,12 +18,15 @@ namespace RMWPFUserInterface.ViewModels
IProductEndPoint _productEndPoint; IProductEndPoint _productEndPoint;
IConfigHelper _configHelper; IConfigHelper _configHelper;
ISaleEndPoint _saleEndPoint; ISaleEndPoint _saleEndPoint;
IMapper _mapper;
public SalesViewModel(IProductEndPoint productEndPoint, IConfigHelper configHelper, ISaleEndPoint saleEndPoint) public SalesViewModel(IProductEndPoint productEndPoint, IConfigHelper configHelper,
ISaleEndPoint saleEndPoint, IMapper mapper)
{ {
_productEndPoint = productEndPoint; _productEndPoint = productEndPoint;
_configHelper = configHelper; _configHelper = configHelper;
_saleEndPoint = saleEndPoint; _saleEndPoint = saleEndPoint;
_mapper = mapper;
} }
protected override async void OnViewLoaded(object view) protected override async void OnViewLoaded(object view)
@ -32,13 +37,14 @@ namespace RMWPFUserInterface.ViewModels
private async Task LoadProducts() private async Task LoadProducts()
{ {
var prods = await _productEndPoint.GetAll(); var products_lsit = await _productEndPoint.GetAll();
Products = new BindingList<ProductModel>(prods); var products = _mapper.Map<List<ProductDisplayModel>>(products_lsit);
Products = new BindingList<ProductDisplayModel>(products);
} }
private BindingList<ProductModel> _products; private BindingList<ProductDisplayModel> _products;
public BindingList<ProductModel> Products public BindingList<ProductDisplayModel> Products
{ {
get { return _products; } get { return _products; }
set set
@ -48,9 +54,9 @@ namespace RMWPFUserInterface.ViewModels
} }
} }
private ProductModel _selectedProduct; private ProductDisplayModel _selectedProduct;
public ProductModel SelectedProduct public ProductDisplayModel SelectedProduct
{ {
get { return _selectedProduct; } get { return _selectedProduct; }
set set
@ -62,9 +68,9 @@ namespace RMWPFUserInterface.ViewModels
} }
private BindingList<CartItemModel> _cart = new BindingList<CartItemModel>(); private BindingList<CartItemDisplayModel> _cart = new BindingList<CartItemDisplayModel>();
public BindingList<CartItemModel> Cart public BindingList<CartItemDisplayModel> Cart
{ {
get { return _cart; } get { return _cart; }
set set
@ -152,7 +158,7 @@ namespace RMWPFUserInterface.ViewModels
} }
public void AddToCart() public void AddToCart()
{ {
CartItemModel existingItem = Cart.FirstOrDefault(x => x.Product == SelectedProduct); CartItemDisplayModel existingItem = Cart.FirstOrDefault(x => x.Product == SelectedProduct);
if (existingItem != null) if (existingItem != null)
{ {
@ -160,7 +166,7 @@ namespace RMWPFUserInterface.ViewModels
} }
else else
{ {
CartItemModel item = new CartItemModel CartItemDisplayModel item = new CartItemDisplayModel
{ {
Product = SelectedProduct, Product = SelectedProduct,
QuantityInCart = ItemQuantity QuantityInCart = ItemQuantity
@ -174,7 +180,6 @@ namespace RMWPFUserInterface.ViewModels
NotifyOfPropertyChange(() => Tax); NotifyOfPropertyChange(() => Tax);
NotifyOfPropertyChange(() => Total); NotifyOfPropertyChange(() => Total);
NotifyOfPropertyChange(() => CanCheckOut); NotifyOfPropertyChange(() => CanCheckOut);
Cart.ResetBindings();
} }
public bool CanRemoveFromCart public bool CanRemoveFromCart

View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<packages> <packages>
<package id="AutoMapper" version="8.1.1" targetFramework="net472" />
<package id="Caliburn.Micro" version="4.0.210" targetFramework="net472" /> <package id="Caliburn.Micro" version="4.0.210" targetFramework="net472" />
<package id="Caliburn.Micro.Core" version="4.0.210" targetFramework="net472" /> <package id="Caliburn.Micro.Core" version="4.0.210" targetFramework="net472" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.9" targetFramework="net472" /> <package id="Microsoft.AspNet.WebApi.Client" version="5.2.9" targetFramework="net472" />