-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
454 additions
and
1 deletion.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
...ngRegistry.Api.BackOffice.Abstractions/Building/Requests/CreateBuildingSnapshotRequest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...stry.Api.BackOffice.Abstractions/Building/SqsRequests/CreateBuildingSnapshotSqsRequest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
...y.Api.BackOffice.Handlers.Lambda/Handlers/Building/CreateBuildingSnapshotLambdaHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
...y.Api.BackOffice.Handlers.Lambda/Requests/Building/CreateBuildingSnapshotLambdaRequest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
namespace BuildingRegistry.Api.BackOffice.Handlers.Lambda.Requests.Building | ||
{ | ||
using Be.Vlaanderen.Basisregisters.Sqs.Lambda.Requests; | ||
using BuildingRegistry.AllStream.Commands; | ||
using BuildingRegistry.Api.BackOffice.Abstractions.Building.Requests; | ||
using BuildingRegistry.Api.BackOffice.Abstractions.Building.SqsRequests; | ||
using BuildingRegistry.Building; | ||
using BuildingRegistry.Building.Commands; | ||
|
||
public sealed record CreateBuildingSnapshotLambdaRequest : BuildingLambdaRequest, Abstractions.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 CreateOsloSnapshots command | ||
/// </summary> | ||
/// <returns>CreateOsloSnapshots</returns> | ||
public CreateSnapshot ToCommand() | ||
{ | ||
return new CreateSnapshot( | ||
new BuildingPersistentLocalId(BuildingPersistentLocalId), | ||
Provenance); | ||
} | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
src/BuildingRegistry.Api.BackOffice/Building/BuildingController-CreateSnapshot.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.DecentraleBijwerker)] | ||
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); | ||
} | ||
} | ||
} |
File renamed without changes.
35 changes: 35 additions & 0 deletions
35
src/BuildingRegistry.Api.BackOffice/Handlers/Building/CreateBuildingSnapshotSqsHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/BuildingRegistry/Building/Commands/CreateOsloSnapshots.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
namespace BuildingRegistry.Building.Commands | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using Be.Vlaanderen.Basisregisters.Generators.Guid; | ||
using Be.Vlaanderen.Basisregisters.GrAr.Provenance; | ||
using Be.Vlaanderen.Basisregisters.Utilities; | ||
|
||
public sealed class CreateSnapshot : IHasCommandProvenance | ||
{ | ||
private static readonly Guid Namespace = new Guid("1ca80df2-b1e3-463a-849e-c22eeca84c3f"); | ||
|
||
public BuildingPersistentLocalId BuildingPersistentLocalId { get; } | ||
|
||
public Provenance Provenance { get; } | ||
|
||
public CreateSnapshot( | ||
BuildingPersistentLocalId buildingPersistentLocalId, | ||
Provenance provenance) | ||
{ | ||
BuildingPersistentLocalId = buildingPersistentLocalId; | ||
Provenance = provenance; | ||
} | ||
|
||
public Guid CreateCommandId() | ||
=> Deterministic.Create(Namespace, $"CreateSnapshot-{ToString()}"); | ||
|
||
public override string? ToString() | ||
=> ToStringBuilder.ToString(IdentityFields()); | ||
|
||
private IEnumerable<object> IdentityFields() | ||
{ | ||
yield return BuildingPersistentLocalId; | ||
|
||
foreach (var field in Provenance.GetIdentityFields()) | ||
{ | ||
yield return field; | ||
} | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/BuildingRegistry/Building/Events/BuildingSnapshotWasRequested.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
namespace BuildingRegistry.Building.Events | ||
{ | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.Linq; | ||
using Be.Vlaanderen.Basisregisters.EventHandling; | ||
using Be.Vlaanderen.Basisregisters.GrAr.Common; | ||
using Be.Vlaanderen.Basisregisters.GrAr.Provenance; | ||
using Newtonsoft.Json; | ||
|
||
[EventName(EventName)] | ||
[EventDescription("EventStore snapshot voor het gebouw werd aangevraagd.")] | ||
public sealed class BuildingSnapshotWasRequested: IBuildingEvent, IHasBuildingPersistentLocalId | ||
{ | ||
public const string EventName = "BuildingSnapshotWasRequested"; // BE CAREFUL CHANGING THIS!! | ||
|
||
[EventPropertyDescription("Objectidentificator van het gebouw.")] | ||
public int BuildingPersistentLocalId { get; } | ||
|
||
[EventPropertyDescription("Metadata bij het event.")] | ||
public ProvenanceData Provenance { get; private set; } | ||
|
||
public BuildingSnapshotWasRequested(BuildingPersistentLocalId buildingPersistentLocalId) | ||
{ | ||
BuildingPersistentLocalId = buildingPersistentLocalId; | ||
} | ||
|
||
[JsonConstructor] | ||
private BuildingSnapshotWasRequested(int buildingPersistentLocalId, ProvenanceData provenance) | ||
: this(new BuildingPersistentLocalId(buildingPersistentLocalId)) | ||
=> ((ISetProvenance)this).SetProvenance(provenance.ToProvenance()); | ||
|
||
void ISetProvenance.SetProvenance(Provenance provenance) => Provenance = new ProvenanceData(provenance); | ||
|
||
public IEnumerable<string> GetHashFields() | ||
{ | ||
var fields = Provenance.GetHashFields().ToList(); | ||
fields.Add(BuildingPersistentLocalId.ToString(CultureInfo.InvariantCulture)); | ||
return fields; | ||
} | ||
|
||
public string GetHash() => this.ToEventHash(EventName); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...ildingRegistry.Tests/AggregateTests/WhenRequestingBuildingSnapshot/GivenBuildingExists.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
namespace BuildingRegistry.Tests.AggregateTests.WhenRequestingBuildingSnapshot | ||
{ | ||
using AutoFixture; | ||
using Be.Vlaanderen.Basisregisters.AggregateSource; | ||
using Be.Vlaanderen.Basisregisters.AggregateSource.Testing; | ||
using Building; | ||
using Building.Commands; | ||
using Building.Events; | ||
using Fixtures; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
public class GivenBuildingExists : BuildingRegistryTest | ||
{ | ||
public GivenBuildingExists(ITestOutputHelper testOutputHelper) : base(testOutputHelper) | ||
{ | ||
Fixture.Customize(new WithFixedBuildingPersistentLocalId()); | ||
} | ||
|
||
[Fact] | ||
public void ThenCreateSnapshotWasRequested() | ||
{ | ||
var command = Fixture.Create<CreateSnapshot>(); | ||
|
||
Assert(new Scenario() | ||
.Given( | ||
new BuildingStreamId(Fixture.Create<BuildingPersistentLocalId>()), | ||
Fixture.Create<BuildingWasPlannedV2>()) | ||
.When(command) | ||
.Then(new Fact( | ||
new BuildingStreamId(command.BuildingPersistentLocalId), | ||
new BuildingSnapshotWasRequested(command.BuildingPersistentLocalId)))); | ||
} | ||
} | ||
} |
Oops, something went wrong.