2025-11-24 22:55:14 +03:30
|
|
|
using CMSMicroservice.Domain.Enums;
|
2025-11-22 22:02:04 +03:30
|
|
|
using CMSMicroservice.Domain.Events;
|
2025-11-24 22:55:14 +03:30
|
|
|
|
2025-11-22 22:02:04 +03:30
|
|
|
namespace CMSMicroservice.Application.UserOrderCQ.Commands.SubmitShopBuyOrder;
|
2025-11-24 22:55:14 +03:30
|
|
|
|
|
|
|
|
public class
|
|
|
|
|
SubmitShopBuyOrderCommandHandler : IRequestHandler<SubmitShopBuyOrderCommand, SubmitShopBuyOrderResponseDto>
|
2025-11-22 22:02:04 +03:30
|
|
|
{
|
|
|
|
|
private readonly IApplicationDbContext _context;
|
|
|
|
|
|
|
|
|
|
public SubmitShopBuyOrderCommandHandler(IApplicationDbContext context)
|
|
|
|
|
{
|
|
|
|
|
_context = context;
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-24 22:55:14 +03:30
|
|
|
public async Task<SubmitShopBuyOrderResponseDto> Handle(SubmitShopBuyOrderCommand request,
|
|
|
|
|
CancellationToken cancellationToken)
|
2025-11-22 22:02:04 +03:30
|
|
|
{
|
2025-11-24 22:55:14 +03:30
|
|
|
var user = await _context.Users
|
|
|
|
|
.Include(i => i.UserAddresss)
|
|
|
|
|
.Include(i => i.UserCartss)
|
|
|
|
|
.ThenInclude(i => i.Product)
|
|
|
|
|
.FirstOrDefaultAsync(w => w.Id == request.UserId, cancellationToken: cancellationToken);
|
|
|
|
|
if (user.UserCartss.Count == 0)
|
|
|
|
|
throw new NotFoundException("UserCart", request.UserId);
|
|
|
|
|
if (user.UserCartss.Sum(s => s.Count * s.Product.Price) != request.TotalAmount)
|
|
|
|
|
throw new Exception("مبلغ سفارش با مجموع سبد خرید مطابقت ندارد.");
|
|
|
|
|
|
|
|
|
|
await _context.UserOrders.AddAsync(new UserOrder()
|
|
|
|
|
{
|
|
|
|
|
Price = request.TotalAmount,
|
|
|
|
|
PaymentStatus = PaymentStatus.Success,
|
|
|
|
|
PaymentMethod = PaymentMethod.Wallet,
|
|
|
|
|
PaymentDate =DateTime.Now,
|
|
|
|
|
UserId = request.UserId,
|
|
|
|
|
UserAddressId = user.UserAddresss.First(f=>f.IsDefault).Id
|
|
|
|
|
}, cancellationToken);
|
|
|
|
|
await _context.FactorDetailss.AddRangeAsync(user.UserCartss.Select(s => new FactorDetails()
|
|
|
|
|
{
|
|
|
|
|
ProductId = s.ProductId,
|
|
|
|
|
Count = s.Count,
|
|
|
|
|
UnitPrice = s.Product.Price,
|
|
|
|
|
|
|
|
|
|
}), cancellationToken);
|
2025-11-22 22:02:04 +03:30
|
|
|
return new SubmitShopBuyOrderResponseDto();
|
|
|
|
|
}
|
2025-11-24 22:55:14 +03:30
|
|
|
}
|