Skip to content

Commit

Permalink
Merge branch 'main' into xabpe/1-Refactor-ArchivedMessages-to-clean-a…
Browse files Browse the repository at this point in the history
…rchitecture
  • Loading branch information
AndersBallingPetersen committed Nov 15, 2024
2 parents c1a83c7 + c6c19d2 commit b0a5351
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,20 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using Energinet.DataHub.EDI.ArchivedMessages.Domain.Exceptions;
using Energinet.DataHub.EDI.ArchivedMessages.Domain.Models;
using Energinet.DataHub.EDI.ArchivedMessages.Interfaces.Models;
using Energinet.DataHub.EDI.BuildingBlocks.Domain.Validation;

namespace Energinet.DataHub.EDI.ArchivedMessages.Application.Mapping;

internal static class ArchivedMessageMapper
{
internal static ArchivedMessage Map(ArchivedMessageDto dto)
{
return new ArchivedMessage(
return !EnumCompatibilityChecker.AreEnumsCompatible<ArchivedMessageType, ArchivedMessageTypeDto>()
? throw new InvalidEnumMappingException($"Enum of type {nameof(ArchivedMessageType)} cannot be mapped to type {nameof(ArchivedMessageTypeDto)}.")
: new ArchivedMessage(
id: new ArchivedMessageId(dto.Id.Value),
messageId: dto.MessageId,
eventIds: dto.EventIds,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private static SortedCursorBasedPagination SetSortedCursorBasedPagination(Sorted
pageSize: dto.PageSize,
navigationForward: dto.NavigationForward,
fieldToSortBy: SetFieldToSortBy(dto.FieldToSortBy),
sortByDirection: SetDirectionToSortBy(dto.DirectionToSortBy));
directionToSortBy: SetDirectionToSortBy(dto.DirectionToSortBy));
}

private static MessageCreationPeriod? SetMessageCreationPeriod(MessageCreationPeriodDto? messageCreationPeriod)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2020 Energinet DataHub A/S
//
// Licensed under the Apache License, Version 2.0 (the "License2");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Energinet.DataHub.EDI.ArchivedMessages.Domain.Exceptions;

public class InvalidEnumMappingException : Exception
{
public InvalidEnumMappingException()
{
}

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

public InvalidEnumMappingException(string message, Exception innerException)
: base(message, innerException)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class SortedCursorBasedPagination(
int pageSize = 100,
bool navigationForward = true,
FieldToSortBy? fieldToSortBy = null,
DirectionToSortBy? sortByDirection = null)
DirectionToSortBy? directionToSortBy = null)
{
/// <summary>
/// The current position in the dataset.
Expand All @@ -43,12 +43,12 @@ public class SortedCursorBasedPagination(
/// <summary>
/// The field to sort by.
/// </summary>
public FieldToSortBy SortBy { get; } = fieldToSortBy ?? FieldToSortBy.CreatedAt;
public FieldToSortBy FieldToSortBy { get; } = fieldToSortBy ?? FieldToSortBy.CreatedAt;

/// <summary>
/// The direction to sort by.
/// </summary>
public DirectionToSortBy SortByDirection { get; } = sortByDirection ?? DirectionToSortBy.Descending;
public DirectionToSortBy DirectionToSortBy { get; } = directionToSortBy ?? DirectionToSortBy.Descending;
}

/// <summary>
Expand Down
10 changes: 5 additions & 5 deletions source/ArchivedMessages.Infrastructure/QueryBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public QueryInput BuildFrom(GetMessagesQuery query)
return new QueryInput(BuildStatement(query), BuildTotalCountStatement(query), _queryParameters);
}

private static string WherePaginationPosition(FieldToSortBy fieldToSortBy, DirectionToSortBy sortByDirection, SortingCursor cursor, bool isForward)
private static string WherePaginationPosition(FieldToSortBy fieldToSortBy, DirectionToSortBy directionToSortBy, SortingCursor cursor, bool isForward)
{
if (cursor.SortedFieldValue is null)
{
Expand All @@ -131,8 +131,8 @@ private static string WherePaginationPosition(FieldToSortBy fieldToSortBy, Direc
}

// Toggle the sort direction if navigating backwards, because sql use top to limit the result
var sortingDirection = isForward ? sortByDirection.Identifier == DirectionToSortBy.Descending.Identifier ? "<" : ">"
: sortByDirection.Identifier == DirectionToSortBy.Descending.Identifier ? ">" : "<";
var sortingDirection = isForward ? directionToSortBy.Identifier == DirectionToSortBy.Descending.Identifier ? "<" : ">"
: directionToSortBy.Identifier == DirectionToSortBy.Descending.Identifier ? ">" : "<";
return isForward
? $"""
(({fieldToSortBy.Identifier} = '{cursor.SortedFieldValue}' AND (RecordId < {cursor.RecordId} OR {cursor.RecordId} = 0))
Expand All @@ -156,7 +156,7 @@ private string BuildStatement(GetMessagesQuery query)
{
var whereClause = " WHERE ";
whereClause += _statement.Count > 0 ? $"{string.Join(" AND ", _statement)} AND " : string.Empty;
whereClause += WherePaginationPosition(query.Pagination.SortBy, query.Pagination.SortByDirection, query.Pagination.Cursor, query.Pagination.NavigationForward);
whereClause += WherePaginationPosition(query.Pagination.FieldToSortBy, query.Pagination.DirectionToSortBy, query.Pagination.Cursor, query.Pagination.NavigationForward);
string sqlStatement;

if (query.IncludeRelatedMessages == true && query.MessageId is not null)
Expand All @@ -183,7 +183,7 @@ private string BuildStatement(GetMessagesQuery query)
sqlStatement = selectStatement + whereClause;
}

sqlStatement += OrderBy(query.Pagination.SortBy, query.Pagination.SortByDirection, query.Pagination.NavigationForward);
sqlStatement += OrderBy(query.Pagination.FieldToSortBy, query.Pagination.DirectionToSortBy, query.Pagination.NavigationForward);
return sqlStatement;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright 2020 Energinet DataHub A/S
//
// Licensed under the Apache License, Version 2.0 (the "License2");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using Energinet.DataHub.EDI.ArchivedMessages.Domain.Models;
using Energinet.DataHub.EDI.ArchivedMessages.Interfaces.Models;
using Energinet.DataHub.EDI.BuildingBlocks.Domain.Validation;
using Xunit;

namespace Energinet.DataHub.EDI.ArchivedMessages.IntegrationTests;

public class ArchivedMessagesMappingTests
{
private enum MismatchedEnum
{
A = 1,
B = 2,
D = 4,
}

[Fact]
public void Given_EnumsAreLogicallyCompatible_When_Mapping_Then_ReturnTrueWhenEnumsMatch()
{
// Act
var result = EnumCompatibilityChecker.AreEnumsCompatible<ArchivedMessageType, ArchivedMessageTypeDto>();

// Assert
Assert.True(result, "The enums should be logically compatible.");
}

[Fact]
public void Given_EnumsAreNotLogicallyCompatible_When_Mapping_Then_ReturnFalseWhenEnumsDoNotMatch()
{
// Act
var result = EnumCompatibilityChecker.AreEnumsCompatible<ArchivedMessageType, MismatchedEnum>();

// Assert
Assert.False(result, "The enums should not be logically compatible.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ public sealed record SortedCursorBasedPaginationDto(
/// <summary>
/// The field to sort by.
/// </summary>
public FieldToSortByDto SortBy { get; } = FieldToSortBy ?? FieldToSortByDto.CreatedAt;
public FieldToSortByDto SortField { get; } = FieldToSortBy ?? FieldToSortByDto.CreatedAt;

/// <summary>
/// The direction to sort by.
/// </summary>
public DirectionToSortByDto SortByDirection { get; } = DirectionToSortBy ?? DirectionToSortByDto.Descending;
public DirectionToSortByDto SortDirection { get; } = DirectionToSortBy ?? DirectionToSortByDto.Descending;
}

public sealed record SortingCursorDto(string? SortedFieldValue = null, long RecordId = 0);
2 changes: 1 addition & 1 deletion source/BuildingBlocks.Domain/BuildingBlocks.Domain.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<AssemblyName>Energinet.DataHub.EDI.BuildingBlocks.Domain</AssemblyName>
<RootNamespace>Energinet.DataHub.EDI.BuildingBlocks.Domain</RootNamespace>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2020 Energinet DataHub A/S
//
// Licensed under the Apache License, Version 2.0 (the "License2");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Energinet.DataHub.EDI.BuildingBlocks.Domain.Validation;

public static class EnumCompatibilityChecker
{
public static bool AreEnumsCompatible<T1, T2>()
where T1 : Enum
where T2 : Enum
{
var firstEnum = Enum.GetValues(typeof(T1)).Cast<Enum>().ToDictionary(e => e.ToString(), Convert.ToInt32);
var secondEnum = Enum.GetValues(typeof(T2)).Cast<Enum>().ToDictionary(e => e.ToString(), Convert.ToInt32);

return firstEnum.All(e => secondEnum.ContainsKey(e.Key) && secondEnum[e.Key] == e.Value);
}
}

0 comments on commit b0a5351

Please sign in to comment.