feat: Add ClearCart command and response, implement CancelOrder command with validation, and enhance DeliveryStatus and User models
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
namespace CMSMicroservice.Application.UserCartsCQ.Commands.ClearCart;
|
||||
|
||||
/// <summary>
|
||||
/// Command برای پاک کردن تمام سبد خرید کاربر
|
||||
/// </summary>
|
||||
public record ClearCartCommand : IRequest<ClearCartResponseDto>
|
||||
{
|
||||
/// <summary>
|
||||
/// شناسه کاربر
|
||||
/// </summary>
|
||||
public long UserId { get; init; }
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
using CMSMicroservice.Domain.Events;
|
||||
|
||||
namespace CMSMicroservice.Application.UserCartsCQ.Commands.ClearCart;
|
||||
|
||||
public class ClearCartCommandHandler : IRequestHandler<ClearCartCommand, ClearCartResponseDto>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
|
||||
public ClearCartCommandHandler(IApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<ClearCartResponseDto> Handle(ClearCartCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
// پیدا کردن تمام آیتمهای سبد خرید کاربر
|
||||
var cartItems = await _context.UserCartss
|
||||
.Where(c => c.UserId == request.UserId)
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
if (!cartItems.Any())
|
||||
{
|
||||
return new ClearCartResponseDto
|
||||
{
|
||||
UserId = request.UserId,
|
||||
RemovedItemsCount = 0,
|
||||
Message = "سبد خرید خالی است"
|
||||
};
|
||||
}
|
||||
|
||||
var itemsCount = cartItems.Count;
|
||||
|
||||
// حذف تمام آیتمها
|
||||
_context.UserCartss.RemoveRange(cartItems);
|
||||
|
||||
// ثبت Event
|
||||
// میتونیم یک Event برای هر آیتم یا یک Event کلی بفرستیم
|
||||
foreach (var item in cartItems)
|
||||
{
|
||||
item.AddDomainEvent(new ClearCartEvent(item));
|
||||
}
|
||||
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new ClearCartResponseDto
|
||||
{
|
||||
UserId = request.UserId,
|
||||
RemovedItemsCount = itemsCount,
|
||||
Message = $"{itemsCount} آیتم از سبد خرید حذف شد"
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
namespace CMSMicroservice.Application.UserCartsCQ.Commands.ClearCart;
|
||||
|
||||
public class ClearCartCommandValidator : AbstractValidator<ClearCartCommand>
|
||||
{
|
||||
public ClearCartCommandValidator()
|
||||
{
|
||||
RuleFor(v => v.UserId)
|
||||
.GreaterThan(0)
|
||||
.WithMessage("شناسه کاربر باید بزرگتر از صفر باشد");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace CMSMicroservice.Application.UserCartsCQ.Commands.ClearCart;
|
||||
|
||||
public class ClearCartResponseDto
|
||||
{
|
||||
public long UserId { get; set; }
|
||||
public int RemovedItemsCount { get; set; }
|
||||
public string Message { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using CMSMicroservice.Domain.Events;
|
||||
|
||||
namespace CMSMicroservice.Application.UserCartsCQ.EventHandlers.ClearCartEventHandlers;
|
||||
|
||||
public class ClearCartEventHandler : INotificationHandler<ClearCartEvent>
|
||||
{
|
||||
private readonly ILogger<ClearCartEventHandler> _logger;
|
||||
|
||||
public ClearCartEventHandler(ILogger<ClearCartEventHandler> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public Task Handle(ClearCartEvent notification, CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogInformation("Cart item {CartId} removed for user {UserId}",
|
||||
notification.Item.Id,
|
||||
notification.Item.UserId);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user