From 75daebf3ef3e64ee8577565e6f07840b53f219c4 Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Thu, 6 Jun 2024 17:48:21 +0200 Subject: [PATCH] Change Blazor app to show rankings from server --- .../BlazorRankingClient.csproj | 18 +++++ .../Components/Layout/NavMenu.razor | 4 +- .../Components/Pages/Ranking.razor | 46 +++++++++++++ .../Components/Pages/Weather.razor | 64 ------------------ .../BlazorRankingClient/OpenAPIs/swagger.json | 66 +++++++++++++++++++ 5 files changed, 132 insertions(+), 66 deletions(-) create mode 100644 Tasks/Lab13/BlazorRankingClient/Components/Pages/Ranking.razor delete mode 100644 Tasks/Lab13/BlazorRankingClient/Components/Pages/Weather.razor create mode 100644 Tasks/Lab13/BlazorRankingClient/OpenAPIs/swagger.json diff --git a/Tasks/Lab13/BlazorRankingClient/BlazorRankingClient.csproj b/Tasks/Lab13/BlazorRankingClient/BlazorRankingClient.csproj index 1b28a01..768c365 100644 --- a/Tasks/Lab13/BlazorRankingClient/BlazorRankingClient.csproj +++ b/Tasks/Lab13/BlazorRankingClient/BlazorRankingClient.csproj @@ -6,4 +6,22 @@ enable + + + http://localhost:5072/swagger/v1/swagger.json + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + diff --git a/Tasks/Lab13/BlazorRankingClient/Components/Layout/NavMenu.razor b/Tasks/Lab13/BlazorRankingClient/Components/Layout/NavMenu.razor index cf3f8eb..85d9957 100644 --- a/Tasks/Lab13/BlazorRankingClient/Components/Layout/NavMenu.razor +++ b/Tasks/Lab13/BlazorRankingClient/Components/Layout/NavMenu.razor @@ -21,8 +21,8 @@ diff --git a/Tasks/Lab13/BlazorRankingClient/Components/Pages/Ranking.razor b/Tasks/Lab13/BlazorRankingClient/Components/Pages/Ranking.razor new file mode 100644 index 0000000..3fd85bd --- /dev/null +++ b/Tasks/Lab13/BlazorRankingClient/Components/Pages/Ranking.razor @@ -0,0 +1,46 @@ +@page "/ranking" +@using RankingService +@attribute [StreamRendering] + +Ranking + +

Ranking

+ +

This component demonstrates showing data.

+ +@if (rankings == null) +{ +

Loading...

+} +else +{ + + + + + + + + + + @for (int i = 0; i < rankings.Length; i++) + { + + + + + + } + +
#NameTime
@(i + 1)@rankings[i].Name@rankings[i].Time
+} + +@code { + private RankingClient client = new RankingClient("http://localhost:5072", new HttpClient()); + private Competitor[]? rankings; + + protected override async Task OnInitializedAsync() + { + rankings = (await client.RankingsAsync()).ToArray(); + } +} diff --git a/Tasks/Lab13/BlazorRankingClient/Components/Pages/Weather.razor b/Tasks/Lab13/BlazorRankingClient/Components/Pages/Weather.razor deleted file mode 100644 index 43a1ecb..0000000 --- a/Tasks/Lab13/BlazorRankingClient/Components/Pages/Weather.razor +++ /dev/null @@ -1,64 +0,0 @@ -@page "/weather" -@attribute [StreamRendering] - -Weather - -

Weather

- -

This component demonstrates showing data.

- -@if (forecasts == null) -{ -

Loading...

-} -else -{ - - - - - - - - - - - @foreach (var forecast in forecasts) - { - - - - - - - } - -
DateTemp. (C)Temp. (F)Summary
@forecast.Date.ToShortDateString()@forecast.TemperatureC@forecast.TemperatureF@forecast.Summary
-} - -@code { - private WeatherForecast[]? forecasts; - - protected override async Task OnInitializedAsync() - { - // Simulate asynchronous loading to demonstrate streaming rendering - await Task.Delay(500); - - var startDate = DateOnly.FromDateTime(DateTime.Now); - var summaries = new[] { "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" }; - forecasts = Enumerable.Range(1, 5).Select(index => new WeatherForecast - { - Date = startDate.AddDays(index), - TemperatureC = Random.Shared.Next(-20, 55), - Summary = summaries[Random.Shared.Next(summaries.Length)] - }).ToArray(); - } - - private class WeatherForecast - { - public DateOnly Date { get; set; } - public int TemperatureC { get; set; } - public string? Summary { get; set; } - public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); - } -} diff --git a/Tasks/Lab13/BlazorRankingClient/OpenAPIs/swagger.json b/Tasks/Lab13/BlazorRankingClient/OpenAPIs/swagger.json new file mode 100644 index 0000000..3838c70 --- /dev/null +++ b/Tasks/Lab13/BlazorRankingClient/OpenAPIs/swagger.json @@ -0,0 +1,66 @@ +{ + "openapi": "3.0.1", + "info": { + "title": "Ranking API", + "description": "List rankings", + "version": "v1" + }, + "paths": { + "/Rankings": { + "get": { + "tags": [ + "Rankings" + ], + "responses": { + "200": { + "description": "OK", + "content": { + "text/plain": { + "schema": { + "type": "array", + "items": { + "$ref": "#/components/schemas/Competitor" + } + } + }, + "application/json": { + "schema": { + "type": "array", + "items": { + "$ref": "#/components/schemas/Competitor" + } + } + }, + "text/json": { + "schema": { + "type": "array", + "items": { + "$ref": "#/components/schemas/Competitor" + } + } + } + } + } + } + } + } + }, + "components": { + "schemas": { + "Competitor": { + "type": "object", + "properties": { + "name": { + "type": "string", + "nullable": true + }, + "time": { + "type": "string", + "nullable": true + } + }, + "additionalProperties": false + } + } + } +} \ No newline at end of file