From ab0a0c3f53320771c9dc95fcf3908094abbb5848 Mon Sep 17 00:00:00 2001 From: Sidney Andrews Date: Mon, 8 Jan 2024 09:42:44 -0500 Subject: [PATCH] Refactor Cosmos DB service --- src/web/Pages/Index.razor | 103 +---------------------- src/web/Program.cs | 2 + src/web/Services/CosmosDbService.cs | 123 ++++++++++++++++++++++++++++ 3 files changed, 128 insertions(+), 100 deletions(-) create mode 100644 src/web/Services/CosmosDbService.cs 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 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}");
-        }
-
-        {
-            Product item = new(
-                id: "68719518371",
-                category: "gear-surf-surfboards",
-                name: "Kiama Classic Surfboard",
-                quantity: 25,
-                price: 790.00m,
-                clearance: false
-            );
-
-            ItemResponse 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 response = await container.ReadItemAsync(
-                id: "68719518391",
-                partitionKey: new PartitionKey("gear-surf-surfboards")
-            );
-            // 
-            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}");
-        }
-
-        {
-            // 
-            var query = new QueryDefinition(
-                query: "SELECT * FROM products p WHERE p.category = @category"
-            )
-                .WithParameter("@category", "gear-surf-surfboards");
-
-            using FeedIterator feed = container.GetItemQueryIterator(
-                queryDefinition: query
-            );
-            // 
-            await WriteToConsoleAync($"Ran query:\t{query.QueryText}");
-
-            // 
-            List items = new();
-            double requestCharge = 0d;
-            while (feed.HasMoreResults)
-            {
-                FeedResponse response = await feed.ReadNextAsync();
-                foreach (Product item in response)
-                {
-                    items.Add(item);
-                }
-                requestCharge += response.RequestCharge;
-            }
-            // 
-
-            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.AddTransient();
+
 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 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}");
+        }
+
+        {
+            Product item = new(
+                id: "68719518371",
+                category: "gear-surf-surfboards",
+                name: "Kiama Classic Surfboard",
+                quantity: 25,
+                price: 790.00m,
+                clearance: false
+            );
+
+            ItemResponse 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 response = await container.ReadItemAsync(
+                id: "68719518391",
+                partitionKey: new PartitionKey("gear-surf-surfboards")
+            );
+            // 
+            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}");
+        }
+
+        {
+            // 
+            var query = new QueryDefinition(
+                query: "SELECT * FROM products p WHERE p.category = @category"
+            )
+                .WithParameter("@category", "gear-surf-surfboards");
+
+            using FeedIterator feed = container.GetItemQueryIterator(
+                queryDefinition: query
+            );
+            // 
+            await writeOutputAync($"Ran query:\t{query.QueryText}");
+
+            // 
+            List items = new();
+            double requestCharge = 0d;
+            while (feed.HasMoreResults)
+            {
+                FeedResponse response = await feed.ReadNextAsync();
+                foreach (Product item in response)
+                {
+                    items.Add(item);
+                }
+                requestCharge += response.RequestCharge;
+            }
+            // 
+
+            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 file