-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCartViewModel.cs
More file actions
31 lines (24 loc) · 892 Bytes
/
CartViewModel.cs
File metadata and controls
31 lines (24 loc) · 892 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
using EmpowerPlant.Services;
using EmpowerPlant.Models;
namespace EmpowerPlant;
public partial class CartViewModel(IDataService dataService) : ObservableObject
{
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(CartCount))]
[NotifyPropertyChangedFor(nameof(SubTotal))]
[NotifyCanExecuteChangedFor(nameof(OrderCommand))]
List<Product> cart = new();
public int CartCount => Cart.Count;
public int SubTotal => Cart.Sum(x => x.Price);
[RelayCommand]
async Task Load() => this.Cart = await dataService.GetCartItems();
[RelayCommand]
async Task Remove(Product product)
{
await dataService.RemoveFromCart(product);
this.LoadCommand.Execute(null!);
}
[RelayCommand(CanExecute = nameof(CanOrder))]
Task Order() => Shell.Current.GoToAsync("OrderPage");
bool CanOrder() => this.CartCount > 0;
}