public async Task Handle(PaymentAuthorizedEvent paymentAuthorizedEvent,
                             CancellationToken cancellationToken)
    {
        var payment = await _unitOfWork.Payments
                      .GetById(paymentAuthorizedEvent.PaymentId, cancellationToken);

        var order = await _unitOfWork.Orders
                    .GetById(payment.OrderId, cancellationToken);

        if (payment == null)
        {
            throw new ApplicationDataException("Payment not found.");
        }

        if (order == null)
        {
            throw new ApplicationDataException("Order not found.");
        }

        // Changing order status
        _orderStatusWorkflow.CalculateOrderStatus(order, payment);
        await _unitOfWork.CommitAsync(cancellationToken);

        // Broadcasting order update
        await _orderStatusBroadcaster.BroadcastOrderStatus(
            order.CustomerId,
            order.Id,
            order.Status
            );
    }
Example #2
0
    public async Task Handle(PaymentCreatedEvent paymentCreatedEvent
                             , CancellationToken cancellationToken)
    {
        var payment = await _unitOfWork.Payments
                      .GetById(paymentCreatedEvent.PaymentId, cancellationToken);

        var customer = await _unitOfWork.Customers
                       .GetById(payment.CustomerId, cancellationToken);

        var order = await _unitOfWork.Orders
                    .GetById(payment.OrderId, cancellationToken);

        if (payment == null)
        {
            throw new ApplicationDataException("Payment not found.");
        }

        if (customer == null)
        {
            throw new ApplicationDataException("Customer not found.");
        }

        if (order == null)
        {
            throw new ApplicationDataException("order not found.");
        }

        // Changing order status
        _orderStatusWorkflow.CalculateOrderStatus(order, payment);
        await _unitOfWork.CommitAsync(cancellationToken);

        // Attempting to pay
        MakePaymentCommand command = new MakePaymentCommand(paymentCreatedEvent.PaymentId.Value);
        await _mediator.Send(command, cancellationToken);

        // Broadcasting order update
        await _orderStatusBroadcaster.BroadcastOrderStatus(
            customer.Id,
            order.Id,
            order.Status
            );
    }