|
| 1 | +namespace ParcelRegistry.Projector.Snapshots |
| 2 | +{ |
| 3 | + using System.Threading; |
| 4 | + using System.Threading.Tasks; |
| 5 | + using Asp.Versioning; |
| 6 | + using Be.Vlaanderen.Basisregisters.Api; |
| 7 | + using Dapper; |
| 8 | + using Microsoft.AspNetCore.Mvc; |
| 9 | + using Microsoft.Data.SqlClient; |
| 10 | + using Microsoft.Extensions.Configuration; |
| 11 | + |
| 12 | + [ApiVersion("1.0")] |
| 13 | + [ApiRoute("snapshots")] |
| 14 | + public class SnapshotsController : ApiController |
| 15 | + { |
| 16 | + private const string SnapshotConnectionStringKey = "Events"; |
| 17 | + private const string SnapshotVerificationsTableName = "[ParcelRegistry].[SnapshotVerificationStates]"; |
| 18 | + private const string SnaphotsTableName = "[ParcelRegistry].[Snapshots]"; |
| 19 | + |
| 20 | + [HttpGet] |
| 21 | + public async Task<IActionResult> Get( |
| 22 | + [FromServices] IConfiguration configuration, |
| 23 | + CancellationToken cancellationToken) |
| 24 | + { |
| 25 | + await using var sqlConnection = new SqlConnection(configuration.GetConnectionString(SnapshotConnectionStringKey)); |
| 26 | + var failedSnapshotCount = |
| 27 | + await sqlConnection.QuerySingleAsync<int>( |
| 28 | + $"SELECT Count(*) FROM {SnapshotVerificationsTableName} WHERE Status = 'Failed'", |
| 29 | + cancellationToken); |
| 30 | + |
| 31 | + var differenceInDaysOfLastVerification = |
| 32 | + await sqlConnection.QuerySingleAsync<int>( |
| 33 | + $"SELECT DATEDIFF(DAY," + |
| 34 | + $"(SELECT Created FROM {SnaphotsTableName} WHERE Id = (SELECT MAX(SnapshotId) FROM {SnapshotVerificationsTableName}))," + |
| 35 | + $"(SELECT MAX(Created) FROM [ParcelRegistry].[Snapshots])) As DaysDiff", |
| 36 | + cancellationToken); |
| 37 | + |
| 38 | + return Ok(new SnapshotStatusResponse(failedSnapshotCount, differenceInDaysOfLastVerification)); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + public sealed class SnapshotStatusResponse |
| 43 | + { |
| 44 | + public int FailedSnapshotsCount { get; set; } |
| 45 | + public int DifferenceInDaysOfLastVerification { get; set; } |
| 46 | + |
| 47 | + public SnapshotStatusResponse(int failedSnapshotsCount, int differenceInDaysOfLastVerification) |
| 48 | + { |
| 49 | + FailedSnapshotsCount = failedSnapshotsCount; |
| 50 | + DifferenceInDaysOfLastVerification = differenceInDaysOfLastVerification; |
| 51 | + } |
| 52 | + } |
| 53 | +} |
0 commit comments