Adding output bindings for the orders collection

Once the order status is updated, an order needs to be created in the orders collection in the Sales database. Add the following code to the Function parameters:

[CosmosDB(databaseName:"Sales", collectionName:"Orders", ConnectionStringSetting ="dbConnectionString")] IAsyncCollector<PurchaseOrder> writeResultsToCosmos 

The binding is an output binding as we make use of IAsyncCollector. The wirteResultToCosmos object will be able to write the document to the orders collection. We will have to add the following line to update the orders collection:

await writeResultsToCosmos.AddAsync(order); 

The complete Function code looks as follows:

using Microsoft.Azure.WebJobs; 
using Microsoft.Azure.WebJobs.Host; 
using Microsoft.Extensions.Logging; 
using System.Collections.Generic; 
using System.Collections; 
using System.Linq; 
namespace ShipAnyWhere 
{ 
    public static class UpdateInventory 
    { 
        [FunctionName("UpdateInventory")] 
        public async static void Run([ServiceBusTrigger("orders", Connection = "ShipAnyWhere_SERVICEBUS")]PurchaseOrder order, 
 
                              [CosmosDB(databaseName: "inventory", 
                               collectionName:"Products", 
                               ConnectionStringSetting = "dbConnectionString", 
                               Id = "{Sku}", PartitionKey="{Sku}"),]Product product ,                            
 
                               [CosmosDB(databaseName:"Sales",  
                               collectionName:"Orders",  
                               ConnectionStringSetting ="dbConnectionString")] 
                               IAsyncCollector<PurchaseOrder> writeResultsToCosmos,   
                                                             
                               ILogger log) 
                                
        {             
           if( product!=null && product.AvailableQuantity >= order.Quantity)             
            { 
              order.Status = "Accepted"; 
            } 
            else 
            order.Status ="Rejected"; 
           await writeResultsToCosmos.AddAsync(order); 
            log.LogInformation($"C# ServiceBus queue trigger function processed message: {order.Sku}"); 
        } 
    } 
} 
 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.83.151