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 4da97c0
Showing 1 changed file with 19 additions and 1 deletion.
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,28 @@ 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 ILogger<EligiblePostcodeService> logger;
private readonly List<string> eligiblePostcodes;

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 @@ -31,6 +49,6 @@ public bool IsEligiblePostcode(string postcode)
return false;
}

return EligiblePostcodeData.EligiblePostcodes.Contains(normalisedPostcode);
return eligiblePostcodes.Contains(normalisedPostcode);
}
}

0 comments on commit 4da97c0

Please sign in to comment.