Update order system with new gRPC services and wallet integration
This commit is contained in:
@@ -1,11 +1,16 @@
|
||||
using DateTimeConverterCL;
|
||||
using FrontOffice.BFF.ShopingCart.Protobuf.Protos.ShopingCart;
|
||||
using Google.Protobuf.WellKnownTypes;
|
||||
|
||||
namespace FrontOffice.Main.Utilities;
|
||||
|
||||
public record CartItem(long ProductId, string Title, string ImageUrl, long UnitPrice, int Quantity)
|
||||
public record CartItem(long cartId,long ProductId, string Title, string ImageUrl, long UnitPrice, int Quantity)
|
||||
{
|
||||
public long LineTotal => UnitPrice * Quantity;
|
||||
public long DiscountValue => (Discount*(UnitPrice * Quantity))/100;
|
||||
public int Discount { get; init; }
|
||||
public string Created { get; init; } = string.Empty;
|
||||
public string Description { get; init; } = string.Empty;
|
||||
}
|
||||
|
||||
public class CartService
|
||||
@@ -22,30 +27,52 @@ public class CartService
|
||||
|
||||
public IReadOnlyList<CartItem> Items => _items.AsReadOnly();
|
||||
public long Total => _items.Sum(i => i.LineTotal);
|
||||
public long TotalDiscount => _items.Sum(i => i.DiscountValue);
|
||||
public int Count => _items.Sum(i => i.Quantity);
|
||||
|
||||
public async Task Add(Product product, int quantity = 1)
|
||||
{
|
||||
if (quantity <= 0) return;
|
||||
var existing = _items.FirstOrDefault(i => i.ProductId == product.Id);
|
||||
int newQuantity;
|
||||
|
||||
if (existing is null)
|
||||
{
|
||||
_items.Add(new CartItem(product.Id, product.Title, product.ImageUrl, product.Price, quantity));
|
||||
newQuantity = quantity;
|
||||
_items.Add(new CartItem(0,product.Id, product.Title, product.ImageUrl, product.Price, newQuantity)
|
||||
{
|
||||
Discount = product.Discount,
|
||||
Created = DateTime.Now.MiladiToJalali(),
|
||||
Description = product.Description
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
var idx = _items.IndexOf(existing);
|
||||
_items[idx] = existing with { Quantity = existing.Quantity + quantity };
|
||||
newQuantity = existing.Quantity + quantity;
|
||||
_items[idx] = existing with { Quantity = newQuantity };
|
||||
}
|
||||
Notify();
|
||||
|
||||
try
|
||||
{
|
||||
await _client.AddNewUserCartAsync(new AddNewUserCartRequest
|
||||
if (existing is null)
|
||||
{
|
||||
ProductId = product.Id,
|
||||
Count = quantity
|
||||
});
|
||||
await _client.AddNewUserCartAsync(new AddNewUserCartRequest
|
||||
{
|
||||
ProductId = product.Id,
|
||||
Count = newQuantity
|
||||
});
|
||||
await LoadFromServerAsync();
|
||||
}
|
||||
else
|
||||
{
|
||||
await _client.UpdateUserCartAsync(new UpdateUserCartRequest
|
||||
{
|
||||
UserCartId = existing.cartId,
|
||||
Count = newQuantity
|
||||
});
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
@@ -59,7 +86,7 @@ public class CartService
|
||||
if (existing is null) return;
|
||||
if (quantity <= 0)
|
||||
{
|
||||
Remove(productId);
|
||||
Remove(existing.cartId);
|
||||
return;
|
||||
}
|
||||
var idx = _items.IndexOf(existing);
|
||||
@@ -70,7 +97,7 @@ public class CartService
|
||||
{
|
||||
await _client.UpdateUserCartAsync(new UpdateUserCartRequest
|
||||
{
|
||||
UserCartId = productId,
|
||||
UserCartId = existing.cartId,
|
||||
Count = quantity
|
||||
});
|
||||
}
|
||||
@@ -80,9 +107,9 @@ public class CartService
|
||||
}
|
||||
}
|
||||
|
||||
public async Task Remove(long productId)
|
||||
public async Task Remove(long cartId)
|
||||
{
|
||||
_items.RemoveAll(i => i.ProductId == productId);
|
||||
_items.RemoveAll(i => i.cartId == cartId);
|
||||
Notify();
|
||||
|
||||
try
|
||||
@@ -90,7 +117,7 @@ public class CartService
|
||||
// Interpret Count=0 as remove from cart
|
||||
await _client.UpdateUserCartAsync(new UpdateUserCartRequest
|
||||
{
|
||||
UserCartId = productId,
|
||||
UserCartId = cartId,
|
||||
Count = 0
|
||||
});
|
||||
}
|
||||
@@ -131,14 +158,20 @@ public class CartService
|
||||
{
|
||||
var response = await _client.GetAllUserCartAsync(new Empty());
|
||||
_items.Clear();
|
||||
foreach (var m in response.Models)
|
||||
foreach (var model in response.Models)
|
||||
{
|
||||
var item = new CartItem(
|
||||
ProductId: m.Id,
|
||||
Title: m.Title ?? string.Empty,
|
||||
ImageUrl: string.IsNullOrWhiteSpace(m.ImagePath) ? string.Empty : UrlUtility.DownloadUrl + m.ImagePath,
|
||||
UnitPrice: m.Price,
|
||||
Quantity: m.SaleCount > 0 ? m.SaleCount : 1);
|
||||
cartId:model.Id,
|
||||
ProductId: model.ProductId,
|
||||
Title: model.ProductTitle ?? string.Empty,
|
||||
ImageUrl: string.IsNullOrWhiteSpace(model.ProductThumbnailPath) ? string.Empty : UrlUtility.DownloadUrl + model.ProductThumbnailPath,
|
||||
UnitPrice: model.ProductPrice,
|
||||
Quantity: model.Count > 0 ? model.Count : 1)
|
||||
{
|
||||
Discount = model.ProductDiscount,
|
||||
Created = model.Created.ToDateTime().MiladiToJalali(),
|
||||
Description = model.ProductShortInfomation
|
||||
};
|
||||
_items.Add(item);
|
||||
}
|
||||
Notify();
|
||||
|
||||
Reference in New Issue
Block a user