Skip to content

Commit

Permalink
fix cancellation tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
ElectNewt committed Nov 19, 2024
1 parent 471077d commit a83d242
Showing 1 changed file with 14 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ public async Task<Result<CreateOrderResponse>> Execute(CreateOrderRequest create
.Async()
//On a real scenario:
//validate orders
//validate fraud check
.Bind(x=> ValidateFraudCheck(x, cancellationToken))
.Bind(x => SaveOrder(x, cancellationToken))
.Then(x => MapToOrderResponse(x, cancellationToken)
.Bind(or => PublishDomainEvent(or, cancellationToken)))
.Then(x => MapToOrderResponse(x)
.Bind(PublishDomainEvent))
.Map(x => new CreateOrderResponse(x.Id, $"order/getorderstatus/{x.Id}"));
}

Expand All @@ -60,23 +60,28 @@ private async Task<Result<OrderDetails>> SaveOrder(OrderDetails orderDetails, Ca
return orderDetails;
}

private async Task<Result<OrderResponse>> MapToOrderResponse(OrderDetails orderDetails,
CancellationToken cancellationToken)
private async Task<Result<OrderResponse>> MapToOrderResponse(OrderDetails orderDetails)
{
var products = await orderDetails.Products
.SelectAsync(async p => new ProductQuantityName(p.ProductId, p.Quantity,
await _productNameService.GetProductName(p.ProductId, cancellationToken)));
await _productNameService.GetProductName(p.ProductId)));


OrderResponse orderResponse = new OrderResponse(orderDetails.Id, orderDetails.Status.ToString(),
orderDetails.Delivery, orderDetails.PaymentInformation, products.ToList());
return orderResponse;
}

private async Task<Result<Guid>> PublishDomainEvent(OrderResponse orderResponse,
CancellationToken cancellationToken)
private async Task<Result<Guid>> PublishDomainEvent(OrderResponse orderResponse)
{
await _domainMessagePublisher.Publish(orderResponse, routingKey: "order", cancellationToken: cancellationToken);
await _domainMessagePublisher.Publish(orderResponse, routingKey: "order");
return orderResponse.OrderId;
}

private async Task<Result<OrderDetails>> ValidateFraudCheck(OrderDetails orderDetails,

Check warning on line 81 in src/Services/Orders/Distribt.Services.Orders/Services/CreateOrderService.cs

View workflow job for this annotation

GitHub Actions / build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
CancellationToken cancellationToken)
{
//Simulation of fraud check validation
return orderDetails;
}
}

0 comments on commit a83d242

Please sign in to comment.