Files
CMS/src/CMSMicroservice.Application/DiscountShopCQ/Commands/AddToCustomerCart/AddToCustomerCartCommandHandler.cs
T
masoodafar-web b2d676b555 feat: Implement customer profile and referral queries
- Add GetCustomerProfileResponseDto for retrieving customer profile information.
- Create GetCustomerReferralsQuery and GetCustomerReferralsQueryHandler to fetch customer referrals with pagination and filtering options.
- Introduce GetCustomerReferralsResponseDto to structure the response for customer referrals.
- Implement GetCustomerSettingsQuery and GetCustomerSettingsQueryHandler to retrieve user settings.
- Add GetCustomerOrder and GetCustomerOrderQueryHandler for fetching specific customer orders.
- Create GetCustomerOrderHistoryQuery and GetCustomerOrderHistoryQueryHandler to retrieve order history with filtering options.
- Implement GetCustomerOrdersQuery and GetCustomerOrdersQueryHandler for fetching multiple customer orders with filters.
- Add GetCustomerWalletChangeLogQuery and GetCustomerWalletChangeLogQueryHandler for retrieving wallet change logs.
- Implement GetCustomerWithdrawalSettingsQuery and GetCustomerWithdrawalSettingsQueryHandler for fetching withdrawal settings.
- Create GetCustomerWithdrawalsQuery and GetCustomerWithdrawalsQueryHandler to retrieve customer withdrawal requests.
2026-02-05 23:01:50 +03:30

81 lines
2.7 KiB
C#

using CMSMicroservice.Application.Common.Interfaces;
using CMSMicroservice.Domain.Entities;
using MediatR;
using Microsoft.EntityFrameworkCore;
namespace CMSMicroservice.Application.DiscountShopCQ.Commands.AddToCustomerCart;
public class AddToCustomerCartCommandHandler : IRequestHandler<AddToCustomerCartCommand, AddToCustomerCartCommandResponse>
{
private readonly IApplicationDbContext _context;
private readonly ICurrentUserService _currentUser;
public AddToCustomerCartCommandHandler(IApplicationDbContext context, ICurrentUserService currentUser)
{
_context = context;
_currentUser = currentUser;
}
public async Task<AddToCustomerCartCommandResponse> Handle(AddToCustomerCartCommand request, CancellationToken cancellationToken)
{
// Extract UserId from JWT token
var userId = long.TryParse(_currentUser.UserId, out var id) ? id : 0;
if (userId == 0)
{
throw new UnauthorizedAccessException("User not authenticated");
}
// Check if product exists and is not deleted
var product = await _context.Products
.FirstOrDefaultAsync(p => p.Id == request.ProductId && !p.IsDeleted, cancellationToken);
if (product == null)
{
return new AddToCustomerCartCommandResponse
{
Success = false,
Message = "محصول یافت نشد یا حذف شده است"
};
}
// Check if item already exists in cart
var existingCartItem = await _context.UserCarts
.FirstOrDefaultAsync(uc => uc.UserId == userId && uc.ProductId == request.ProductId, cancellationToken);
if (existingCartItem != null)
{
// Update count
existingCartItem.Count += request.Count;
_context.UserCarts.Update(existingCartItem);
await _context.SaveChangesAsync(cancellationToken);
return new AddToCustomerCartCommandResponse
{
Id = existingCartItem.Id,
Success = true,
Message = "تعداد محصول در سبد خرید به‌روزرسانی شد"
};
}
// Create new cart item
var cartItem = new UserCart
{
UserId = userId,
ProductId = request.ProductId,
Count = request.Count,
Created = DateTime.UtcNow
};
_context.UserCarts.Add(cartItem);
await _context.SaveChangesAsync(cancellationToken);
return new AddToCustomerCartCommandResponse
{
Id = cartItem.Id,
Success = true,
Message = "محصول به سبد خرید اضافه شد"
};
}
}