Skip to content

Commit

Permalink
Merge pull request #24 from Azure-Samples/refactor-cosmos-service
Browse files Browse the repository at this point in the history
Refactor Azure Cosmos DB service
  • Loading branch information
seesharprun authored Jan 8, 2024
2 parents 1c3fa44 + ab0a0c3 commit 3f8f791
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 100 deletions.
103 changes: 3 additions & 100 deletions src/web/Pages/Index.razor
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@page "/"
@inject CosmosClient client
@inject ICosmosDbService cosmosDbService
@using System.Text

<pre class="bg-light text-dark fw-light font-monospace mt-5 p-2">
Expand Down Expand Up @@ -34,7 +34,7 @@
<strong>
ENDPOINT:
</strong>
@client.Endpoint
@cosmosDbService.GetEndpoint()
</small>
</footer>

Expand All @@ -54,104 +54,7 @@

await WriteToConsoleAync("Current Status:\tStarting...");

// <get_database>
Database database = client.GetDatabase("cosmicworks");
// </get_database>
database = await database.ReadAsync();
await WriteToConsoleAync($"Get database:\t{database.Id}");

// <get_container>
Container container = database.GetContainer("products");
// </get_container>
container = await container.ReadContainerAsync();
await WriteToConsoleAync($"Get container:\t{container.Id}");

{
// <create_item>
Product item = new(
id: "68719518391",
category: "gear-surf-surfboards",
name: "Yamba Surfboard",
quantity: 12,
price: 850.00m,
clearance: false
);

ItemResponse<Product> response = await container.UpsertItemAsync<Product>(
item: item,
partitionKey: new PartitionKey("gear-surf-surfboards")
);
// </create_item>
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<Product> response = await container.UpsertItemAsync<Product>(
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}");
}

{
// <read_item>
ItemResponse<Product> response = await container.ReadItemAsync<Product>(
id: "68719518391",
partitionKey: new PartitionKey("gear-surf-surfboards")
);
// </read_item>
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}");
}

{
// <query_items>
var query = new QueryDefinition(
query: "SELECT * FROM products p WHERE p.category = @category"
)
.WithParameter("@category", "gear-surf-surfboards");

using FeedIterator<Product> feed = container.GetItemQueryIterator<Product>(
queryDefinition: query
);
// </query_items>
await WriteToConsoleAync($"Ran query:\t{query.QueryText}");

// <parse_results>
List<Product> items = new();
double requestCharge = 0d;
while (feed.HasMoreResults)
{
FeedResponse<Product> response = await feed.ReadNextAsync();
foreach (Product item in response)
{
items.Add(item);
}
requestCharge += response.RequestCharge;
}
// </parse_results>
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);
Expand Down
2 changes: 2 additions & 0 deletions src/web/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
});
}

builder.Services.AddTransient<ICosmosDbService, CosmosDbService>();

var app = builder.Build();

app.UseDeveloperExceptionPage();
Expand Down
123 changes: 123 additions & 0 deletions src/web/Services/CosmosDbService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
using Cosmos.Samples.NoSQL.Quickstart.Web.Models;
using Microsoft.Azure.Cosmos;

internal interface ICosmosDbService
{
Task RunDemoAsync(Func<string, Task> 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<string, Task> writeOutputAync)
{
// <get_database>
Database database = this._client.GetDatabase("cosmicworks");
// </get_database>
database = await database.ReadAsync();
await writeOutputAync($"Get database:\t{database.Id}");

// <get_container>
Container container = database.GetContainer("products");
// </get_container>
container = await container.ReadContainerAsync();
await writeOutputAync($"Get container:\t{container.Id}");

{
// <create_item>
Product item = new(
id: "68719518391",
category: "gear-surf-surfboards",
name: "Yamba Surfboard",
quantity: 12,
price: 850.00m,
clearance: false
);

ItemResponse<Product> response = await container.UpsertItemAsync<Product>(
item: item,
partitionKey: new PartitionKey("gear-surf-surfboards")
);
// </create_item>
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<Product> response = await container.UpsertItemAsync<Product>(
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}");
}

{
// <read_item>
ItemResponse<Product> response = await container.ReadItemAsync<Product>(
id: "68719518391",
partitionKey: new PartitionKey("gear-surf-surfboards")
);
// </read_item>
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}");
}

{
// <query_items>
var query = new QueryDefinition(
query: "SELECT * FROM products p WHERE p.category = @category"
)
.WithParameter("@category", "gear-surf-surfboards");

using FeedIterator<Product> feed = container.GetItemQueryIterator<Product>(
queryDefinition: query
);
// </query_items>
await writeOutputAync($"Ran query:\t{query.QueryText}");

// <parse_results>
List<Product> items = new();
double requestCharge = 0d;
while (feed.HasMoreResults)
{
FeedResponse<Product> response = await feed.ReadNextAsync();
foreach (Product item in response)
{
items.Add(item);
}
requestCharge += response.RequestCharge;
}
// </parse_results>

foreach (var item in items)
{
await writeOutputAync($"Found item:\t{item.name}\t[{item.id}]");
}
await writeOutputAync($"Request charge:\t{requestCharge:0.00}");
}
}
}

0 comments on commit 3f8f791

Please sign in to comment.