From 72886ed86b108066120cff0e6388472ff038025f Mon Sep 17 00:00:00 2001 From: "portal-quickstart-synchronizer[bot]" <184439314+portal-quickstart-synchronizer[bot]@users.noreply.github.com> Date: Mon, 28 Oct 2024 13:11:38 +0000 Subject: [PATCH] Automatically generated commit --- Dockerfile | 24 ++++ Microsoft.Samples.Cosmos.NoSQL.Quickstart.sln | 34 ++++++ ...ples.Cosmos.NoSQL.Quickstart.Models.csproj | 7 ++ models/Product.cs | 10 ++ services/DemoService.cs | 105 ++++++++++++++++++ services/Interfaces/IDemoService.cs | 8 ++ ...es.Cosmos.NoSQL.Quickstart.Services.csproj | 14 +++ web/Components/App.razor | 24 ++++ web/Components/Layout/MainLayout.razor | 13 +++ web/Components/Pages/Home.razor | 97 ++++++++++++++++ web/Components/Routes.razor | 11 ++ web/Components/_Imports.razor | 12 ++ ...Samples.Cosmos.NoSQL.Quickstart.Web.csproj | 17 +++ web/Program.cs | 14 ++- web/Properties/launchSettings.json | 61 +++------- 15 files changed, 402 insertions(+), 49 deletions(-) create mode 100644 Dockerfile create mode 100644 Microsoft.Samples.Cosmos.NoSQL.Quickstart.sln create mode 100644 models/Microsoft.Samples.Cosmos.NoSQL.Quickstart.Models.csproj create mode 100644 models/Product.cs create mode 100644 services/DemoService.cs create mode 100644 services/Interfaces/IDemoService.cs create mode 100644 services/Microsoft.Samples.Cosmos.NoSQL.Quickstart.Services.csproj create mode 100644 web/Components/App.razor create mode 100644 web/Components/Layout/MainLayout.razor create mode 100644 web/Components/Pages/Home.razor create mode 100644 web/Components/Routes.razor create mode 100644 web/Components/_Imports.razor create mode 100644 web/Microsoft.Samples.Cosmos.NoSQL.Quickstart.Web.csproj diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..79aea07 --- /dev/null +++ b/Dockerfile @@ -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"] \ No newline at end of file diff --git a/Microsoft.Samples.Cosmos.NoSQL.Quickstart.sln b/Microsoft.Samples.Cosmos.NoSQL.Quickstart.sln new file mode 100644 index 0000000..d1dd916 --- /dev/null +++ b/Microsoft.Samples.Cosmos.NoSQL.Quickstart.sln @@ -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 diff --git a/models/Microsoft.Samples.Cosmos.NoSQL.Quickstart.Models.csproj b/models/Microsoft.Samples.Cosmos.NoSQL.Quickstart.Models.csproj new file mode 100644 index 0000000..80f16bc --- /dev/null +++ b/models/Microsoft.Samples.Cosmos.NoSQL.Quickstart.Models.csproj @@ -0,0 +1,7 @@ + + + net9.0 + enable + enable + + \ No newline at end of file diff --git a/models/Product.cs b/models/Product.cs new file mode 100644 index 0000000..0d5ba04 --- /dev/null +++ b/models/Product.cs @@ -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 +); diff --git a/services/DemoService.cs b/services/DemoService.cs new file mode 100644 index 0000000..28a60bc --- /dev/null +++ b/services/DemoService.cs @@ -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 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 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}"); + } + } +} diff --git a/services/Interfaces/IDemoService.cs b/services/Interfaces/IDemoService.cs new file mode 100644 index 0000000..a2574b4 --- /dev/null +++ b/services/Interfaces/IDemoService.cs @@ -0,0 +1,8 @@ +namespace Microsoft.Samples.Cosmos.NoSQL.Quickstart.Services.Interfaces; + +public interface IDemoService +{ + Task RunAsync(Func writeOutputAync); + + string GetEndpoint(); +} \ No newline at end of file diff --git a/services/Microsoft.Samples.Cosmos.NoSQL.Quickstart.Services.csproj b/services/Microsoft.Samples.Cosmos.NoSQL.Quickstart.Services.csproj new file mode 100644 index 0000000..3a8972d --- /dev/null +++ b/services/Microsoft.Samples.Cosmos.NoSQL.Quickstart.Services.csproj @@ -0,0 +1,14 @@ + + + net9.0 + enable + enable + + + + + + + + + \ No newline at end of file diff --git a/web/Components/App.razor b/web/Components/App.razor new file mode 100644 index 0000000..3767ae4 --- /dev/null +++ b/web/Components/App.razor @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + Azure Cosmos DB for NoSQL + + + + + + + + + + diff --git a/web/Components/Layout/MainLayout.razor b/web/Components/Layout/MainLayout.razor new file mode 100644 index 0000000..a31dae5 --- /dev/null +++ b/web/Components/Layout/MainLayout.razor @@ -0,0 +1,13 @@ +@inherits LayoutComponentBase + +
+

