Adding input bindings to Inventory DB

Inventory DB contains a collection called Products. From the order message, we need to extract a field called Sku and look it up against productid of product documents. If the product with Sku is not available or the available quantity is less than the ordered quantity, we will mark an order status as Rejected:

  1. Add a class file called PurchaseOrder.cs to our Function project:
          
namespace ShipAnyWhere 
{ 
    using Newtonsoft.Json; 
    using Newtonsoft.Json.Converters; 
    public  class PurchaseOrder 
    { 
        [JsonProperty("ponumber")]      
        public string Ponumber { get; set; } 
 
        [JsonProperty("name")] 
        public string Name { get; set; } 
 
        [JsonProperty("sku")] 
        
        public string Sku { get; set; } 
 
        [JsonProperty("Location")] 
        public string Location { get; set; } 
 
        [JsonProperty("price")] 
        public double Price { get; set; } 
 
        [JsonProperty("quantity")] 
        public long Quantity { get; set; } 
 
        [JsonProperty("messagetype")] 
        public string Messagetype { get; set; } 
 
         [JsonProperty("status")] 
         public string Status { get; set; } 
 
          [JsonProperty("shipTo")] 
        public To ShipTo { get; set; } 
 
        [JsonProperty("billTo")] 
        public To BillTo { get; set; } 
    }     
    public partial class To 
    { 
        [JsonProperty("name")] 
        public string Name { get; set; } 
 
        [JsonProperty("address")] 
        public string Address { get; set; } 
 
        [JsonProperty("city")] 
        public string City { get; set; } 
 
        [JsonProperty("state")] 
        public string State { get; set; } 
 
        [JsonProperty("zip")]         
        public string Zip { get; set; } 
    } 
} 
 
  1. Add a class file called Product.cs to our Function project:
   namespace ShipAnyWhere 
{ using Newtonsoft.Json; using Newtonsoft.Json.Converters; public partial class Product { [JsonProperty("id")] public string Productid { get; set; } [JsonProperty("name")] public string Name { get; set; } [JsonProperty("location")] public string Location { get; set; } [JsonProperty("avialableQuantity")] public long AvailableQuantity { get; set; } } }
  1. Change the Service Bus trigger with PurchaseOrder class. This ensures that the order message received from the Service Bus queue is deserialized into an object:
  1. Add a CosmosDb input binding to run the method as shown:
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 ,ILogger log ) 

This is all we need to do to get product details from the inventory database based on Sku which is one of the fields of the purchase order. The product object will be null if there is no document, else it will have the product details.

We will check this condition in the Function as follows:

if( product!=null && product.AvailableQuantity >= order.Quantity) { order.Status = "Accepted"; }

..................Content has been hidden....................

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