Skip to content

Commit

Permalink
Merge pull request #13 from AzureCosmosDB/auto-20241028131138
Browse files Browse the repository at this point in the history
[AUTO] 2024-10-28 | Merge latest from source repo
  • Loading branch information
2 parents 10d1c1a + 72886ed commit 929273e
Show file tree
Hide file tree
Showing 15 changed files with 402 additions and 49 deletions.
24 changes: 24 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS base
USER app
WORKDIR /app
EXPOSE 8080
EXPOSE 8081
FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["web/Microsoft.Samples.Cosmos.NoSQL.Quickstart.Web.csproj", "web/"]
COPY ["models/Microsoft.Samples.Cosmos.NoSQL.Quickstart.Models.csproj", "models/"]
COPY ["services/Microsoft.Samples.Cosmos.NoSQL.Quickstart.Services.csproj", "services/"]
COPY ["Microsoft.Samples.Cosmos.NoSQL.Quickstart.sln", "."]
RUN dotnet restore "Microsoft.Samples.Cosmos.NoSQL.Quickstart.sln"
COPY . .
RUN dotnet build "Microsoft.Samples.Cosmos.NoSQL.Quickstart.sln" -c $BUILD_CONFIGURATION -o /app/build

FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "web/Microsoft.Samples.Cosmos.NoSQL.Quickstart.Web.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Microsoft.Samples.Cosmos.NoSQL.Quickstart.Web.dll"]
34 changes: 34 additions & 0 deletions Microsoft.Samples.Cosmos.NoSQL.Quickstart.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.Samples.Cosmos.NoSQL.Quickstart.Web", "web\Microsoft.Samples.Cosmos.NoSQL.Quickstart.Web.csproj", "{DB1EBDE0-AA02-4D28-8574-EF28DAC09147}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.Samples.Cosmos.NoSQL.Quickstart.Models", "models\Microsoft.Samples.Cosmos.NoSQL.Quickstart.Models.csproj", "{5FC7FBD8-3D5B-4E86-AFA5-DC26BA5CB4D3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.Samples.Cosmos.NoSQL.Quickstart.Services", "services\Microsoft.Samples.Cosmos.NoSQL.Quickstart.Services.csproj", "{BE19E8F8-0FE7-437B-AFD3-F31F854BCE87}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{DB1EBDE0-AA02-4D28-8574-EF28DAC09147}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DB1EBDE0-AA02-4D28-8574-EF28DAC09147}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DB1EBDE0-AA02-4D28-8574-EF28DAC09147}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DB1EBDE0-AA02-4D28-8574-EF28DAC09147}.Release|Any CPU.Build.0 = Release|Any CPU
{5FC7FBD8-3D5B-4E86-AFA5-DC26BA5CB4D3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5FC7FBD8-3D5B-4E86-AFA5-DC26BA5CB4D3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5FC7FBD8-3D5B-4E86-AFA5-DC26BA5CB4D3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5FC7FBD8-3D5B-4E86-AFA5-DC26BA5CB4D3}.Release|Any CPU.Build.0 = Release|Any CPU
{BE19E8F8-0FE7-437B-AFD3-F31F854BCE87}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BE19E8F8-0FE7-437B-AFD3-F31F854BCE87}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BE19E8F8-0FE7-437B-AFD3-F31F854BCE87}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BE19E8F8-0FE7-437B-AFD3-F31F854BCE87}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
10 changes: 10 additions & 0 deletions models/Product.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Microsoft.Samples.Cosmos.NoSQL.Quickstart.Models;

public record Product(
string id,
string category,
string name,
int quantity,
decimal price,
bool clearance
);
105 changes: 105 additions & 0 deletions services/DemoService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
using Microsoft.Azure.Cosmos;
using Microsoft.Samples.Cosmos.NoSQL.Quickstart.Models;
using Microsoft.Samples.Cosmos.NoSQL.Quickstart.Services.Interfaces;

namespace Microsoft.Samples.Cosmos.NoSQL.Quickstart.Services;

public sealed class DemoService(CosmosClient client) : IDemoService
{
public string GetEndpoint() => $"{client.Endpoint}";

public async Task RunAsync(Func<string, Task> writeOutputAync)
{
Database database = 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<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}");
}

{
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}");
}

{
ItemResponse<Product> response = await container.ReadItemAsync<Product>(
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<Product> feed = container.GetItemQueryIterator<Product>(
queryDefinition: query
);

await writeOutputAync($"Ran query:\t{query.QueryText}");

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;
}

foreach (var item in items)
{
await writeOutputAync($"Found item:\t{item.name}\t[{item.id}]");
}
await writeOutputAync($"Request charge:\t{requestCharge:0.00}");
}
}
}
8 changes: 8 additions & 0 deletions services/Interfaces/IDemoService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Microsoft.Samples.Cosmos.NoSQL.Quickstart.Services.Interfaces;

public interface IDemoService
{
Task RunAsync(Func<string, Task> writeOutputAync);

string GetEndpoint();
}
14 changes: 14 additions & 0 deletions services/Microsoft.Samples.Cosmos.NoSQL.Quickstart.Services.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.*" />
<PackageReference Include="Newtonsoft.Json" Version="13.*" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\models\Microsoft.Samples.Cosmos.NoSQL.Quickstart.Models.csproj" />
</ItemGroup>
</Project>
24 changes: 24 additions & 0 deletions web/Components/App.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<base href="/" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1/font/bootstrap-icons.min.css">
<ImportMap />
<link rel="icon" type="image/x-icon" href="@Assets["favicon.ico"]" />
<HeadOutlet />
<PageTitle>
Azure Cosmos DB for NoSQL
</PageTitle>
</head>

