Files
CMS/src/CMSMicroservice.Application/UserAddressCQ/Commands/CreateCustomerAddress/CreateCustomerAddressCommandHandler.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

63 lines
2.1 KiB
C#

using CMSMicroservice.Application.Common.Interfaces;
using CMSMicroservice.Domain.Entities;
using MediatR;
using Microsoft.EntityFrameworkCore;
namespace CMSMicroservice.Application.UserAddressCQ.Commands.CreateCustomerAddress;
public class CreateCustomerAddressCommandHandler : IRequestHandler<CreateCustomerAddressCommand, CreateCustomerAddressCommandResponse>
{
private readonly IApplicationDbContext _context;
private readonly ICurrentUserService _currentUser;
public CreateCustomerAddressCommandHandler(IApplicationDbContext context, ICurrentUserService currentUser)
{
_context = context;
_currentUser = currentUser;
}
public async Task<CreateCustomerAddressCommandResponse> Handle(CreateCustomerAddressCommand 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");
}
// If this address is set as default, unset other defaults
if (request.IsDefault)
{
var existingDefaults = await _context.UserAddresses
.Where(ua => ua.UserId == userId && ua.IsDefault && !ua.IsDeleted)
.ToListAsync(cancellationToken);
foreach (var addr in existingDefaults)
{
addr.IsDefault = false;
}
}
// Create new address
var address = new UserAddress
{
UserId = userId,
Title = request.Title,
Address = request.Address,
PostalCode = request.PostalCode,
IsDefault = request.IsDefault,
CityId = request.CityId,
Created = DateTime.UtcNow
};
_context.UserAddresses.Add(address);
await _context.SaveChangesAsync(cancellationToken);
return new CreateCustomerAddressCommandResponse
{
Id = address.Id,
Message = "آدرس با موفقیت ایجاد شد"
};
}
}