private static async Task UpdateCategorySales(string sku, int newSales) { string categoryId = null; string categoryName = null; string sql = $"SELECT c.categoryId, c.categoryName, 'category' as type FROM c WHERE c.sku = '{sku}'"; FeedIterator <dynamic> resultSet = productContainer.GetItemQueryIterator <dynamic>(new QueryDefinition(sql)); while (resultSet.HasMoreResults) { FeedResponse <dynamic> response = await resultSet.ReadNextAsync(); foreach (dynamic category in response) { categoryId = category.categoryId; categoryName = category.categoryName; } } //Get current sales for category CategorySales categorySales = await ReadCategorySales(categoryId); if (categorySales != null) { categorySales.totalSales += newSales; } else { //First time execution, no data in container categorySales = new CategorySales(); categorySales.id = categoryId; categorySales.categoryId = categoryId; categorySales.categoryName = categoryName; categorySales.totalSales = newSales; } //update total sales for category await salesByCategoryContainer.UpsertItemAsync <CategorySales>(categorySales, new PartitionKey(categoryId)); Console.WriteLine($"Category: {categorySales.categoryName} updated total sales: {categorySales.totalSales}"); }
private static async Task <CategorySales> ReadCategorySales(string categoryId) { CategorySales categorySales = null; try { ItemResponse <CategorySales> response = await salesByCategoryContainer.ReadItemAsync <CategorySales>( id : categoryId, partitionKey : new PartitionKey(categoryId)); categorySales = response.Resource; } catch (CosmosException e) { //First time execution, no data in container if (e.StatusCode == System.Net.HttpStatusCode.NotFound) { categorySales = null; } } return(categorySales); }
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)); }