Skip to content

Commit

Permalink
PC-1344: load json file at runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
samyou-softwire committed Sep 30, 2024
1 parent cc2f4fb commit 3d26601
Showing 1 changed file with 26 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using HerPublicWebsite.BusinessLogic.Extensions;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;

namespace HerPublicWebsite.BusinessLogic.Services.EligiblePostcode;

Expand All @@ -10,11 +11,32 @@ public interface IEligiblePostcodeService

public class EligiblePostcodeService : IEligiblePostcodeService
{
// build structure means json file is in different location when running locally
private const string PostcodeJsonPath = "Services/EligiblePostcode/EligiblePostcodeData.json";

private const string LocalPostcodeJsonPath =
"../HerPublicWebsite.BusinessLogic/Services/EligiblePostcode/EligiblePostcodeData.json";

private readonly List<string> eligiblePostcodes;
private readonly ILogger<EligiblePostcodeService> logger;

public EligiblePostcodeService(ILogger<EligiblePostcodeService> logger)
{
this.logger = logger;
string jsonContents;

if (File.Exists(LocalPostcodeJsonPath))
{
using var reader = new StreamReader(LocalPostcodeJsonPath);
jsonContents = reader.ReadToEnd();
}
else
{
using var reader = new StreamReader(PostcodeJsonPath);
jsonContents = reader.ReadToEnd();
}

eligiblePostcodes = JsonConvert.DeserializeObject<List<string>>(jsonContents);
}

// Check whether a postcode is in the list of eligible postcodes found on this page
Expand All @@ -30,7 +52,7 @@ public bool IsEligiblePostcode(string postcode)
logger.LogError("IsEligiblePostcode was called with a string that is not a valid postcode: {}", postcode);
return false;
}
return EligiblePostcodeData.EligiblePostcodes.Contains(normalisedPostcode);

return eligiblePostcodes.Contains(normalisedPostcode);
}
}
}

0 comments on commit 3d26601

Please sign in to comment.