From 4854a84db8d2f55171a35941bd1662823f4aec11 Mon Sep 17 00:00:00 2001 From: emalfroy Date: Wed, 7 Feb 2024 14:02:50 +0100 Subject: [PATCH] feat: add backoffice projections status --- .../BackOffice/BackOfficeController.cs | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/StreetNameRegistry.Projector/BackOffice/BackOfficeController.cs diff --git a/src/StreetNameRegistry.Projector/BackOffice/BackOfficeController.cs b/src/StreetNameRegistry.Projector/BackOffice/BackOfficeController.cs new file mode 100644 index 000000000..710d6ecd9 --- /dev/null +++ b/src/StreetNameRegistry.Projector/BackOffice/BackOfficeController.cs @@ -0,0 +1,56 @@ +namespace StreetNameRegistry.Projector.BackOffice +{ + using System.Collections.Generic; + using System.Linq; + using System.Threading; + using System.Threading.Tasks; + using Be.Vlaanderen.Basisregisters.Api; + using Be.Vlaanderen.Basisregisters.ProjectionHandling.Runner.ProjectionStates; + using Dapper; + using Microsoft.AspNetCore.Mvc; + using Microsoft.Data.SqlClient; + using Microsoft.Extensions.Configuration; + using SqlStreamStore; + + [ApiVersion("1.0")] + [ApiRoute("backoffice")] + public class BackOfficeController : ApiController + { + private const string? BackOfficeConnectionStringKey = "Events"; + + [HttpGet] + public async Task Get( + [FromServices] IConfiguration configuration, + [FromServices] IStreamStore streamStore, + CancellationToken cancellationToken) + { + var streamPosition = await streamStore.ReadHeadPosition(cancellationToken); + + List response; + await using (var sqlConnection = new SqlConnection(configuration.GetConnectionString(BackOfficeConnectionStringKey))) + { + var result = + await sqlConnection.QueryAsync( + "SELECT * FROM [StreetNameRegistryBackOfficeProjections].[ProjectionStates]", cancellationToken); + + response = result.Select(x => new BackOfficeStatusResponse(x, streamPosition)).ToList(); + } + + return Ok(response); + } + } + + public class BackOfficeStatusResponse + { + public long MaxPosition { get; set; } + public string ProjectionName { get; set; } + public long Position { get; set; } + + public BackOfficeStatusResponse(ProjectionStateItem projectionStateItem, long maxPosition) + { + MaxPosition = maxPosition; + ProjectionName = projectionStateItem.Name; + Position = projectionStateItem.Position; + } + } +}