Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add snapshot on request #1282

Merged
merged 1 commit into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions paket.dependencies
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,18 @@ nuget Npgsql.EntityFrameworkCore.PostgreSQL.Design 2.0.0-preview1
nuget Be.Vlaanderen.Basisregisters.Build.Pipeline 7.2.5
nuget Be.Vlaanderen.Basisregisters.Testing.Infrastructure.Events 4.0.0 content: true, copy_content_to_output_dir: always, copy_local: true

nuget Be.Vlaanderen.Basisregisters.AggregateSource 9.0.1
nuget Be.Vlaanderen.Basisregisters.AggregateSource.ExplicitRouting 9.0.1
nuget Be.Vlaanderen.Basisregisters.AggregateSource.SqlStreamStore.Autofac 9.0.1
nuget Be.Vlaanderen.Basisregisters.AggregateSource.Testing 9.0.1
nuget Be.Vlaanderen.Basisregisters.AggregateSource.Testing.SqlStreamStore.Autofac 9.0.1
nuget Be.Vlaanderen.Basisregisters.AggregateSource.Testing.Xunit 9.0.1

nuget Be.Vlaanderen.Basisregisters.CommandHandling 9.0.1
nuget Be.Vlaanderen.Basisregisters.CommandHandling.Idempotency 9.0.1
nuget Be.Vlaanderen.Basisregisters.CommandHandling.SqlStreamStore 9.0.1

nuget Be.Vlaanderen.Basisregisters.Snapshotverifier 9.0.1
nuget Be.Vlaanderen.Basisregisters.AggregateSource 9.1.1
nuget Be.Vlaanderen.Basisregisters.AggregateSource.ExplicitRouting 9.1.1
nuget Be.Vlaanderen.Basisregisters.AggregateSource.SqlStreamStore.Autofac 9.1.1
nuget Be.Vlaanderen.Basisregisters.AggregateSource.Testing 9.1.1
nuget Be.Vlaanderen.Basisregisters.AggregateSource.Testing.SqlStreamStore.Autofac 9.1.1
nuget Be.Vlaanderen.Basisregisters.AggregateSource.Testing.Xunit 9.1.1

nuget Be.Vlaanderen.Basisregisters.CommandHandling 9.1.1
nuget Be.Vlaanderen.Basisregisters.CommandHandling.Idempotency 9.1.1
nuget Be.Vlaanderen.Basisregisters.CommandHandling.SqlStreamStore 9.1.1

nuget Be.Vlaanderen.Basisregisters.Snapshotverifier 9.1.1

nuget Be.Vlaanderen.Basisregisters.Api 23.1.0

