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.
This commit is contained in:
+20
@@ -0,0 +1,20 @@
|
||||
using MediatR;
|
||||
|
||||
namespace CMSMicroservice.Application.DiscountShopCQ.Commands.AddToCustomerCart;
|
||||
|
||||
/// <summary>
|
||||
/// Command برای افزودن محصول به سبد خرید کاربر فعلی
|
||||
/// </summary>
|
||||
public class AddToCustomerCartCommand : IRequest<AddToCustomerCartCommandResponse>
|
||||
{
|
||||
public long ProductId { get; set; }
|
||||
public int Count { get; set; }
|
||||
// UserId from ICurrentUserService
|
||||
}
|
||||
|
||||
public class AddToCustomerCartCommandResponse
|
||||
{
|
||||
public long Id { get; set; }
|
||||
public string Message { get; set; } = string.Empty;
|
||||
public bool Success { get; set; }
|
||||
}
|
||||
+80
@@ -0,0 +1,80 @@
|
||||
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 = "محصول به سبد خرید اضافه شد"
|
||||
};
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
using MediatR;
|
||||
|
||||
namespace CMSMicroservice.Application.DiscountShopCQ.Commands.RemoveFromCustomerCart;
|
||||
|
||||
/// <summary>
|
||||
/// Command برای حذف محصول از سبد خرید
|
||||
/// </summary>
|
||||
public class RemoveFromCustomerCartCommand : IRequest<RemoveFromCustomerCartCommandResponse>
|
||||
{
|
||||
public long CartItemId { get; set; }
|
||||
// UserId from ICurrentUserService
|
||||
}
|
||||
|
||||
public class RemoveFromCustomerCartCommandResponse
|
||||
{
|
||||
public string Message { get; set; } = string.Empty;
|
||||
public bool Success { get; set; }
|
||||
}
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
using CMSMicroservice.Application.Common.Interfaces;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace CMSMicroservice.Application.DiscountShopCQ.Commands.RemoveFromCustomerCart;
|
||||
|
||||
public class RemoveFromCustomerCartCommandHandler : IRequestHandler<RemoveFromCustomerCartCommand, RemoveFromCustomerCartCommandResponse>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
private readonly ICurrentUserService _currentUser;
|
||||
|
||||
public RemoveFromCustomerCartCommandHandler(IApplicationDbContext context, ICurrentUserService currentUser)
|
||||
{
|
||||
_context = context;
|
||||
_currentUser = currentUser;
|
||||
}
|
||||
|
||||
public async Task<RemoveFromCustomerCartCommandResponse> Handle(RemoveFromCustomerCartCommand 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");
|
||||
}
|
||||
|
||||
// Find and remove cart item
|
||||
var cartItem = await _context.UserCarts
|
||||
.FirstOrDefaultAsync(uc => uc.Id == request.CartItemId && uc.UserId == userId, cancellationToken);
|
||||
|
||||
if (cartItem == null)
|
||||
{
|
||||
return new RemoveFromCustomerCartCommandResponse
|
||||
{
|
||||
Success = false,
|
||||
Message = "آیتم سبد خرید یافت نشد"
|
||||
};
|
||||
}
|
||||
|
||||
_context.UserCarts.Remove(cartItem);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new RemoveFromCustomerCartCommandResponse
|
||||
{
|
||||
Success = true,
|
||||
Message = "آیتم از سبد خرید حذف شد"
|
||||
};
|
||||
}
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
using MediatR;
|
||||
|
||||
namespace CMSMicroservice.Application.DiscountShopCQ.Commands.UpdateCustomerCartItem;
|
||||
|
||||
/// <summary>
|
||||
/// Command برای بهروزرسانی تعداد محصول در سبد خرید
|
||||
/// </summary>
|
||||
public class UpdateCustomerCartItemCommand : IRequest<UpdateCustomerCartItemCommandResponse>
|
||||
{
|
||||
public long CartItemId { get; set; }
|
||||
public int Count { get; set; }
|
||||
// UserId from ICurrentUserService
|
||||
}
|
||||
|
||||
public class UpdateCustomerCartItemCommandResponse
|
||||
{
|
||||
public string Message { get; set; } = string.Empty;
|
||||
public bool Success { get; set; }
|
||||
}
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
using CMSMicroservice.Application.Common.Interfaces;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace CMSMicroservice.Application.DiscountShopCQ.Commands.UpdateCustomerCartItem;
|
||||
|
||||
public class UpdateCustomerCartItemCommandHandler : IRequestHandler<UpdateCustomerCartItemCommand, UpdateCustomerCartItemCommandResponse>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
private readonly ICurrentUserService _currentUser;
|
||||
|
||||
public UpdateCustomerCartItemCommandHandler(IApplicationDbContext context, ICurrentUserService currentUser)
|
||||
{
|
||||
_context = context;
|
||||
_currentUser = currentUser;
|
||||
}
|
||||
|
||||
public async Task<UpdateCustomerCartItemCommandResponse> Handle(UpdateCustomerCartItemCommand 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");
|
||||
}
|
||||
|
||||
// Find cart item
|
||||
var cartItem = await _context.UserCarts
|
||||
.FirstOrDefaultAsync(uc => uc.Id == request.CartItemId && uc.UserId == userId, cancellationToken);
|
||||
|
||||
if (cartItem == null)
|
||||
{
|
||||
return new UpdateCustomerCartItemCommandResponse
|
||||
{
|
||||
Success = false,
|
||||
Message = "آیتم سبد خرید یافت نشد"
|
||||
};
|
||||
}
|
||||
|
||||
// Update count
|
||||
if (request.Count <= 0)
|
||||
{
|
||||
// Remove item if count is 0 or negative
|
||||
_context.UserCarts.Remove(cartItem);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new UpdateCustomerCartItemCommandResponse
|
||||
{
|
||||
Success = true,
|
||||
Message = "آیتم از سبد خرید حذف شد"
|
||||
};
|
||||
}
|
||||
|
||||
cartItem.Count = request.Count;
|
||||
_context.UserCarts.Update(cartItem);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new UpdateCustomerCartItemCommandResponse
|
||||
{
|
||||
Success = true,
|
||||
Message = "تعداد آیتم بهروزرسانی شد"
|
||||
};
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
using MediatR;
|
||||
|
||||
namespace CMSMicroservice.Application.DiscountShopCQ.Queries.GetCustomerCart;
|
||||
|
||||
/// <summary>
|
||||
/// Query برای دریافت سبد خرید کاربر فعلی
|
||||
/// </summary>
|
||||
public class GetCustomerCartQuery : IRequest<GetCustomerCartQueryResponse>
|
||||
{
|
||||
// UserId from ICurrentUserService
|
||||
}
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
using CMSMicroservice.Application.Common.Interfaces;
|
||||
using CMSMicroservice.Domain.Entities;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace CMSMicroservice.Application.DiscountShopCQ.Queries.GetCustomerCart;
|
||||
|
||||
public class GetCustomerCartQueryHandler : IRequestHandler<GetCustomerCartQuery, GetCustomerCartQueryResponse>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
private readonly ICurrentUserService _currentUser;
|
||||
|
||||
public GetCustomerCartQueryHandler(IApplicationDbContext context, ICurrentUserService currentUser)
|
||||
{
|
||||
_context = context;
|
||||
_currentUser = currentUser;
|
||||
}
|
||||
|
||||
public async Task<GetCustomerCartQueryResponse> Handle(GetCustomerCartQuery 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");
|
||||
}
|
||||
|
||||
// Get all cart items for the current user
|
||||
var cartItems = await _context.UserCarts
|
||||
.Include(uc => uc.Product)
|
||||
.Where(uc => uc.UserId == userId)
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
var response = new GetCustomerCartQueryResponse
|
||||
{
|
||||
TotalItemsCount = cartItems.Sum(c => c.Count),
|
||||
Message = cartItems.Count > 0 ? "سبد خرید با موفقیت بازیابی شد" : "سبد خرید خالی است"
|
||||
};
|
||||
|
||||
foreach (var item in cartItems)
|
||||
{
|
||||
// Use Product.ThumbnailPath directly
|
||||
var thumbnailPath = item.Product?.ThumbnailPath ?? string.Empty;
|
||||
var itemPrice = item.Product?.Price ?? 0;
|
||||
var itemDiscount = item.Product?.Discount ?? 0;
|
||||
var finalPrice = itemPrice * (100 - itemDiscount) / 100;
|
||||
var totalItemPrice = finalPrice * item.Count;
|
||||
|
||||
response.Items.Add(new CustomerCartItemModel
|
||||
{
|
||||
Id = item.Id,
|
||||
ProductId = item.ProductId,
|
||||
ProductTitle = item.Product?.Title ?? string.Empty,
|
||||
ProductShortInformation = item.Product?.ShortInfomation ?? string.Empty, // Typo in DB: ShortInfomation
|
||||
ProductPrice = itemPrice,
|
||||
ProductDiscount = itemDiscount,
|
||||
ProductThumbnailPath = thumbnailPath,
|
||||
Count = item.Count,
|
||||
TotalItemPrice = totalItemPrice,
|
||||
Created = item.Created
|
||||
});
|
||||
|
||||
response.TotalPrice += totalItemPrice;
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
namespace CMSMicroservice.Application.DiscountShopCQ.Queries.GetCustomerCart;
|
||||
|
||||
public class GetCustomerCartQueryResponse
|
||||
{
|
||||
public List<CustomerCartItemModel> Items { get; set; } = new();
|
||||
public long TotalPrice { get; set; }
|
||||
public int TotalItemsCount { get; set; }
|
||||
public string Message { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
public class CustomerCartItemModel
|
||||
{
|
||||
public long Id { get; set; }
|
||||
public long ProductId { get; set; }
|
||||
public string ProductTitle { get; set; } = string.Empty;
|
||||
public string ProductShortInformation { get; set; } = string.Empty;
|
||||
public long ProductPrice { get; set; }
|
||||
public int ProductDiscount { get; set; }
|
||||
public string ProductThumbnailPath { get; set; } = string.Empty;
|
||||
public int Count { get; set; }
|
||||
public long TotalItemPrice { get; set; }
|
||||
public DateTime Created { get; set; }
|
||||
}
|
||||
Reference in New Issue
Block a user