Add Clock page with live DB time and TimeApiClient service
Introduced a new Clock page that displays and updates the current database server time every second by calling a backend API. Added the TimeApiClient service to handle API requests for the server time. Registered TimeApiClient in Program.cs and updated the navigation menu to include a link to the new Clock page. Includes error handling and custom UI styling for the clock display.
This commit is contained in:
27
DbFirst.BlazorWebApp/Services/TimeApiClient.cs
Normal file
27
DbFirst.BlazorWebApp/Services/TimeApiClient.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using System.Net.Http.Json;
|
||||
|
||||
namespace DbFirst.BlazorWebApp.Services;
|
||||
|
||||
public class TimeApiClient
|
||||
{
|
||||
private readonly HttpClient _httpClient;
|
||||
private const string Endpoint = "api/time";
|
||||
|
||||
public TimeApiClient(HttpClient httpClient)
|
||||
{
|
||||
_httpClient = httpClient;
|
||||
}
|
||||
|
||||
public async Task<DateTime?> InsertAndGetLastAsync()
|
||||
{
|
||||
var response = await _httpClient.PostAsync(Endpoint, null);
|
||||
response.EnsureSuccessStatusCode();
|
||||
var result = await response.Content.ReadFromJsonAsync<TimeResponse>();
|
||||
return result?.Now;
|
||||
}
|
||||
|
||||
private sealed class TimeResponse
|
||||
{
|
||||
public DateTime? Now { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user