public static async Task DeleteOrder() { Database database = client.GetDatabase("database-v4"); Container container = database.GetContainer("customer"); string customerId = "FFD0DD37-1F0E-4E2E-8FAC-EAF45B0E9447"; string orderId = "5350ce31-ea50-4df9-9a48-faff97675ac5"; ItemResponse <CustomerV4> response = await container.ReadItemAsync <CustomerV4>( id : customerId, partitionKey : new PartitionKey(customerId) ); CustomerV4 customer = response.Resource; //Decrement the salesOrderTotal property customer.salesOrderCount--; //Submit both as a transactional batch TransactionalBatchResponse txBatchResponse = await container.CreateTransactionalBatch( new PartitionKey(customerId)) .DeleteItem(orderId) .ReplaceItem <CustomerV4>(customer.id, customer) .ExecuteAsync(); if (txBatchResponse.IsSuccessStatusCode) { Console.WriteLine("Order deleted successfully"); } Console.WriteLine("Press any key to continue..."); Console.ReadKey(); }
public static async Task CreateHugeMountainBikeOrder() { Database database = client.GetDatabase("database-v4"); Container container = database.GetContainer("customer"); //Get the customer string customerId = "FFD0DD37-1F0E-4E2E-8FAC-EAF45B0E9447"; ItemResponse <CustomerV4> response = await container.ReadItemAsync <CustomerV4>( id : customerId, partitionKey : new PartitionKey(customerId) ); CustomerV4 customer = response.Resource; //Increment the salesOrderTotal property customer.salesOrderCount++; //Create a new order string orderId = "f571e271-c98e-44d1-bb6c-47ad353c4ebc"; SalesOrder salesOrder = new SalesOrder { id = orderId, type = "salesOrder", customerId = customer.id, orderDate = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ"), shipDate = "", details = new List <SalesOrderDetails> { new SalesOrderDetails { sku = "BK-M82S-44", name = "Mountain-100 Silver, 44", price = 3399.99, quantity = 170 } } }; //Submit both as a transactional batch TransactionalBatchResponse txBatchResponse = await container.CreateTransactionalBatch( new PartitionKey(salesOrder.customerId)) .CreateItem <SalesOrder>(salesOrder) .ReplaceItem <CustomerV4>(customer.id, customer) .ExecuteAsync(); if (txBatchResponse.IsSuccessStatusCode) { Console.WriteLine("Order created successfully"); } Console.WriteLine("Press any key to continue..."); Console.ReadKey(); }
public static async Task CreateNewOrderAndUpdateCustomerOrderTotal() { Database database = client.GetDatabase("database-v4"); Container container = database.GetContainer("customer"); string customerId = "FFD0DD37-1F0E-4E2E-8FAC-EAF45B0E9447"; //Get the customer ItemResponse <CustomerV4> response = await container.ReadItemAsync <CustomerV4>( id : customerId, partitionKey : new PartitionKey(customerId) ); CustomerV4 customer = response.Resource; //To-Do: Write code to increment salesorderCount //Create a new order string orderId = "5350ce31-ea50-4df9-9a48-faff97675ac5"; //Normally would use Guid.NewGuid().ToString() SalesOrder salesOrder = new SalesOrder { id = orderId, type = "salesOrder", customerId = customer.id, orderDate = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ"), shipDate = "", details = new List <SalesOrderDetails> { new SalesOrderDetails { sku = "FR-M94B-38", name = "HL Mountain Frame - Black, 38", price = 1349.6, quantity = 1 }, new SalesOrderDetails { sku = "SO-R809-M", name = "Racing Socks, M", price = 8.99, quantity = 2 } } }; //To-Do: Write code to insert the new order and update the customer as a transaction Console.WriteLine("Press any key to continue..."); Console.ReadKey(); }
public static async Task QueryCustomerAndSalesOrdersByCustomerId() { Database database = client.GetDatabase("database-v4"); Container container = database.GetContainer("customer"); string customerId = "FFD0DD37-1F0E-4E2E-8FAC-EAF45B0E9447"; string sql = "SELECT * from c WHERE c.customerId = @customerId"; FeedIterator <dynamic> resultSet = container.GetItemQueryIterator <dynamic>( new QueryDefinition(sql) .WithParameter("@customerId", customerId), requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey(customerId) }); CustomerV4 customer = new CustomerV4(); List <SalesOrder> orders = new List <SalesOrder>(); while (resultSet.HasMoreResults) { //dynamic response. Deserialize into POCO's based upon "type" property FeedResponse <dynamic> response = await resultSet.ReadNextAsync(); foreach (var item in response) { if (item.type == "customer") { customer = JsonConvert.DeserializeObject <CustomerV4>(item.ToString()); } else if (item.type == "salesOrder") { orders.Add(JsonConvert.DeserializeObject <SalesOrder>(item.ToString())); } } } Console.ForegroundColor = ConsoleColor.Blue; Console.WriteLine("\nPrint out customer record and all their orders"); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("------------------------------------------------\n"); Console.ForegroundColor = ConsoleColor.White; Print(customer); foreach (SalesOrder order in orders) { Print(order); } Console.WriteLine("Press any key to continue..."); Console.ReadKey(); }
private static async Task DeleteHugeMountainBikeOrder() { Database database = client.GetDatabase("database-v4"); Container container = database.GetContainer("customer"); Container container2 = database.GetContainer("salesByCategory"); string customerId = "FFD0DD37-1F0E-4E2E-8FAC-EAF45B0E9447"; string orderId = "f571e271-c98e-44d1-bb6c-47ad353c4ebc"; string categoryId = "56400CF3-446D-4C3F-B9B2-68286DA3BB99"; ItemResponse <CustomerV4> response = await container.ReadItemAsync <CustomerV4>( id : customerId, partitionKey : new PartitionKey(customerId) ); CustomerV4 customer = response.Resource; //Decrement the salesOrderTotal property customer.salesOrderCount--; //Submit both as a transactional batch TransactionalBatchResponse txBatchResponse = await container.CreateTransactionalBatch( new PartitionKey(customerId)) .DeleteItem(orderId) .ReplaceItem <CustomerV4>(customer.id, customer) .ExecuteAsync(); //revert category sales to original value (normally this would be done with a soft delete or some other means) ItemResponse <CategorySales> response1 = await container2.ReadItemAsync <CategorySales>(categoryId, new PartitionKey(categoryId)); CategorySales categorySales = response1.Resource; categorySales.totalSales = 11788915; await container2.ReplaceItemAsync(categorySales, categoryId, new PartitionKey(categoryId)); }
public static async Task CreateNewOrderAndUpdateCustomerOrderTotal() { Database database = client.GetDatabase("database-v4"); Container container = database.GetContainer("customer"); //Get the customer string customerId = "FFD0DD37-1F0E-4E2E-8FAC-EAF45B0E9447"; ItemResponse <CustomerV4> response = await container.ReadItemAsync <CustomerV4>( id : customerId, partitionKey : new PartitionKey(customerId) ); CustomerV4 customer = response.Resource; //Increment the salesOrderTotal property customer.salesOrderCount++; //Create a new order string orderId = "5350ce31-ea50-4df9-9a48-faff97675ac5"; //Normally would use Guid.NewGuid().ToString() SalesOrder salesOrder = new SalesOrder { id = orderId, type = "salesOrder", customerId = customer.id, orderDate = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ"), shipDate = "", details = new List <SalesOrderDetails> { new SalesOrderDetails { sku = "FR-M94B-38", name = "HL Mountain Frame - Black, 38", price = 1349.6, quantity = 1 }, new SalesOrderDetails { sku = "SO-R809-M", name = "Racing Socks, M", price = 8.99, quantity = 2 } } }; //Submit both as a transactional batch TransactionalBatchResponse txBatchResponse = await container.CreateTransactionalBatch( new PartitionKey(salesOrder.customerId)) .CreateItem <SalesOrder>(salesOrder) .ReplaceItem <CustomerV4>(customer.id, customer) .ExecuteAsync(); if (txBatchResponse.IsSuccessStatusCode) { Console.WriteLine("Order created successfully"); } Console.WriteLine("Press any key to continue..."); Console.ReadKey(); }