feat: enhance user club feature management with sorting and creation timestamps
This commit is contained in:
+40
@@ -32,6 +32,34 @@ public class
|
||||
if (user.UserCarts.Count == 0)
|
||||
throw new NotFoundException("UserCart", request.UserId);
|
||||
|
||||
// چک موجودی محصولات
|
||||
var outOfStockProducts = new List<string>();
|
||||
var insufficientStockProducts = new List<(string Title, int Requested, int Available)>();
|
||||
|
||||
foreach (var cartItem in user.UserCarts)
|
||||
{
|
||||
if (cartItem.Product.RemainingCount <= 0)
|
||||
{
|
||||
outOfStockProducts.Add(cartItem.Product.Title);
|
||||
}
|
||||
else if (cartItem.Product.RemainingCount < cartItem.Count)
|
||||
{
|
||||
insufficientStockProducts.Add((cartItem.Product.Title, cartItem.Count, cartItem.Product.RemainingCount));
|
||||
}
|
||||
}
|
||||
|
||||
if (outOfStockProducts.Any())
|
||||
{
|
||||
throw new Exception($"محصولات زیر ناموجود شدهاند: {string.Join("، ", outOfStockProducts)}");
|
||||
}
|
||||
|
||||
if (insufficientStockProducts.Any())
|
||||
{
|
||||
var messages = insufficientStockProducts.Select(p =>
|
||||
$"«{p.Title}»: درخواست {p.Requested} عدد، موجودی {p.Available} عدد");
|
||||
throw new Exception($"موجودی محصولات زیر کافی نیست: {string.Join(" | ", messages)}");
|
||||
}
|
||||
|
||||
if (user.UserCarts.Sum(s => s.Count * s.Product.Price) != request.TotalAmount)
|
||||
throw new Exception("مبلغ سفارش با مجموع سبد خرید مطابقت ندارد.");
|
||||
|
||||
@@ -100,8 +128,20 @@ public class
|
||||
OrderId = newOrder.Id
|
||||
});
|
||||
await _context.FactorDetails.AddRangeAsync(factorDetailsList, cancellationToken);
|
||||
|
||||
// کاهش موجودی محصولات و افزایش تعداد فروش
|
||||
foreach (var cartItem in user.UserCarts)
|
||||
{
|
||||
cartItem.Product.RemainingCount -= cartItem.Count;
|
||||
cartItem.Product.SaleCount += cartItem.Count;
|
||||
}
|
||||
|
||||
user.UserCarts.Clear();
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
_logger.LogInformation("Order {OrderId} completed. Stock updated for {ProductCount} products.",
|
||||
newOrder.Id, factorDetailsList.Count());
|
||||
|
||||
var finalResult = new SubmitShopBuyOrderResponseDto()
|
||||
{
|
||||
Id = newOrder.Id,
|
||||
|
||||
Reference in New Issue
Block a user