Expand Down
60 changes: 30 additions & 30 deletions paket.lock
Original file line number Diff line number Diff line change
Expand Up @@ -63,44 +63,44 @@ NUGET
System.Security.Cryptography.ProtectedData (>= 4.7)
System.Text.Json (>= 4.7.2)
System.Threading.Tasks.Extensions (>= 4.5.4)
Be.Vlaanderen.Basisregisters.AggregateSource (9.0.1)
Be.Vlaanderen.Basisregisters.AggregateSource (9.1.1)
Be.Vlaanderen.Basisregisters.Utilities.HashCodeCalculator (>= 4.0)
Be.Vlaanderen.Basisregisters.Utilities.HexByteConvertor (>= 4.0)
Be.Vlaanderen.Basisregisters.Utilities.ToStringBuilder (>= 4.0)
Newtonsoft.Json (>= 13.0.3)
NodaTime (>= 3.1.11)
Be.Vlaanderen.Basisregisters.AggregateSource.ExplicitRouting (9.0.1)
Be.Vlaanderen.Basisregisters.AggregateSource (9.0.1)
Be.Vlaanderen.Basisregisters.AggregateSource.SqlStreamStore (9.0.1)
Be.Vlaanderen.Basisregisters.AggregateSource (9.0.1)
Be.Vlaanderen.Basisregisters.AggregateSource.ExplicitRouting (9.1.1)
Be.Vlaanderen.Basisregisters.AggregateSource (9.1.1)
Be.Vlaanderen.Basisregisters.AggregateSource.SqlStreamStore (9.1.1)
Be.Vlaanderen.Basisregisters.AggregateSource (9.1.1)
Be.Vlaanderen.Basisregisters.EventHandling (>= 5.0)
SqlStreamStore (>= 1.2)
Be.Vlaanderen.Basisregisters.AggregateSource.SqlStreamStore.Autofac (9.0.1)
Be.Vlaanderen.Basisregisters.AggregateSource.SqlStreamStore.Autofac (9.1.1)
Autofac.Extensions.DependencyInjection (>= 9.0)
Be.Vlaanderen.Basisregisters.AggregateSource (9.0.1)
Be.Vlaanderen.Basisregisters.AggregateSource.SqlStreamStore (9.0.1)
Be.Vlaanderen.Basisregisters.AggregateSource (9.1.1)
Be.Vlaanderen.Basisregisters.AggregateSource.SqlStreamStore (9.1.1)
SqlStreamStore (>= 1.2)
SqlStreamStore.MsSql (>= 1.2)
Be.Vlaanderen.Basisregisters.AggregateSource.Testing (9.0.1)
Be.Vlaanderen.Basisregisters.AggregateSource (9.0.1)
Be.Vlaanderen.Basisregisters.AggregateSource.Testing (9.1.1)
Be.Vlaanderen.Basisregisters.AggregateSource (9.1.1)
CompareNETObjects (>= 4.83)
Microsoft.CSharp (>= 4.7)
Newtonsoft.Json (>= 13.0.3)
Be.Vlaanderen.Basisregisters.AggregateSource.Testing.CommandHandling (9.0.1)
Be.Vlaanderen.Basisregisters.AggregateSource.Testing (9.0.1)
Be.Vlaanderen.Basisregisters.CommandHandling (9.0.1)
Be.Vlaanderen.Basisregisters.AggregateSource.Testing.SqlStreamStore (9.0.1)
Be.Vlaanderen.Basisregisters.AggregateSource.Testing (9.0.1)
Be.Vlaanderen.Basisregisters.AggregateSource.Testing.CommandHandling (9.1.1)
Be.Vlaanderen.Basisregisters.AggregateSource.Testing (9.1.1)
Be.Vlaanderen.Basisregisters.CommandHandling (9.1.1)
Be.Vlaanderen.Basisregisters.AggregateSource.Testing.SqlStreamStore (9.1.1)
Be.Vlaanderen.Basisregisters.AggregateSource.Testing (9.1.1)
Be.Vlaanderen.Basisregisters.EventHandling (>= 5.0)
SqlStreamStore (>= 1.2)
Be.Vlaanderen.Basisregisters.AggregateSource.Testing.SqlStreamStore.Autofac (9.0.1)
Be.Vlaanderen.Basisregisters.AggregateSource.Testing.SqlStreamStore.Autofac (9.1.1)
Autofac.Extensions.DependencyInjection (>= 9.0)
Be.Vlaanderen.Basisregisters.AggregateSource.SqlStreamStore.Autofac (9.0.1)
Be.Vlaanderen.Basisregisters.AggregateSource.Testing.CommandHandling (9.0.1)
Be.Vlaanderen.Basisregisters.AggregateSource.Testing.SqlStreamStore (9.0.1)
Be.Vlaanderen.Basisregisters.AggregateSource.Testing.Xunit (9.0.1)
Be.Vlaanderen.Basisregisters.AggregateSource.Testing.Xunit (9.0.1)
Be.Vlaanderen.Basisregisters.AggregateSource.Testing (9.0.1)
Be.Vlaanderen.Basisregisters.AggregateSource.SqlStreamStore.Autofac (9.1.1)
Be.Vlaanderen.Basisregisters.AggregateSource.Testing.CommandHandling (9.1.1)
Be.Vlaanderen.Basisregisters.AggregateSource.Testing.SqlStreamStore (9.1.1)
Be.Vlaanderen.Basisregisters.AggregateSource.Testing.Xunit (9.1.1)
Be.Vlaanderen.Basisregisters.AggregateSource.Testing.Xunit (9.1.1)
Be.Vlaanderen.Basisregisters.AggregateSource.Testing (9.1.1)
Microsoft.Extensions.Logging (>= 8.0)
xunit (>= 2.7)
Be.Vlaanderen.Basisregisters.Api (23.1)
Expand Down Expand Up @@ -224,19 +224,19 @@ NUGET
Microsoft.Data.SqlClient (>= 5.2)
System.Collections.Immutable (>= 8.0)
Be.Vlaanderen.Basisregisters.Build.Pipeline (7.2.5)
Be.Vlaanderen.Basisregisters.CommandHandling (9.0.1)
Be.Vlaanderen.Basisregisters.CommandHandling.Idempotency (9.0.1)
Be.Vlaanderen.Basisregisters.CommandHandling (9.0.1)
Be.Vlaanderen.Basisregisters.CommandHandling (9.1.1)
Be.Vlaanderen.Basisregisters.CommandHandling.Idempotency (9.1.1)
Be.Vlaanderen.Basisregisters.CommandHandling (9.1.1)
Be.Vlaanderen.Basisregisters.Converters.Timestamp (>= 4.0)
Be.Vlaanderen.Basisregisters.Utilities.HexByteConvertor (>= 4.0)
Microsoft.Data.SqlClient (>= 5.2)
Microsoft.EntityFrameworkCore.SqlServer (>= 8.0.2)
Microsoft.Extensions.Configuration (>= 8.0)
Microsoft.Extensions.Options.ConfigurationExtensions (>= 8.0)
Newtonsoft.Json (>= 13.0.3)
Be.Vlaanderen.Basisregisters.CommandHandling.SqlStreamStore (9.0.1)
Be.Vlaanderen.Basisregisters.AggregateSource (9.0.1)
Be.Vlaanderen.Basisregisters.CommandHandling (9.0.1)
Be.Vlaanderen.Basisregisters.CommandHandling.SqlStreamStore (9.1.1)
Be.Vlaanderen.Basisregisters.AggregateSource (9.1.1)
Be.Vlaanderen.Basisregisters.CommandHandling (9.1.1)
Be.Vlaanderen.Basisregisters.EventHandling (>= 5.0)
Be.Vlaanderen.Basisregisters.Generators.Guid.Deterministic (>= 4.0)
SqlStreamStore (>= 1.2)
Expand Down Expand Up @@ -401,8 +401,8 @@ NUGET
System.Threading.Tasks.Dataflow (>= 8.0)
Be.Vlaanderen.Basisregisters.Shaperon (10.0.2)
Be.Vlaanderen.Basisregisters.Shaperon.Geometries (10.0.2)
Be.Vlaanderen.Basisregisters.SnapshotVerifier (9.0.1)
Be.Vlaanderen.Basisregisters.AggregateSource.SqlStreamStore (9.0.1)
Be.Vlaanderen.Basisregisters.SnapshotVerifier (9.1.1)
Be.Vlaanderen.Basisregisters.AggregateSource.SqlStreamStore (9.1.1)
CompareNETObjects (>= 4.83)
Dapper (>= 2.1.35)
Microsoft.Extensions.Hosting (>= 8.0)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace BuildingRegistry.Api.BackOffice.Abstractions.Building.Requests
{
public sealed class CreateBuildingSnapshotRequest
{
public int PersistentLocalId { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace BuildingRegistry.Api.BackOffice.Abstractions.Building.SqsRequests
{
using Be.Vlaanderen.Basisregisters.Sqs.Requests;
using Requests;

public sealed class CreateBuildingSnapshotSqsRequest : SqsRequest
{
public CreateBuildingSnapshotRequest Request { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
namespace BuildingRegistry.Api.BackOffice.Handlers.Lambda.Handlers.Building
{
using Abstractions;
using Abstractions.Validation;
using Be.Vlaanderen.Basisregisters.AggregateSource;
using Be.Vlaanderen.Basisregisters.CommandHandling.Idempotency;
using Be.Vlaanderen.Basisregisters.Sqs.Lambda.Infrastructure;
using Be.Vlaanderen.Basisregisters.Sqs.Responses;
using BuildingRegistry.Building;
using BuildingRegistry.Building.Exceptions;
using Microsoft.Extensions.Configuration;
using Requests.Building;
using TicketingService.Abstractions;

public sealed class CreateBuildingSnapshotLambdaHandler : BuildingLambdaHandler<CreateBuildingSnapshotLambdaRequest>
{
public CreateBuildingSnapshotLambdaHandler(
IConfiguration configuration,
ICustomRetryPolicy retryPolicy,
ITicketing ticketing,
IIdempotentCommandHandler idempotentCommandHandler,
IBuildings buildings)
: base(
configuration,
retryPolicy,
ticketing,
idempotentCommandHandler,
buildings)
{ }

protected override async Task<object> InnerHandle(CreateBuildingSnapshotLambdaRequest request, CancellationToken cancellationToken)
{
var cmd = request.ToCommand();

try
{
await IdempotentCommandHandler.Dispatch(
cmd.CreateCommandId(),
cmd,
request.Metadata,
cancellationToken);
}
catch (IdempotencyException)
{
// Idempotent: Do Nothing return last etag
}

return "snapshot created";
}

protected override TicketError? InnerMapDomainException(DomainException exception, CreateBuildingSnapshotLambdaRequest request)
{
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace BuildingRegistry.Api.BackOffice.Handlers.Lambda
using Requests.BuildingUnit;
using System.Threading;
using System.Threading.Tasks;
using Abstractions.Building.Requests;
using Building;

public class MessageHandler : IMessageHandler
Expand Down Expand Up @@ -185,6 +186,12 @@ await mediator.Send(
cancellationToken);
break;

case CreateBuildingSnapshotSqsRequest request:
await mediator.Send(
new CreateBuildingSnapshotLambdaRequest(messageMetadata.MessageGroupId!, request),
cancellationToken);
break;

default:
throw new NotImplementedException(
$"{messageData.GetType().Name} has no corresponding SqsLambdaRequest defined.");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
namespace BuildingRegistry.Api.BackOffice.Handlers.Lambda.Requests.Building
{
using Abstractions.Building.Requests;
using Abstractions.Building.SqsRequests;
using BuildingRegistry.Building;
using BuildingRegistry.Building.Commands;
using IHasBuildingPersistentLocalId = Abstractions.IHasBuildingPersistentLocalId;

public sealed record CreateBuildingSnapshotLambdaRequest : BuildingLambdaRequest, IHasBuildingPersistentLocalId
{
public CreateBuildingSnapshotRequest Request { get; }
public int BuildingPersistentLocalId => Request.PersistentLocalId;

public CreateBuildingSnapshotLambdaRequest(
string messageGroupId,
CreateBuildingSnapshotSqsRequest sqsRequest)
: base(
messageGroupId,
sqsRequest.TicketId,
null,
sqsRequest.ProvenanceData.ToProvenance(),
sqsRequest.Metadata)
{
Request = sqsRequest.Request;
}

/// <summary>
/// Map to CreateSnapshot command
/// </summary>
/// <returns>CreateSnapshot</returns>
public CreateSnapshot ToCommand()
{
return new CreateSnapshot(
new BuildingPersistentLocalId(BuildingPersistentLocalId),
Provenance);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
namespace BuildingRegistry.Api.BackOffice.Building
{
using System.Threading;
using System.Threading.Tasks;
using Abstractions.Building.Requests;
using Abstractions.Building.Validators;
using Abstractions.Building.SqsRequests;
using Abstractions.Validation;
using Be.Vlaanderen.Basisregisters.Auth.AcmIdm;
using Be.Vlaanderen.Basisregisters.Api.ETag;
using Be.Vlaanderen.Basisregisters.Api.Exceptions;
using Be.Vlaanderen.Basisregisters.GrAr.Provenance;
using BuildingRegistry.Building;
using Infrastructure;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Filters;

public partial class BuildingController
{
/// <summary>
/// Snapshot voor het gebouw aanvragen.
/// </summary>
/// <param name="buildingExistsValidator"></param>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <response code="202">Als de snapshot voor het gebouw aangevraagd is.</response>
/// <returns></returns>
[HttpPost("{persistentLocalId}/acties/snapshot")]
[ProducesResponseType(StatusCodes.Status202Accepted)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status500InternalServerError)]
[SwaggerResponseHeader(StatusCodes.Status202Accepted, "location", "string", "De url van het gebouw.")]
[SwaggerResponseExample(StatusCodes.Status400BadRequest, typeof(BadRequestResponseExamples))]
[SwaggerResponseExample(StatusCodes.Status500InternalServerError, typeof(InternalServerErrorResponseExamples))]
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme, Policy = PolicyNames.GeschetstGebouw.InterneBijwerker)]
public async Task<IActionResult> CreateSnapshot(
[FromServices] BuildingExistsValidator buildingExistsValidator,
[FromRoute] CreateBuildingSnapshotRequest request,
CancellationToken cancellationToken = default)
{
if (!await buildingExistsValidator.Exists(new BuildingPersistentLocalId(request.PersistentLocalId),
cancellationToken))
{
throw new ApiException(ValidationErrors.Common.BuildingNotFound.Message, StatusCodes.Status404NotFound);
}

var result = await Mediator.Send(
new CreateBuildingSnapshotSqsRequest
{
Request = request,
Metadata = GetMetadata(),
ProvenanceData = new ProvenanceData(CreateProvenance(Modification.Unknown)),
}, cancellationToken);

return Accepted(result);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
namespace BuildingRegistry.Api.BackOffice.Handlers.Building
{
using System.Collections.Generic;
using Abstractions.Building.SqsRequests;
using AllStream;
using Be.Vlaanderen.Basisregisters.Sqs;
using Be.Vlaanderen.Basisregisters.Sqs.Handlers;
using TicketingService.Abstractions;

public sealed class CreateBuildingSnapshotSqsHandler : SqsHandler<CreateBuildingSnapshotSqsRequest>
{
public const string Action = "CreateBuildingSnapshot";

public CreateBuildingSnapshotSqsHandler(
ISqsQueue sqsQueue,
ITicketing ticketing,
ITicketingUrl ticketingUrl) : base(sqsQueue, ticketing, ticketingUrl)
{ }

protected override string? WithAggregateId(CreateBuildingSnapshotSqsRequest request)
{
return request.Request.PersistentLocalId.ToString();
}

protected override IDictionary<string, string> WithTicketMetadata(string aggregateId, CreateBuildingSnapshotSqsRequest sqsRequest)
{
return new Dictionary<string, string>
{
{ RegistryKey, nameof(BuildingRegistry) },
{ ActionKey, Action },
{ AggregateIdKey, aggregateId }
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace BuildingRegistry.Infrastructure.Modules
using Be.Vlaanderen.Basisregisters.AggregateSource.Snapshotting;
using Be.Vlaanderen.Basisregisters.EventHandling;
using Be.Vlaanderen.Basisregisters.EventHandling.Autofac;
using Building.Events;
using BuildingRegistry.AllStream;
using BuildingRegistry.Building;
using BuildingRegistry.Infrastructure;
Expand Down Expand Up @@ -36,10 +37,12 @@ protected override void Load(ContainerBuilder builder)
snapshotStrategy = IntervalStrategy.SnapshotEvery(snapshotInterval);
}

var anySnapshot = new AnySnapshotStrategy([snapshotStrategy, new AfterEventTypeStrategy(typeof(BuildingSnapshotWasRequested))]);

builder.RegisterSnapshotModule(_configuration);

builder
.Register(c => new BuildingFactory(snapshotStrategy))
.Register(c => new BuildingFactory(anySnapshot))
.As<IBuildingFactory>();

builder
Expand Down
Loading
Loading