using System;
using System.Threading;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using PolyTrader.Core;
using PolyTrader.Web.Updates;
namespace PolyTrader.Web.Endpoints
{
///
/// Update-Status im WebUI (#565): laufende und verfügbare Fassung, Hinweis auf ein Sicherheitsupdate.
///
/// Kein Installieren über das WebUI. Die Installation startet den Update-Agent und beendet
/// danach die Anwendung – und mit ihr den Webserver. Richards Vorgabe beschränkt das Beenden über das
/// WebUI auf das Trading (#563). Installiert wird im Desktop.
///
public static class UpdatesEndpoints
{
public static IEndpointRouteBuilder MapUpdatesEndpoints(this IEndpointRouteBuilder routes)
{
routes.MapGet("/api/updates", async (IServiceProvider sp, bool? refresh, CancellationToken ct) =>
{
var service = sp.GetService();
if (service == null)
return Results.Problem("Die Update-Prüfung ist nicht verfügbar.", statusCode: StatusCodes.Status503ServiceUnavailable);
var (snapshot, fromCache, retryAfter) = await service.GetAsync(refresh == true, ct);
return Results.Ok(new
{
currentVersion = BuildInfo.Version,
currentBuild = BuildInfo.Summary,
enabled = snapshot.Enabled,
updateAvailable = snapshot.UpdateAvailable,
latestVersion = snapshot.LatestVersion,
changelog = snapshot.Changelog,
isCritical = snapshot.IsCritical,
unauthorized = snapshot.Unauthorized,
error = snapshot.Error,
checkedAt = snapshot.CheckedAtUtc.ToString("o"),
fromCache,
retryAfterSeconds = retryAfter.HasValue ? (int?)Math.Ceiling(retryAfter.Value.TotalSeconds) : null,
installVia = "desktop"
});
});
return routes;
}
}
}