Skip to content

Commit 491f6bf

Browse files
Merge pull request #151 from TransactionProcessing/bug/#150_fileimportlogidgeneration
Add Estate Id to Import Log Id
2 parents 5c57e53 + 86b5c8c commit 491f6bf

4 files changed

Lines changed: 113 additions & 14 deletions

File tree

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
namespace FileProcessor.BusinessLogic.Common
2+
{
3+
using System;
4+
5+
public static class GuidCalculator
6+
{
7+
#region Methods
8+
9+
/// <summary>
10+
/// Combines the specified GUIDs into a new GUID.
11+
/// </summary>
12+
/// <param name="firstGuid">The first unique identifier.</param>
13+
/// <param name="secondGuid">The second unique identifier.</param>
14+
/// <param name="offset">The offset.</param>
15+
/// <returns>Guid.</returns>
16+
public static Guid Combine(Guid firstGuid,
17+
Guid secondGuid,
18+
Byte offset)
19+
{
20+
Byte[] firstAsBytes = firstGuid.ToByteArray();
21+
Byte[] secondAsBytes = secondGuid.ToByteArray();
22+
23+
Byte[] newBytes = new Byte[16];
24+
25+
for (Int32 i = 0; i < 16; i++)
26+
{
27+
// Add and truncate any overflow
28+
newBytes[i] = (Byte)(firstAsBytes[i] + secondAsBytes[i] + offset);
29+
}
30+
31+
return new Guid(newBytes);
32+
}
33+
34+
/// <summary>
35+
/// Combines the specified GUIDs into a new GUID.
36+
/// </summary>
37+
/// <param name="firstGuid">The first unique identifier.</param>
38+
/// <param name="secondGuid">The second unique identifier.</param>
39+
/// <returns>Guid.</returns>
40+
public static Guid Combine(Guid firstGuid,
41+
Guid secondGuid)
42+
{
43+
return GuidCalculator.Combine(firstGuid,
44+
secondGuid,
45+
0);
46+
}
47+
48+
/// <summary>
49+
/// Combines the specified first unique identifier.
50+
/// </summary>
51+
/// <param name="firstGuid">The first unique identifier.</param>
52+
/// <param name="secondGuid">The second unique identifier.</param>
53+
/// <param name="thirdGuid">The third unique identifier.</param>
54+
/// <param name="offset">The offset.</param>
55+
/// <returns>Guid.</returns>
56+
public static Guid Combine(Guid firstGuid,
57+
Guid secondGuid,
58+
Guid thirdGuid,
59+
Byte offset)
60+
{
61+
Byte[] firstAsBytes = firstGuid.ToByteArray();
62+
Byte[] secondAsBytes = secondGuid.ToByteArray();
63+
Byte[] thirdAsBytes = thirdGuid.ToByteArray();
64+
65+
Byte[] newBytes = new Byte[16];
66+
67+
for (Int32 i = 0; i < 16; i++)
68+
{
69+
// Add and truncate any overflow
70+
newBytes[i] = (Byte)(firstAsBytes[i] + secondAsBytes[i] + thirdAsBytes[i] + offset);
71+
}
72+
73+
return new Guid(newBytes);
74+
}
75+
76+
/// <summary>
77+
/// Combines the specified first unique identifier.
78+
/// </summary>
79+
/// <param name="firstGuid">The first unique identifier.</param>
80+
/// <param name="secondGuid">The second unique identifier.</param>
81+
/// <param name="thirdGuid">The third unique identifier.</param>
82+
/// <returns>Guid.</returns>
83+
public static Guid Combine(Guid firstGuid,
84+
Guid secondGuid,
85+
Guid thirdGuid)
86+
{
87+
return GuidCalculator.Combine(firstGuid,
88+
secondGuid,
89+
thirdGuid,
90+
0);
91+
}
92+
93+
#endregion
94+
}
95+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace FileProcessor.BusinessLogic.Common
2+
{
3+
using System;
4+
using Shared.Extensions;
5+
6+
public static class Helpers
7+
{
8+
public static Guid CalculateFileImportLogAggregateId(DateTime importLogDateTime,
9+
Guid estateId)
10+
{
11+
Guid aggregateId = GuidCalculator.Combine(estateId, importLogDateTime.ToGuid());
12+
return aggregateId;
13+
}
14+
}
15+
}

FileProcessor.BusinessLogic/RequestHandlers/FileRequestHandler.cs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ namespace FileProcessor.BusinessLogic.RequestHandlers
1212
using System.Security.Cryptography;
1313
using MediatR;
1414
using System.Threading;
15+
using Common;
1516
using EstateManagement.Client;
1617
using EstateManagement.DataTransferObjects.Responses;
1718
using EventHandling;
@@ -128,7 +129,7 @@ public async Task<Guid> Handle(UploadFileRequest request,
128129
DateTime importLogDateTime = request.FileUploadedDateTime;
129130

130131
// This will now create the import log and add an event for the file being uploaded
131-
Guid importLogId = this.CreateGuidFromDateTime(importLogDateTime.Date);
132+
Guid importLogId = Helpers.CalculateFileImportLogAggregateId(importLogDateTime.Date, request.EstateId);
132133

133134
// Get the import log
134135
FileImportLogAggregate fileImportLogAggregate = await this.FileImportLogAggregateRepository.GetLatestVersion(importLogId, cancellationToken);
@@ -201,18 +202,6 @@ private Guid CreateGuidFromFileData(String fileContents)
201202
}
202203
}
203204

204-
private Guid CreateGuidFromDateTime(DateTime dateTime)
205-
{
206-
var bytes = BitConverter.GetBytes(dateTime.Ticks);
207-
208-
Array.Resize(ref bytes, 16);
209-
210-
var guid = new Guid(bytes);
211-
212-
return guid;
213-
}
214-
215-
216205
public async Task<Unit> Handle(ProcessUploadedFileRequest request, CancellationToken cancellationToken)
217206
{
218207
// TODO: Should the file id be generated from the file uploaded to protect against duplicate files???

FileProcessor.Tests/FileProcessor.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
1111
<PackageReference Include="Moq" Version="4.16.1" />
1212
<PackageReference Include="Shouldly" Version="4.0.3" />
13-
<PackageReference Include="System.IO.Abstractions.TestingHelpers" Version="13.2.29" />
13+
<PackageReference Include="System.IO.Abstractions.TestingHelpers" Version="17.0.3" />
1414
<PackageReference Include="xunit" Version="2.4.1" />
1515
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
1616
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

0 commit comments

Comments
 (0)