<body data-bs-theme="dark">
<Routes />
<script src="_framework/blazor.web.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5/dist/css/bootstrap.min.css">
</body>

</html>
13 changes: 13 additions & 0 deletions web/Components/Layout/MainLayout.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
@inherits LayoutComponentBase

<main class="container">
<h1 class="my-5 text-center">
Azure Cosmos DB for NoSQL | .NET Quickstart
</h1>
@Body
<div id="blazor-error-ui" class="collapse fixed-top rounded-0 mb-0 alert alert-danger" role="alert">
An unhandled error has occurred.
<a href="." class="reload">Reload</a>
<span class="dismiss">🗙</span>
</div>
</main>
97 changes: 97 additions & 0 deletions web/Components/Pages/Home.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
@page "/"
@using System.Text
@inject IDemoService demoService
@rendermode InteractiveServer

<PageTitle>
Azure Cosmos DB for NoSQL | .NET Quickstart
</PageTitle>

<pre class="bg-light text-dark fw-light font-monospace mt-5 p-2">
@ConsoleText
</pre>

<section class="d-flex justify-content-center">
<div class="list-group py-2">
<button type="button" class="list-group-item list-group-item-action @(RunAgainEnabled ? "" : "disabled") active" aria-current="true" @onclick="RunAgainAsync">
<i class="bi bi-arrow-clockwise me-2"></i>
Restart
</button>
<a href="https://learn.microsoft.com/azure/cosmos-db/nosql/quickstart-dotnet#code-examples" class="list-group-item list-group-item-action" target="_blank">
<i class="bi bi-book me-2"></i>
Walk through the code for this sample application
<small class="ms-1 align-top">
<i class="bi bi-box-arrow-up-right" style="font-size: 0.65rem;"></i>
</small>
</a>
<a href="https://learn.microsoft.com/azure/cosmos-db/nosql/" class="list-group-item list-group-item-action" target="_blank">
<i class="bi bi-lightbulb me-2"></i>
Learn more about Azure Cosmos DB for NoSQL
<small class="ms-1 align-top">
<i class="bi bi-box-arrow-up-right" style="font-size: 0.65rem;"></i>
</small>
</a>
</div>
</section>

<footer class="fixed-bottom bg-secondary bg-body-secondary border">
<small class="px-5 fw-light fst-italic">
<strong>
ENDPOINT:
</strong>
@demoService.GetEndpoint()
</small>
</footer>

@code {
private StringBuilder _console = new();

private string ConsoleText
{
get => _console.ToString();
}

private bool RunAgainEnabled { get; set; }

protected override async Task OnInitializedAsync()
{
await GenerateQueryDataAsync();
}

private async Task RunAgainAsync()
{
await GenerateQueryDataAsync();
}

private async Task GenerateQueryDataAsync()
{
await SetRunAgain(false);
await ClearConsoleAsync();

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

await demoService.RunAsync(writeOutputAync: WriteToConsoleAync);

await WriteToConsoleAync("Current Status:\tStopping...");
await SetRunAgain(true);
}

private async Task WriteToConsoleAync(string message)
{
_console.AppendLine(message);
await InvokeAsync(StateHasChanged);
}

private async Task ClearConsoleAsync()
{
_console.Clear();
await InvokeAsync(StateHasChanged);
}

private async Task SetRunAgain(bool state)
{
RunAgainEnabled = state;
await InvokeAsync(StateHasChanged);
}
}

11 changes: 11 additions & 0 deletions web/Components/Routes.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Router AppAssembly="typeof(Program).Assembly">
<Found Context="routeData">
<RouteView RouteData="routeData" DefaultLayout="typeof(Layout.MainLayout)" />
</Found>
<NotFound>
<PageTitle>Not found</PageTitle>
<LayoutView Layout="@typeof(Layout.MainLayout)">
<p role="alert">Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>
12 changes: 12 additions & 0 deletions web/Components/_Imports.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
@using System.Net.Http
@using System.Net.Http.Json
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.Web
@using static Microsoft.AspNetCore.Components.Web.RenderMode
@using Microsoft.AspNetCore.Components.Web.Virtualization
@using Microsoft.JSInterop
@using Microsoft.Samples.Cosmos.NoSQL.Quickstart.Models
@using Microsoft.Samples.Cosmos.NoSQL.Quickstart.Services.Interfaces
@using Microsoft.Samples.Cosmos.NoSQL.Quickstart.Web
@using Microsoft.Samples.Cosmos.NoSQL.Quickstart.Web.Components
17 changes: 17 additions & 0 deletions web/Microsoft.Samples.Cosmos.NoSQL.Quickstart.Web.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>f6167579-5a7c-405e-bdae-cf20a79d6b9d</UserSecretsId>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Azure.Identity" Version="1.12.1" />
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.*" />
<PackageReference Include="Newtonsoft.Json" Version="13.*" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\models\Microsoft.Samples.Cosmos.NoSQL.Quickstart.Models.csproj" />
<ProjectReference Include="..\services\Microsoft.Samples.Cosmos.NoSQL.Quickstart.Services.csproj" />
</ItemGroup>
</Project>
Loading

0 comments on commit 929273e

Please sign in to comment.