From 3fb8d0baf8b8f90aed5e4addf13125f2dbf79469 Mon Sep 17 00:00:00 2001 From: sgaiso Date: Wed, 26 Mar 2025 16:29:30 -0400 Subject: [PATCH 1/2] Finished most --- Library.eCommerce/Models/Item.cs | 3 ++ .../Services/ProductServiceProxy.cs | 6 +-- .../Services/ShoppingCartService.cs | 4 ++ Maui.eCommerce/AppShell.xaml | 4 ++ Maui.eCommerce/Maui.eCommerce.csproj | 7 ++- Maui.eCommerce/ViewModels/ProductViewModel.cs | 14 ++++++ .../ViewModels/ShoppingManagementViewModel.cs | 21 ++++++++- .../ViewModels/TotalCostViewModel.cs | 44 +++++++++++++++++++ Maui.eCommerce/Views/CheckoutView.xaml | 16 +++++++ Maui.eCommerce/Views/CheckoutView.xaml.cs | 23 ++++++++++ Maui.eCommerce/Views/ProductDetails.xaml | 7 +++ .../Views/ShoppingManagementView.xaml | 3 ++ .../Views/ShoppingManagementView.xaml.cs | 6 +++ 13 files changed, 151 insertions(+), 7 deletions(-) create mode 100644 Maui.eCommerce/ViewModels/TotalCostViewModel.cs create mode 100644 Maui.eCommerce/Views/CheckoutView.xaml create mode 100644 Maui.eCommerce/Views/CheckoutView.xaml.cs diff --git a/Library.eCommerce/Models/Item.cs b/Library.eCommerce/Models/Item.cs index cb3caf4..8495064 100644 --- a/Library.eCommerce/Models/Item.cs +++ b/Library.eCommerce/Models/Item.cs @@ -16,6 +16,8 @@ public class Item public ProductDTO Product { get; set; } public int? Quantity { get; set; } + public int Price { get; set; } + public ICommand? AddCommand { get; set; } public override string ToString() @@ -48,6 +50,7 @@ public Item(Item i) Product = new ProductDTO(i.Product); Quantity = i.Quantity; Id = i.Id; + Price = i.Price; AddCommand = new Command(DoAdd); } diff --git a/Library.eCommerce/Services/ProductServiceProxy.cs b/Library.eCommerce/Services/ProductServiceProxy.cs index a2594e3..1c1b0eb 100644 --- a/Library.eCommerce/Services/ProductServiceProxy.cs +++ b/Library.eCommerce/Services/ProductServiceProxy.cs @@ -15,9 +15,9 @@ private ProductServiceProxy() { Products = new List { - new Item{ Product = new ProductDTO{Id = 1, Name ="Product 1"}, Id = 1, Quantity = 1 }, - new Item{ Product = new ProductDTO{Id = 2, Name ="Product 2"}, Id = 2 , Quantity = 2 }, - new Item{ Product = new ProductDTO{Id = 3, Name ="Product 3"}, Id=3 , Quantity = 3 } + new Item{ Product = new ProductDTO{Id = 1, Name ="Product 1"}, Id = 1, Quantity = 1, Price = 5 }, + new Item{ Product = new ProductDTO{Id = 2, Name ="Product 2"}, Id = 2 , Quantity = 2,Price = 50 }, + new Item{ Product = new ProductDTO{Id = 3, Name ="Product 3"}, Id=3 , Quantity = 3, Price = 500 } }; } diff --git a/Library.eCommerce/Services/ShoppingCartService.cs b/Library.eCommerce/Services/ShoppingCartService.cs index c3a6f93..474f64d 100644 --- a/Library.eCommerce/Services/ShoppingCartService.cs +++ b/Library.eCommerce/Services/ShoppingCartService.cs @@ -6,6 +6,7 @@ public class ShoppingCartService { private ProductServiceProxy _prodSvc = ProductServiceProxy.Current; private List items; + public double CheckoutPrice; public List CartItems { get @@ -29,6 +30,9 @@ private ShoppingCartService() { items = new List(); } + public void ClearList() { items = new List(); } + //Sets the list to an empty, new, list + public Item? AddOrUpdate(Item item) { var existingInvItem = _prodSvc.GetById(item.Id); diff --git a/Maui.eCommerce/AppShell.xaml b/Maui.eCommerce/AppShell.xaml index 71666d7..38fae71 100644 --- a/Maui.eCommerce/AppShell.xaml +++ b/Maui.eCommerce/AppShell.xaml @@ -24,4 +24,8 @@ Title="Shop" ContentTemplate="{DataTemplate views:ShoppingManagementView}" Route="ShoppingManagement" /> + diff --git a/Maui.eCommerce/Maui.eCommerce.csproj b/Maui.eCommerce/Maui.eCommerce.csproj index a3d3c3d..d8b082c 100644 --- a/Maui.eCommerce/Maui.eCommerce.csproj +++ b/Maui.eCommerce/Maui.eCommerce.csproj @@ -57,8 +57,8 @@ - - + + @@ -67,6 +67,9 @@ + + MSBuild:Compile + MSBuild:Compile diff --git a/Maui.eCommerce/ViewModels/ProductViewModel.cs b/Maui.eCommerce/ViewModels/ProductViewModel.cs index c08fa64..60a6bb5 100644 --- a/Maui.eCommerce/ViewModels/ProductViewModel.cs +++ b/Maui.eCommerce/ViewModels/ProductViewModel.cs @@ -43,6 +43,20 @@ public int? Quantity } } + public int? Price + { + get + { + return Model?.Price; + } + set + { + if (Model != null && Model.Price != value) + { + Model.Price = value ?? 0; + } + } + } public Item? Model { get; set; } public void AddOrUpdate() diff --git a/Maui.eCommerce/ViewModels/ShoppingManagementViewModel.cs b/Maui.eCommerce/ViewModels/ShoppingManagementViewModel.cs index 0d39f8e..b5e35e5 100644 --- a/Maui.eCommerce/ViewModels/ShoppingManagementViewModel.cs +++ b/Maui.eCommerce/ViewModels/ShoppingManagementViewModel.cs @@ -13,8 +13,8 @@ namespace Maui.eCommerce.ViewModels { public class ShoppingManagementViewModel : INotifyPropertyChanged { - private ProductServiceProxy _invSvc = ProductServiceProxy.Current; - private ShoppingCartService _cartSvc = ShoppingCartService.Current; + private ProductServiceProxy _invSvc = ProductServiceProxy.Current; + private ShoppingCartService _cartSvc = ShoppingCartService.Current; public Item? SelectedItem { get; set; } public Item? SelectedCartItem { get; set; } @@ -84,5 +84,22 @@ public void ReturnItem() } } } + //Do stuff on list, then the next page will be a readout + public void Checkout() + { + int totalCost = 0; + foreach (var item in ShoppingCart) + { + totalCost += item?.Price ?? 0; + } + _cartSvc.ClearList(); + + if (totalCost > 0) + { + NotifyPropertyChanged(nameof(ShoppingCart)); + } + _cartSvc.CheckoutPrice = totalCost; + NotifyPropertyChanged(nameof(_cartSvc.CheckoutPrice)); + } } } diff --git a/Maui.eCommerce/ViewModels/TotalCostViewModel.cs b/Maui.eCommerce/ViewModels/TotalCostViewModel.cs new file mode 100644 index 0000000..d32dca8 --- /dev/null +++ b/Maui.eCommerce/ViewModels/TotalCostViewModel.cs @@ -0,0 +1,44 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Runtime.CompilerServices; +using System.Text; +using System.Threading.Tasks; +using Library.eCommerce.Services; + +namespace Maui.eCommerce.ViewModels +{ + public class TotalCostViewModel : INotifyPropertyChanged + { + private ShoppingCartService __svc = ShoppingCartService.Current; + + public event PropertyChangedEventHandler? PropertyChanged; + + public double CheckoutCost + { + get { + return Math.Round(__svc.CheckoutPrice + (__svc.CheckoutPrice * .07), 2); + } + set + { + __svc.CheckoutPrice = value; + } + } + + public void NotifyPropertyChanged([CallerMemberName] string propertyName = "") + { + if (propertyName is null) + { + throw new ArgumentNullException(nameof(propertyName)); + } + + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); + } + + public void RefreshUI() + { + NotifyPropertyChanged(nameof(CheckoutCost)); + } + } +} diff --git a/Maui.eCommerce/Views/CheckoutView.xaml b/Maui.eCommerce/Views/CheckoutView.xaml new file mode 100644 index 0000000..e7d67f7 --- /dev/null +++ b/Maui.eCommerce/Views/CheckoutView.xaml @@ -0,0 +1,16 @@ + + + +