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 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.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<IProductEndPoint, ProductEndPoint>()
.PerRequest<ISaleEndPoint, SaleEndPoint>();
@ -46,6 +50,20 @@ namespace RMWPFUserInterface
.ForEach(viewModelType => _container.RegisterPerRequest(
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)
{
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>
</PropertyGroup>
<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">
<HintPath>..\packages\Caliburn.Micro.Core.4.0.210\lib\netstandard2.0\Caliburn.Micro.Core.dll</HintPath>
</Reference>
@ -83,7 +86,8 @@
<Compile Include="BootStrapper.cs" />
<Compile Include="EventModels\LogOnEvent.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\SalesViewModel.cs" />
<Compile Include="ViewModels\ShellViewModel.cs" />
@ -146,5 +150,6 @@
<Name>RMWPFInterfaceLibrary</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View File

@ -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<ProductModel>(prods);
var products_lsit = await _productEndPoint.GetAll();
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; }
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<CartItemModel> _cart = new BindingList<CartItemModel>();
private BindingList<CartItemDisplayModel> _cart = new BindingList<CartItemDisplayModel>();
public BindingList<CartItemModel> Cart
public BindingList<CartItemDisplayModel> 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

View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="AutoMapper" version="8.1.1" 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="Microsoft.AspNet.WebApi.Client" version="5.2.9" targetFramework="net472" />