Skip to content

Commit

Permalink
Abstract static Fuzzy into injected adapter service.
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertGHippo committed Jul 9, 2024
1 parent 93d2f64 commit 9d02989
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
using Contentful.Core.Search;
using Dfe.EarlyYearsQualification.Content.Constants;
using Dfe.EarlyYearsQualification.Content.Entities;
using FuzzySharp;
using Microsoft.Extensions.Logging;

namespace Dfe.EarlyYearsQualification.Content.Services;

public class ContentfulContentFilterService(
IContentfulClient contentfulClient,
IFuzzyAdapter fuzzyAdapter,
ILogger<ContentfulContentFilterService> logger)
: IContentFilterService
{
Expand Down Expand Up @@ -39,9 +39,10 @@ private static readonly ReadOnlyDictionary<string, int>
public QueryBuilder<Qualification> QueryBuilder { get; init; } = QueryBuilder<Qualification>.New;

public async Task<List<Qualification>> GetFilteredQualifications(int? level, int? startDateMonth,
int? startDateYear, string? awardingOrganisation, string? qualificationName)
int? startDateYear, string? awardingOrganisation,
string? qualificationName)
{
logger.LogInformation("Filtering options passed in - level: {Level}, startDateMonth: {StartDateMonth}, startDateYear: {StartDateYear}, awardingOrganisation: {AwardingOrganisation}, qualificationName: {qualificationName}",
logger.LogInformation("Filtering options passed in - level: {Level}, startDateMonth: {StartDateMonth}, startDateYear: {StartDateYear}, awardingOrganisation: {AwardingOrganisation}, qualificationName: {QualificationName}",
level,
startDateMonth,
startDateYear,
Expand All @@ -63,8 +64,9 @@ public async Task<List<Qualification>> GetFilteredQualifications(int? level, int
"All Higher Education Institutes",
"Various Awarding Organisations"
};
awardingOrganisations.AddRange(IncludeLinkedOrganisations(awardingOrganisation, startDateMonth, startDateYear));

awardingOrganisations.AddRange(IncludeLinkedOrganisations(awardingOrganisation, startDateMonth,
startDateYear));

queryBuilder = queryBuilder.FieldIncludes("fields.awardingOrganisationTitle", awardingOrganisations);
}

Expand All @@ -79,23 +81,24 @@ public async Task<List<Qualification>> GetFilteredQualifications(int? level, int
logger.LogError(e, "Error getting qualifications");
return [];
}

// apply start date filtering
var filteredQualifications = FilterQualificationsByDate(startDateMonth, startDateYear, qualifications.ToList());

// Filter based on qualification name
filteredQualifications = FilterQualificationsByName(filteredQualifications, qualificationName);

return filteredQualifications;
}

private static List<string> IncludeLinkedOrganisations(string awardingOrganisation, int? startDateMonth, int? startDateYear)
private static List<string> IncludeLinkedOrganisations(string awardingOrganisation, int? startDateMonth,
int? startDateYear)
{
var result = new List<string>();

if (awardingOrganisation is AwardingOrganisations.Edexcel or AwardingOrganisations.Pearson)
{
result.AddRange(new List<string>{AwardingOrganisations.Edexcel, AwardingOrganisations.Pearson});
result.AddRange(new List<string> { AwardingOrganisations.Edexcel, AwardingOrganisations.Pearson });
}
else if (awardingOrganisation is AwardingOrganisations.Ncfe or AwardingOrganisations.Cache
&& startDateMonth.HasValue && startDateYear.HasValue)
Expand All @@ -104,7 +107,7 @@ private static List<string> IncludeLinkedOrganisations(string awardingOrganisati
var date = new DateOnly(startDateYear.Value, startDateMonth.Value, 1);
if (date >= cutOffDate)
{
result.AddRange(new List<string>{AwardingOrganisations.Ncfe, AwardingOrganisations.Cache});
result.AddRange(new List<string> { AwardingOrganisations.Ncfe, AwardingOrganisations.Cache });
}
else
{
Expand All @@ -115,18 +118,20 @@ private static List<string> IncludeLinkedOrganisations(string awardingOrganisati
{
result.Add(awardingOrganisation);
}

return result;
}

private static List<Qualification> FilterQualificationsByName(List<Qualification> qualifications, string? qualificationName)
private List<Qualification> FilterQualificationsByName(
List<Qualification> qualifications,
string? qualificationName)
{
if (string.IsNullOrEmpty(qualificationName)) return qualifications;

var matchedQualifications = new List<Qualification>();
foreach (var qualification in qualifications)
{
var weight = Fuzz.PartialRatio(qualificationName, qualification.QualificationName);
var weight = fuzzyAdapter.PartialRatio(qualificationName, qualification.QualificationName);
if (weight > 70)
{
matchedQualifications.Add(qualification);
Expand All @@ -140,7 +145,7 @@ private List<Qualification> FilterQualificationsByDate(int? startDateMonth, int?
List<Qualification> qualifications)
{
if (!startDateMonth.HasValue || !startDateYear.HasValue) return qualifications;

var results = new List<Qualification>();
var enteredStartDate = new DateOnly(startDateYear.Value, startDateMonth.Value, Day);
foreach (var qualification in qualifications)
Expand Down
11 changes: 11 additions & 0 deletions src/Dfe.EarlyYearsQualification.Content/Services/FuzzyAdapter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using FuzzySharp;

namespace Dfe.EarlyYearsQualification.Content.Services;

public class FuzzyAdapter : IFuzzyAdapter
{
public int PartialRatio(string input1, string input2)
{
return Fuzz.PartialRatio(input1, input2);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Dfe.EarlyYearsQualification.Content.Services;

public interface IFuzzyAdapter
{
public int PartialRatio(string input1, string input2);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class QualificationListModel
public FilterModel Filters { get; init; } = new();

public NavigationLink? BackButton { get; init; }

public string SingleQualificationFoundText { get; init; } = string.Empty;

public string MultipleQualificationsFoundText { get; init; } = string.Empty;
Expand All @@ -28,7 +28,7 @@ public class QualificationListModel

public string PostSearchCriteriaContent { get; init; } = string.Empty;

public string? SearchCriteria { get; set; } = string.Empty;
public string? SearchCriteria { get; init; } = string.Empty;

public List<BasicQualificationModel> Qualifications { get; init; } = [];
}
3 changes: 2 additions & 1 deletion src/Dfe.EarlyYearsQualification.Web/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
return factory.GetUrlHelper(actionContext!);
});

builder.Services.AddSingleton<IFuzzyAdapter, FuzzyAdapter>();

var accessIsChallenged = !builder.Configuration.GetValue<bool>("ServiceAccess:IsPublic");
// ...by default, challenge the user for the secret value unless that's explicitly turned off

Expand Down Expand Up @@ -105,7 +107,6 @@

await app.RunAsync();


[ExcludeFromCodeCoverage]
// ReSharper disable once UnusedType.Global
// ...declared partial so we can exclude it from code coverage calculations
Expand Down
Loading

0 comments on commit 9d02989

Please sign in to comment.