71 lines
2.2 KiB
C#
71 lines
2.2 KiB
C#
using Microsoft.AspNetCore.Components;
|
|
using ShopOnline.Models.Dtos;
|
|
using ShopOnline.Web.Services;
|
|
using ShopOnline.Web.Services.Contracts;
|
|
|
|
namespace ShopOnline.Web.Pages
|
|
{
|
|
public class ProductsBase : ComponentBase
|
|
{
|
|
[Inject]
|
|
public IProductService ProductService { get; set; }
|
|
|
|
[Inject]
|
|
public IShoppingCartService ShoppingCartService { get; set; }
|
|
|
|
[Inject]
|
|
public IManageProductsLocalStorageService ManageProductsLocalStorageService { get; set; }
|
|
|
|
[Inject]
|
|
public IManageCartItemsLocalStorageService ManageCartItemsLocalStorageService { get; set; }
|
|
|
|
|
|
public IEnumerable<ProductDto> Products { get; set; }
|
|
|
|
[Inject]
|
|
public NavigationManager NavigationManager { get; set; }
|
|
|
|
public string ErrorMessage { get; set; }
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
{
|
|
try
|
|
{
|
|
await ClearLocalStorage();
|
|
|
|
Products = await ManageProductsLocalStorageService.GetCollection();
|
|
|
|
var shoppingCartItems = await ManageCartItemsLocalStorageService.GetCollection();
|
|
var totalQuantity = shoppingCartItems.Sum(i => i.Quantity);
|
|
|
|
ShoppingCartService.RaiseEventOnShoppingCartChanged(totalQuantity);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ErrorMessage = ex.Message;
|
|
}
|
|
|
|
}
|
|
|
|
protected IOrderedEnumerable<IGrouping<int, ProductDto>> GetGroupedProductsByCategory()
|
|
{
|
|
return from product in Products
|
|
group product by product.CategoryId into prodByCatGroup
|
|
orderby prodByCatGroup.Key
|
|
select prodByCatGroup;
|
|
}
|
|
|
|
protected string GetCategoryName(IGrouping<int, ProductDto> groupedProductDtos)
|
|
{
|
|
return groupedProductDtos.FirstOrDefault(pg => pg.CategoryId == groupedProductDtos.Key).CategoryName;
|
|
}
|
|
|
|
private async Task ClearLocalStorage()
|
|
{
|
|
await ManageProductsLocalStorageService.RemoveCollection();
|
|
await ManageCartItemsLocalStorageService.RemoveCollection();
|
|
}
|
|
|
|
}
|
|
}
|