public static async Task Run( [QueueTrigger("orders")] OrderReceivedEvent orderData, [Inject] IOrderProcessor orderProcessor, [Blob("viporders")] CloudBlobContainer vipOrdersContainer, [Blob("normalorders")] CloudBlobContainer normalOrdersContainer, ILogger log) { log.LogInformation($"Received order created event: {orderData.OrderTrackingId}"); await vipOrdersContainer.CreateIfNotExistsAsync(); await normalOrdersContainer.CreateIfNotExistsAsync(); var orderNature = orderProcessor.GetOrderNature(orderData); var serializedOrderData = JsonConvert.SerializeObject(orderData); if (orderNature == OrderType.Vip) { var vipBlob = vipOrdersContainer.GetBlockBlobReference($"{orderData.OrderTrackingId}.json"); await vipBlob.UploadTextAsync(JsonConvert.SerializeObject(serializedOrderData)); } else { var normalBlob = normalOrdersContainer.GetBlockBlobReference($"{orderData.OrderTrackingId}.json"); await normalBlob.UploadTextAsync(JsonConvert.SerializeObject(serializedOrderData)); } }
public static async Task Run([CosmosDBTrigger( databaseName: "MyShopDB", collectionName: "Orders", ConnectionStringSetting = "MyShopDatabaseConnection", CreateLeaseCollectionIfNotExists = true)] IReadOnlyList <Document> input, [Queue("received-orders")] IAsyncCollector <OrderReceivedEvent> receivedOrders, ILogger logger) { logger.LogInformation($"{nameof(OrderHandlingFunction)} got called.."); if (input == null || !input.Any()) { logger.LogInformation("There are no documents to process"); } var documents = new List <Document>(input); foreach (var document in documents) { var content = document.ToString(); var orderRequest = JsonConvert.DeserializeObject <CreateOrderRequest>(content); var orderReceivedEvent = new OrderReceivedEvent { CustomerId = orderRequest.CustomerId, OrderId = orderRequest.OrderId }; await receivedOrders.AddAsync(orderReceivedEvent); logger.LogInformation($"Order received {orderReceivedEvent.OrderId} for customer {orderReceivedEvent.CustomerId}"); } }
public async Task Handle(OrderReceivedEvent message) { var notification = new NotificationAggregate { Id = Guid.NewGuid().ToString(), Content = "receive_order", IsRead = false, From = message.Client, To = message.Seller, CreatedDateTime = DateTime.UtcNow, Parameters = new[] { new NotificationParameter { Id = Guid.NewGuid().ToString(), Type = NotificationParameterTypes.OrderId, Value = message.OrderId } } }; await _notificationRepository.Add(notification); _eventPublisher.Publish(new NotificationAddedEvent { Id = notification.Id, Content = notification.Content, IsRead = notification.IsRead, From = notification.From, To = notification.To }); }
public static void Run([QueueTrigger("received-orders")] OrderReceivedEvent receivedOrder, [CosmosDB( databaseName: "MyShopDB", collectionName: "Orders", ConnectionStringSetting = "MyShopDatabaseConnection", Id = "{OrderId}" )] CreateOrderRequest request, ILogger logger) { logger.LogInformation($"Verify order: {request.OrderId}"); }
public OrderType GetOrderNature(OrderReceivedEvent data) { if (data?.OriginalOrder == null) { throw new ArgumentNullException("Invalid order created event."); } if (data.OriginalOrder.CustomerId % 2 == 0) { return(OrderType.Vip); } return(OrderType.Normal); }
public Task Handle(OrderReceivedEvent message) { if (message == null) { throw new ArgumentNullException(nameof(message)); } var notifier = _connectionManager.GetHubContext <SecuredHub>(); var lst = new[] { message.Client, message.Seller }; lst.Distinct(); var connectionIds = new List <string>(); foreach (var r in lst) { connectionIds.AddRange(SecuredHub.Connections.GetConnections(r).ToList()); } notifier.Clients.Clients(connectionIds).orderReceived(_responseBuilder.GetOrderReceivedEvent(message)); return(Task.FromResult(0)); }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req, [Queue("orders")] IAsyncCollector <OrderReceivedEvent> ordersQueue, ILogger log) { log.LogInformation("Received a create order request"); var requestBody = await new StreamReader(req.Body).ReadToEndAsync(); if (string.IsNullOrWhiteSpace(requestBody)) { return(new BadRequestObjectResult("Request is empty")); } log.LogInformation("Request body"); log.LogInformation(requestBody); var request = JsonConvert.DeserializeObject <CreateOrderRequest>(requestBody); if (request == null) { return(new BadRequestObjectResult("Invalid request format")); } var orderReceived = new OrderReceivedEvent(Guid.NewGuid(), request); await ordersQueue.AddAsync(orderReceived); log.LogInformation("Inserted the order created event to the queue"); var response = new CreateOrderResponse(orderReceived.OrderTrackingId); return(new OkObjectResult(response)); }
public Task Handle(CreateOrderCommand message, IMessageHandlerContext context) { var format = "{Application}.{Service}.{Operation}-{TransactionId}: {LogMessage}"; // publish an OrderReceivedEvent via the context object var orderReceivedEvent = new OrderReceivedEvent { TransactionId = message.TransactionId }; try { context.Publish(orderReceivedEvent); } catch (Exception ex) { var errorMessage = $"Could not publish OrderReceivedEvent. Error: {ex.Message}"; log.ErrorFormat(format, "Lhi.NsbDemo.Orders", "OrdersSaga", "Handle-CreatedOrderCommand", message.TransactionId, errorMessage); } Console.WriteLine($"Step 1: CreateOrderCommand {message.TransactionId} was received."); //log.Info($"Step 1: CreateOrderCommand {message.TransactionId} was received."); _promCounter.Labels(Environment.MachineName, "received").Inc(); var logMessage = $"Step 1: CreateOrderCommand {message.TransactionId} was received."; try { log.InfoFormat(format, "Lhi.NsbDemo.Orders", "OrdersSaga", "Handle-CreatedOrderCommand", message.TransactionId, logMessage); } catch { } return(Task.CompletedTask); }