+ Azure Cosmos DB for NoSQL | .NET Quickstart +

+ @Body + +
diff --git a/web/Components/Pages/Home.razor b/web/Components/Pages/Home.razor new file mode 100644 index 0000000..9f68c41 --- /dev/null +++ b/web/Components/Pages/Home.razor @@ -0,0 +1,97 @@ +@page "/" +@using System.Text +@inject IDemoService demoService +@rendermode InteractiveServer + + + Azure Cosmos DB for NoSQL | .NET Quickstart + + +
+    @ConsoleText
+
+ +
+ +
+ +
+ + + ENDPOINT: + + @demoService.GetEndpoint() + +
+ +@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); + } +} + diff --git a/web/Components/Routes.razor b/web/Components/Routes.razor new file mode 100644 index 0000000..2f49ee3 --- /dev/null +++ b/web/Components/Routes.razor @@ -0,0 +1,11 @@ + + + + + + Not found + +

Sorry, there's nothing at this address.

+
+
+
diff --git a/web/Components/_Imports.razor b/web/Components/_Imports.razor new file mode 100644 index 0000000..690590f --- /dev/null +++ b/web/Components/_Imports.razor @@ -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 diff --git a/web/Microsoft.Samples.Cosmos.NoSQL.Quickstart.Web.csproj b/web/Microsoft.Samples.Cosmos.NoSQL.Quickstart.Web.csproj new file mode 100644 index 0000000..952768b --- /dev/null +++ b/web/Microsoft.Samples.Cosmos.NoSQL.Quickstart.Web.csproj @@ -0,0 +1,17 @@ + + + net9.0 + enable + enable + f6167579-5a7c-405e-bdae-cf20a79d6b9d + + + + + + + + + + + diff --git a/web/Program.cs b/web/Program.cs index d4f9e3f..1f471dd 100644 --- a/web/Program.cs +++ b/web/Program.cs @@ -1,9 +1,12 @@ using Azure.Identity; using Microsoft.Azure.Cosmos; +using Microsoft.Samples.Cosmos.NoSQL.Quickstart.Services; +using Microsoft.Samples.Cosmos.NoSQL.Quickstart.Services.Interfaces; +using Microsoft.Samples.Cosmos.NoSQL.Quickstart.Web.Components; var builder = WebApplication.CreateBuilder(args); -builder.Services.AddRazorPages(); -builder.Services.AddServerSideBlazor(); + +builder.Services.AddRazorComponents().AddInteractiveServerComponents(); builder.Services.AddSingleton((_) => { @@ -21,11 +24,10 @@ app.UseHttpsRedirection(); -app.UseStaticFiles(); +app.UseAntiforgery(); -app.UseRouting(); +app.MapStaticAssets(); -app.MapBlazorHub(); -app.MapFallbackToPage("/_Host"); +app.MapRazorComponents().AddInteractiveServerRenderMode(); app.Run(); diff --git a/web/Properties/launchSettings.json b/web/Properties/launchSettings.json index 6ed4af8..6511432 100644 --- a/web/Properties/launchSettings.json +++ b/web/Properties/launchSettings.json @@ -1,48 +1,23 @@ { - "profiles": { - "http": { - "commandName": "Project", - "launchBrowser": true, - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" + "$schema": "https://json.schemastore.org/launchsettings.json", + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "http://localhost:5220", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } }, - "dotnetRunMessages": true, - "applicationUrl": "http://localhost:5096" - }, - "https": { - "commandName": "Project", - "launchBrowser": true, - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" - }, - "dotnetRunMessages": true, - "applicationUrl": "https://localhost:7186;http://localhost:5096" - }, - "IIS Express": { - "commandName": "IISExpress", - "launchBrowser": true, - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "https://localhost:7228;http://localhost:5220", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } } - }, - "Docker": { - "commandName": "Docker", - "launchBrowser": true, - "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}", - "environmentVariables": { - "ASPNETCORE_HTTPS_PORTS": "8081", - "ASPNETCORE_HTTP_PORTS": "8080" - }, - "publishAllPorts": true, - "useSSL": true - } - }, - "iisSettings": { - "windowsAuthentication": false, - "anonymousAuthentication": true, - "iisExpress": { - "applicationUrl": "http://localhost:2152", - "sslPort": 44370 } } -} \ No newline at end of file