diff --git a/TramsDataApi.Test/Extensions/StringExtensionsTests.cs b/TramsDataApi.Test/Extensions/StringExtensionsTests.cs new file mode 100644 index 000000000..5913d8f96 --- /dev/null +++ b/TramsDataApi.Test/Extensions/StringExtensionsTests.cs @@ -0,0 +1,25 @@ +using TramsDataApi.Extensions; +using Xunit; + +namespace TramsDataApi.Test.Extensions +{ + public class StringExtensionsTests + { + [Theory] + [InlineData("123", 123)] + [InlineData("456", 456)] + [InlineData("0", 0)] + [InlineData("-789", -789)] + [InlineData("abc", 0)] // Invalid string + [InlineData(null, 0)] // Null string + [InlineData("", 0)] // Empty string + public void ToInt_ReturnsExpectedResult(string input, int expectedResult) + { + // Act + int result = input.ToInt(); + + // Assert + Assert.Equal(expectedResult, result); + } + } +} diff --git a/TramsDataApi.Test/Integration/V3/TrustsV3IntegrationTests.cs b/TramsDataApi.Test/Integration/V3/TrustsV3IntegrationTests.cs index 56602d076..7c0a0c0d0 100644 --- a/TramsDataApi.Test/Integration/V3/TrustsV3IntegrationTests.cs +++ b/TramsDataApi.Test/Integration/V3/TrustsV3IntegrationTests.cs @@ -197,7 +197,7 @@ public async Task ShouldReturnEstablishmentDataAgainstOpenTrust_WhenTrustHasAnEs string TrustUKPRN = "123456789"; var closedTrustGroup = _fixture.Build() - .With(f => f.GroupUid, "1") + .With(f => f.GroupUid, "234") .With(f => f.GroupId, groupID) .With(f => f.GroupName, TrustName) .With(f => f.Ukprn, TrustUKPRN) @@ -208,14 +208,14 @@ public async Task ShouldReturnEstablishmentDataAgainstOpenTrust_WhenTrustHasAnEs .Create(); var openTrustGroup = _fixture.Build() - .With(f => f.GroupUid, "2") + .With(f => f.GroupUid, "1234") .With(f => f.GroupId, groupID) - .With(f => f.GroupName, TrustName) - .With(f => f.Ukprn, TrustUKPRN) - .With(f => f.GroupStatus, "Open") - .With(f => f.GroupStatusCode, "OPEN") - .With(f => f.GroupType, "Multi-academy trust") - .Create(); + .With(f => f.GroupName, TrustName) + .With(f => f.Ukprn, TrustUKPRN) + .With(f => f.GroupStatus, "Open") + .With(f => f.GroupStatusCode, "OPEN") + .With(f => f.GroupType, "Multi-academy trust") + .Create(); _legacyDbContext.Group.AddRange(closedTrustGroup, openTrustGroup); diff --git a/TramsDataApi/Extensions/StringExtensions.cs b/TramsDataApi/Extensions/StringExtensions.cs new file mode 100644 index 000000000..1f53092a0 --- /dev/null +++ b/TramsDataApi/Extensions/StringExtensions.cs @@ -0,0 +1,10 @@ +namespace TramsDataApi.Extensions +{ + public static class StringExtensions + { + public static int ToInt(this string value) + { + return int.TryParse(value, out var result) ? result : 0; + } + } +} diff --git a/TramsDataApi/Gateways/TrustGateway.cs b/TramsDataApi/Gateways/TrustGateway.cs index 72a567285..6af82b4e0 100644 --- a/TramsDataApi/Gateways/TrustGateway.cs +++ b/TramsDataApi/Gateways/TrustGateway.cs @@ -1,14 +1,13 @@ +using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; -using System.Linq.Expressions; -using Microsoft.EntityFrameworkCore; using TramsDataApi.DatabaseModels; using TramsDataApi.Extensions; namespace TramsDataApi.Gateways { - public class TrustGateway : ITrustGateway + public class TrustGateway : ITrustGateway { private readonly LegacyTramsDbContext _dbContext; @@ -24,7 +23,11 @@ public Group GetGroupByUkPrn(string ukPrn) public Group GetLatestGroupByUkPrn(string ukPrn) { - return _dbContext.Group.OrderByDescending(f=> f.GroupUid).FirstOrDefault(g => g.Ukprn == ukPrn); + var group = _dbContext.Group.Where(g => g.Ukprn == ukPrn).ToList(); + + var result = group.OrderByDescending(g => g.GroupUid.ToInt()).FirstOrDefault(); + + return result; } public Trust GetIfdTrustByGroupId(string groupId) @@ -89,7 +92,5 @@ public TrustMasterData GetMstrTrustByGroupId(string groupId) { return _dbContext.TrustMasterData.FirstOrDefault(t => t.GroupID == groupId); } - - } }