2025-10-13 12:05:05 +03:30
|
|
|
|
using CMSMicroservice.Protobuf.Protos.UserOrder;
|
|
|
|
|
|
|
2025-09-27 10:36:00 +03:30
|
|
|
|
namespace FrontOffice.BFF.Application.UserOrderCQ.Commands.CreateNewUserOrder;
|
|
|
|
|
|
public class CreateNewUserOrderCommandHandler : IRequestHandler<CreateNewUserOrderCommand, CreateNewUserOrderResponseDto>
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly IApplicationContractContext _context;
|
2025-10-13 12:05:05 +03:30
|
|
|
|
private readonly ICurrentUserService _currentUserService;
|
2025-09-27 10:36:00 +03:30
|
|
|
|
|
2025-10-13 12:05:05 +03:30
|
|
|
|
public CreateNewUserOrderCommandHandler(IApplicationContractContext context, ICurrentUserService currentUserService)
|
2025-09-27 10:36:00 +03:30
|
|
|
|
{
|
|
|
|
|
|
_context = context;
|
2025-10-13 12:05:05 +03:30
|
|
|
|
_currentUserService = currentUserService;
|
2025-09-27 10:36:00 +03:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<CreateNewUserOrderResponseDto> Handle(CreateNewUserOrderCommand request, CancellationToken cancellationToken)
|
|
|
|
|
|
{
|
2025-10-13 12:05:05 +03:30
|
|
|
|
var userId = _currentUserService.UserId ?? throw new ForbiddenAccessException();
|
|
|
|
|
|
var getAllUserAddress = await _context.UserAddress.GetAllUserAddressByFilterAsync(request: new()
|
|
|
|
|
|
{
|
|
|
|
|
|
Filter = new()
|
|
|
|
|
|
{
|
|
|
|
|
|
IsDefault = true,
|
|
|
|
|
|
UserId = long.Parse(userId)
|
|
|
|
|
|
}
|
|
|
|
|
|
}, cancellationToken: cancellationToken);
|
|
|
|
|
|
if (getAllUserAddress?.Models?.Any() == false)
|
|
|
|
|
|
throw new Exception(message:"آدرس نامعتبر است!!");
|
|
|
|
|
|
|
|
|
|
|
|
var req = request.Adapt<CreateNewUserOrderRequest>();
|
|
|
|
|
|
req.UserId = long.Parse(userId);
|
|
|
|
|
|
req.UserAddressId = getAllUserAddress!.Models.First().Id;
|
|
|
|
|
|
|
|
|
|
|
|
var response = await _context.UserOrder.CreateNewUserOrderAsync(request: req, cancellationToken: cancellationToken);
|
|
|
|
|
|
return response.Adapt<CreateNewUserOrderResponseDto>();
|
2025-09-27 10:36:00 +03:30
|
|
|
|
}
|
|
|
|
|
|
}
|