From db791a1183df4d623e622cb32444d9e49a47a158 Mon Sep 17 00:00:00 2001 From: ThompsonNye Date: Thu, 16 Oct 2025 17:23:47 +0200 Subject: [PATCH] Add endpoint to query the system's current time --- .../Endpoints/EndpointExtensions.cs | 1 + src/Vegasco.Server.Api/Info/GetCurrentTime.cs | 21 ++++++++++++ src/Vegasco.Server.Api/Info/GetServerInfo.cs | 2 +- .../Info/GetCurrentTimeTests.cs | 32 +++++++++++++++++++ 4 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 src/Vegasco.Server.Api/Info/GetCurrentTime.cs create mode 100644 tests/Vegasco.Server.Api.Tests.Integration/Info/GetCurrentTimeTests.cs diff --git a/src/Vegasco.Server.Api/Endpoints/EndpointExtensions.cs b/src/Vegasco.Server.Api/Endpoints/EndpointExtensions.cs index 0b5901d..1852cbc 100644 --- a/src/Vegasco.Server.Api/Endpoints/EndpointExtensions.cs +++ b/src/Vegasco.Server.Api/Endpoints/EndpointExtensions.cs @@ -41,5 +41,6 @@ public static class EndpointExtensions .RequireAuthorization(Constants.Authorization.RequireAuthenticatedUserPolicy); GetServerInfo.MapEndpoint(versionedApis); + GetCurrentTime.MapEndpoint(versionedApis); } } diff --git a/src/Vegasco.Server.Api/Info/GetCurrentTime.cs b/src/Vegasco.Server.Api/Info/GetCurrentTime.cs new file mode 100644 index 0000000..601b635 --- /dev/null +++ b/src/Vegasco.Server.Api/Info/GetCurrentTime.cs @@ -0,0 +1,21 @@ +using Microsoft.AspNetCore.Http.HttpResults; + +namespace Vegasco.Server.Api.Info; + +public static class GetCurrentTime +{ + public record Response(DateTimeOffset CurrentTime); + + public static RouteHandlerBuilder MapEndpoint(IEndpointRouteBuilder builder) + { + return builder + .MapGet("info/time", Endpoint) + .WithTags("Info"); + } + + private static Ok Endpoint( + TimeProvider timeProvider) + { + return TypedResults.Ok(new Response(timeProvider.GetUtcNow())); + } +} \ No newline at end of file diff --git a/src/Vegasco.Server.Api/Info/GetServerInfo.cs b/src/Vegasco.Server.Api/Info/GetServerInfo.cs index e188769..735288b 100644 --- a/src/Vegasco.Server.Api/Info/GetServerInfo.cs +++ b/src/Vegasco.Server.Api/Info/GetServerInfo.cs @@ -2,7 +2,7 @@ namespace Vegasco.Server.Api.Info; -public class GetServerInfo +public static class GetServerInfo { public record Response( string FullVersion, diff --git a/tests/Vegasco.Server.Api.Tests.Integration/Info/GetCurrentTimeTests.cs b/tests/Vegasco.Server.Api.Tests.Integration/Info/GetCurrentTimeTests.cs new file mode 100644 index 0000000..b5f2b6b --- /dev/null +++ b/tests/Vegasco.Server.Api.Tests.Integration/Info/GetCurrentTimeTests.cs @@ -0,0 +1,32 @@ +using FluentAssertions; +using FluentAssertions.Extensions; +using System.Net.Http.Json; +using Vegasco.Server.Api.Info; + +namespace Vegasco.Server.Api.Tests.Integration.Info; + +[Collection(SharedTestCollection.Name)] +public sealed class GetCurrentTimeTests +{ + private readonly WebAppFactory _factory; + + public GetCurrentTimeTests(WebAppFactory factory) + { + _factory = factory; + } + + [Fact] + public async Task GetServerInfo_ShouldReturnServerInfo_WhenCalled() + { + // Arrange + + // Act + using HttpResponseMessage response = await _factory.HttpClient.GetAsync("/v1/info/time"); + + // Assert + response.IsSuccessStatusCode.Should().BeTrue(); + GetCurrentTime.Response? timeInfo = await response.Content.ReadFromJsonAsync(); + timeInfo.Should().NotBeNull(); + timeInfo.CurrentTime.Should().BeCloseTo(DateTimeOffset.UtcNow, TimeSpan.FromSeconds(10)); + } +} \ No newline at end of file