Skip to content

Commit

Permalink
feat: add merge municipality
Browse files Browse the repository at this point in the history
  • Loading branch information
ArneD committed Jul 4, 2024
1 parent 47b2c7d commit 439c1e3
Show file tree
Hide file tree
Showing 25 changed files with 446 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace MunicipalityRegistry.Exceptions
{
using System;
using System.Runtime.Serialization;

[Serializable]
public sealed class CannotMergeMunicipalityWithSelfException : MunicipalityRegistryException
{
public CannotMergeMunicipalityWithSelfException()
{ }

private CannotMergeMunicipalityWithSelfException(SerializationInfo info, StreamingContext context)
: base(info, context)
{ }

public CannotMergeMunicipalityWithSelfException(string message)
: base(message)
{ }

public CannotMergeMunicipalityWithSelfException(string message, Exception inner)
: base(message, inner)
{ }
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace MunicipalityRegistry
namespace MunicipalityRegistry.Exceptions
{
using System;
using System.Runtime.Serialization;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace MunicipalityRegistry
namespace MunicipalityRegistry.Exceptions
{
using System;
using System.Runtime.Serialization;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace MunicipalityRegistry.Exceptions
{
using System;
using System.Runtime.Serialization;

[Serializable]
public sealed class MunicipalityHasInvalidStatusException : MunicipalityRegistryException
{
public MunicipalityHasInvalidStatusException()
{ }

private MunicipalityHasInvalidStatusException(SerializationInfo info, StreamingContext context)
: base(info, context)
{ }

public MunicipalityHasInvalidStatusException(string message)
: base(message)
{ }

public MunicipalityHasInvalidStatusException(string message, Exception inner)
: base(message, inner)
{ }
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace MunicipalityRegistry
namespace MunicipalityRegistry.Exceptions
{
using System;
using System.Runtime.Serialization;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace MunicipalityRegistry
namespace MunicipalityRegistry.Exceptions
{
using System;
using System.Runtime.Serialization;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace MunicipalityRegistry
namespace MunicipalityRegistry.Exceptions
{
using System;
using System.Runtime.Serialization;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace MunicipalityRegistry
namespace MunicipalityRegistry.Exceptions
{
using System;
using System.Runtime.Serialization;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
namespace MunicipalityRegistry.Municipality.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 MergeMunicipality : IHasCommandProvenance
{
private static readonly Guid Namespace = new Guid("033e3688-38aa-4fe2-b9df-ba656b49cf70");

public MunicipalityId MunicipalityId { get; }

public List<MunicipalityId> MunicipalityIdsToMergeWithWith { get; }

public List<NisCode> NisCodesToMergeWith { get; }

public MunicipalityId NewMunicipalityId { get; }

public NisCode NewNisCode { get; }

public Provenance Provenance { get; }

public MergeMunicipality(
MunicipalityId municipalityId,
List<MunicipalityId> municipalityIdsToMergeWithWith,
List<NisCode> nisCodesToMergeWith,
MunicipalityId newMunicipalityId,
NisCode newNisCode,
Provenance provenance)
{
MunicipalityId = municipalityId;
MunicipalityIdsToMergeWithWith = municipalityIdsToMergeWithWith;
NisCodesToMergeWith = nisCodesToMergeWith;
NewMunicipalityId = newMunicipalityId;
NewNisCode = newNisCode;
Provenance = provenance;
}

public Guid CreateCommandId()
=> Deterministic.Create(Namespace, $"{nameof(RegisterMunicipality)}-{ToString()}");

public override string? ToString()
=> ToStringBuilder.ToString(IdentityFields());

private IEnumerable<object> IdentityFields()
{
yield return MunicipalityId;
yield return MunicipalityIdsToMergeWithWith;
yield return NisCodesToMergeWith;
yield return NewNisCode;
yield return NewMunicipalityId;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ public sealed class MunicipalityWasDrawn : IHasProvenance, ISetProvenance, IMess
{
[EventPropertyDescription("Interne GUID van de gemeente.")]
public Guid MunicipalityId { get; }

[EventPropertyDescription("Extended WKB-voorstelling van de gemeentegrenzen.")]
public string ExtendedWkbGeometry { get; }

[EventPropertyDescription("Metadata bij het event.")]
public ProvenanceData Provenance { get; private set; }

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
namespace MunicipalityRegistry.Municipality.Events
{
using System;
using System.Collections.Generic;
using System.Linq;
using Be.Vlaanderen.Basisregisters.EventHandling;
using Be.Vlaanderen.Basisregisters.GrAr.Provenance;
using Newtonsoft.Json;

[EventName("MunicipalityWasMerged")]
[EventDescription("De gemeente werd gefusioneerd.")]
public sealed class MunicipalityWasMerged : IHasProvenance, ISetProvenance, IMessage
{
[EventPropertyDescription("Interne GUID van de gemeente.")]
public Guid MunicipalityId { get; }

[EventPropertyDescription("NIS-code (= objectidentificator) van de gemeente.")]
public string NisCode { get; }

[EventPropertyDescription("Interne GUIDs van de gemeenten waarmee gefusioneerd werd.")]
public List<Guid> MunicipalityIdsToMergeWith { get; }

[EventPropertyDescription("NIS-codes (= objectidentificatoren) van de gemeenten waarmee gefusioneerd werd.")]
public List<string> NisCodesToMergeWith { get; }

[EventPropertyDescription("Interne GUID van de nieuwe gemeente.")]
public Guid NewMunicipalityId { get; }

[EventPropertyDescription("NIS-code (= objectidentificator) van de nieuwe gemeente.")]
public string NewNisCode { get; }

[EventPropertyDescription("Metadata bij het event.")]
public ProvenanceData Provenance { get; private set; }

public MunicipalityWasMerged(
MunicipalityId municipalityId,
NisCode nisCode,
IEnumerable<MunicipalityId> municipalityIdsToMergeWith,
IEnumerable<NisCode> nisCodesToMergeWith,
MunicipalityId newMunicipalityId,
NisCode newNisCode)
{
MunicipalityId = municipalityId;
NisCode = nisCode;
MunicipalityIdsToMergeWith = municipalityIdsToMergeWith.Select(x => (Guid)x).ToList();
NisCodesToMergeWith = nisCodesToMergeWith.Select(x => (string)x).ToList();
NewMunicipalityId = newMunicipalityId;
NewNisCode = newNisCode;
}

[JsonConstructor]
private MunicipalityWasMerged(
Guid municipalityId,
string nisCode,
List<Guid> municipalityIdsToMergeWith,
List<string> nisCodesToMergeWith,
Guid newMunicipalityId,
string newNisCode,
ProvenanceData provenance) : this(
new MunicipalityId(municipalityId),
new NisCode(nisCode),
municipalityIdsToMergeWith.Select(x => new MunicipalityId(x)).ToList(),
nisCodesToMergeWith.Select(x => new NisCode(x)).ToList(),
new MunicipalityId(newMunicipalityId),
new NisCode(newNisCode)) =>
((ISetProvenance)this).SetProvenance(provenance.ToProvenance());

void ISetProvenance.SetProvenance(Provenance provenance) => Provenance = new ProvenanceData(provenance);

}
}
22 changes: 22 additions & 0 deletions src/MunicipalityRegistry/Municipality/Municipality.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using Be.Vlaanderen.Basisregisters.AggregateSource;
using Events;
using Exceptions;
using NetTopologySuite.Geometries;

public sealed partial class Municipality : AggregateRootEntity
Expand Down Expand Up @@ -53,6 +54,27 @@ public static Municipality Register(
return municipality;
}

public void Merge(
IEnumerable<MunicipalityId> municipalityIdsToMergeWithWith,
IEnumerable<NisCode> nisCodesToMergeWith,
MunicipalityId newMunicipalityId,
NisCode newNisCode)
{
if(!IsCurrent)
throw new MunicipalityHasInvalidStatusException();

if(MunicipalityId == newMunicipalityId)
throw new CannotMergeMunicipalityWithSelfException();

ApplyChange(new MunicipalityWasMerged(
MunicipalityId,
NisCode,
municipalityIdsToMergeWithWith,
nisCodesToMergeWith,
newMunicipalityId,
newNisCode));
}

private static void GuardPolygon(Geometry? geometry)
{
if (geometry is Polygon
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,24 @@ public MunicipalityCommandHandlerModule(

municipalities.Add(municipalityId, newMunicipality);
});

For<MergeMunicipality>()
.AddSqlStreamStore(getStreamStore, getUnitOfWork, eventMapping, eventSerializer)
.AddProvenance(getUnitOfWork, provenanceFactory)
.Handle(async (message, ct) =>
{
var municipalities = getMunicipalities();

var municipalityId = message.Command.MunicipalityId;

var municipality = await municipalities.GetAsync(municipalityId, ct);

municipality.Merge(
message.Command.MunicipalityIdsToMergeWithWith,
message.Command.NisCodesToMergeWith,
message.Command.NewMunicipalityId,
message.Command.NewNisCode);
});
}
}
}
1 change: 1 addition & 0 deletions src/MunicipalityRegistry/Municipality/Municipality_Crab.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace MunicipalityRegistry.Municipality
using System.Linq;
using Be.Vlaanderen.Basisregisters.Crab;
using Events;
using Exceptions;
using NodaTime;

public sealed partial class Municipality
Expand Down
7 changes: 7 additions & 0 deletions src/MunicipalityRegistry/Municipality/Municipality_State.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ private Municipality()

Register<MunicipalityWasImportedFromCrab>(@event => WhenCrabEventApplied());
Register<MunicipalityNameWasImportedFromCrab>(@event => WhenCrabEventApplied());

Register<MunicipalityWasMerged>(When);
}

private void When(MunicipalityWasMerged @event)
{
Status = MunicipalityStatus.Retired;
}

private void WhenCrabEventApplied()
Expand Down
1 change: 1 addition & 0 deletions src/MunicipalityRegistry/ValueObjects/MunicipalityName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ namespace MunicipalityRegistry
{
using System.Collections.Generic;
using Be.Vlaanderen.Basisregisters.AggregateSource;
using Exceptions;

public sealed class MunicipalityName : ValueObject<MunicipalityName>
{
Expand Down
1 change: 1 addition & 0 deletions src/MunicipalityRegistry/ValueObjects/NisCode.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace MunicipalityRegistry
{
using Be.Vlaanderen.Basisregisters.AggregateSource;
using Exceptions;
using Newtonsoft.Json;

public sealed class NisCode : StringValueObject<NisCode>
Expand Down
Loading

0 comments on commit 439c1e3

Please sign in to comment.