diff --git a/src/web/Pages/Index.razor b/src/web/Pages/Index.razor index b19eeb6..743577f 100644 --- a/src/web/Pages/Index.razor +++ b/src/web/Pages/Index.razor @@ -1,5 +1,5 @@ @page "/" -@inject CosmosClient client +@inject ICosmosDbService cosmosDbService @using System.Text
@@ -34,7 +34,7 @@ ENDPOINT: - @client.Endpoint + @cosmosDbService.GetEndpoint() @@ -54,104 +54,7 @@ await WriteToConsoleAync("Current Status:\tStarting..."); - //- Database database = client.GetDatabase("cosmicworks"); - // - database = await database.ReadAsync(); - await WriteToConsoleAync($"Get database:\t{database.Id}"); - - //- Container container = database.GetContainer("products"); - // - container = await container.ReadContainerAsync(); - await WriteToConsoleAync($"Get container:\t{container.Id}"); - - { - //- Product item = new( - id: "68719518391", - category: "gear-surf-surfboards", - name: "Yamba Surfboard", - quantity: 12, - price: 850.00m, - clearance: false - ); - - ItemResponse - await WriteToConsoleAync($"Upserted item:\t{response.Resource}"); - await WriteToConsoleAync($"Status code:\t{response.StatusCode}"); - await WriteToConsoleAync($"Request charge:\t{response.RequestCharge:0.00}"); - } - - { - Product item = new( - id: "68719518371", - category: "gear-surf-surfboards", - name: "Kiama Classic Surfboard", - quantity: 25, - price: 790.00m, - clearance: false - ); - - ItemResponseresponse = await container.UpsertItemAsync ( - item: item, - partitionKey: new PartitionKey("gear-surf-surfboards") - ); - // response = await container.UpsertItemAsync ( - item: item, - partitionKey: new PartitionKey("gear-surf-surfboards") - ); - await WriteToConsoleAync($"Upserted item:\t{response.Resource}"); - await WriteToConsoleAync($"Status code:\t{response.StatusCode}"); - await WriteToConsoleAync($"Request charge:\t{response.RequestCharge:0.00}"); - } - - { - // - ItemResponse - await WriteToConsoleAync($"Read item id:\t{response.Resource.id}"); - await WriteToConsoleAync($"Read item:\t{response.Resource}"); - await WriteToConsoleAync($"Status code:\t{response.StatusCode}"); - await WriteToConsoleAync($"Request charge:\t{response.RequestCharge:0.00}"); - } - - { - //response = await container.ReadItemAsync ( - id: "68719518391", - partitionKey: new PartitionKey("gear-surf-surfboards") - ); - // - var query = new QueryDefinition( - query: "SELECT * FROM products p WHERE p.category = @category" - ) - .WithParameter("@category", "gear-surf-surfboards"); - - using FeedIterator - await WriteToConsoleAync($"Ran query:\t{query.QueryText}"); - - //feed = container.GetItemQueryIterator ( - queryDefinition: query - ); - // - List - - foreach(var item in items) - { - await WriteToConsoleAync($"Found item:\t{item.name}\t[{item.id}]"); - } - await WriteToConsoleAync($"Request charge:\t{requestCharge:0.00}"); - } + await cosmosDbService.RunDemoAsync(writeOutputAync: WriteToConsoleAync); await WriteToConsoleAync("Current Status:\tStopping..."); await SetRunAgain(true); diff --git a/src/web/Program.cs b/src/web/Program.cs index 044007d..0e785e2 100644 --- a/src/web/Program.cs +++ b/src/web/Program.cs @@ -37,6 +37,8 @@ }); } +builder.Services.AddTransientitems = new(); - double requestCharge = 0d; - while (feed.HasMoreResults) - { - FeedResponse response = await feed.ReadNextAsync(); - foreach (Product item in response) - { - items.Add(item); - } - requestCharge += response.RequestCharge; - } - // (); + var app = builder.Build(); app.UseDeveloperExceptionPage(); diff --git a/src/web/Services/CosmosDbService.cs b/src/web/Services/CosmosDbService.cs new file mode 100644 index 0000000..9492ed1 --- /dev/null +++ b/src/web/Services/CosmosDbService.cs @@ -0,0 +1,123 @@ +using Cosmos.Samples.NoSQL.Quickstart.Web.Models; +using Microsoft.Azure.Cosmos; + +internal interface ICosmosDbService +{ + Task RunDemoAsync(Func writeOutputAync); + + string GetEndpoint(); +} + +internal sealed class CosmosDbService : ICosmosDbService +{ + private readonly CosmosClient _client; + + public CosmosDbService(CosmosClient client) + { + this._client = client; + } + + public string GetEndpoint() => $"{this._client.Endpoint}"; + + public async Task RunDemoAsync(Func writeOutputAync) + { + // + Database database = this._client.GetDatabase("cosmicworks"); + // + database = await database.ReadAsync(); + await writeOutputAync($"Get database:\t{database.Id}"); + + //+ Container container = database.GetContainer("products"); + // + container = await container.ReadContainerAsync(); + await writeOutputAync($"Get container:\t{container.Id}"); + + { + //+ Product item = new( + id: "68719518391", + category: "gear-surf-surfboards", + name: "Yamba Surfboard", + quantity: 12, + price: 850.00m, + clearance: false + ); + + ItemResponse + await writeOutputAync($"Upserted item:\t{response.Resource}"); + await writeOutputAync($"Status code:\t{response.StatusCode}"); + await writeOutputAync($"Request charge:\t{response.RequestCharge:0.00}"); + } + + { + Product item = new( + id: "68719518371", + category: "gear-surf-surfboards", + name: "Kiama Classic Surfboard", + quantity: 25, + price: 790.00m, + clearance: false + ); + + ItemResponseresponse = await container.UpsertItemAsync ( + item: item, + partitionKey: new PartitionKey("gear-surf-surfboards") + ); + // response = await container.UpsertItemAsync ( + item: item, + partitionKey: new PartitionKey("gear-surf-surfboards") + ); + await writeOutputAync($"Upserted item:\t{response.Resource}"); + await writeOutputAync($"Status code:\t{response.StatusCode}"); + await writeOutputAync($"Request charge:\t{response.RequestCharge:0.00}"); + } + + { + // + ItemResponse + await writeOutputAync($"Read item id:\t{response.Resource.id}"); + await writeOutputAync($"Read item:\t{response.Resource}"); + await writeOutputAync($"Status code:\t{response.StatusCode}"); + await writeOutputAync($"Request charge:\t{response.RequestCharge:0.00}"); + } + + { + //response = await container.ReadItemAsync ( + id: "68719518391", + partitionKey: new PartitionKey("gear-surf-surfboards") + ); + // + var query = new QueryDefinition( + query: "SELECT * FROM products p WHERE p.category = @category" + ) + .WithParameter("@category", "gear-surf-surfboards"); + + using FeedIterator + await writeOutputAync($"Ran query:\t{query.QueryText}"); + + //feed = container.GetItemQueryIterator ( + queryDefinition: query + ); + // + List + + foreach (var item in items) + { + await writeOutputAync($"Found item:\t{item.name}\t[{item.id}]"); + } + await writeOutputAync($"Request charge:\t{requestCharge:0.00}"); + } + } +} \ No newline at end of fileitems = new(); + double requestCharge = 0d; + while (feed.HasMoreResults) + { + FeedResponse response = await feed.ReadNextAsync(); + foreach (Product item in response) + { + items.Add(item); + } + requestCharge += response.RequestCharge; + } + //