Skip to content

Commit

Permalink
Initial commit of the contentful filter changes for the qualification…
Browse files Browse the repository at this point in the history
… list. Still a WIP
  • Loading branch information
sam-c-dfe committed Jul 8, 2024
1 parent c421c40 commit e042f3b
Show file tree
Hide file tree
Showing 15 changed files with 111 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

<ItemGroup>
<PackageReference Include="contentful.csharp" Version="7.6.0" />
<PackageReference Include="FuzzySharp" Version="2.0.2" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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;
Expand Down Expand Up @@ -38,13 +39,14 @@ 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)
int? startDateYear, string? awardingOrganisation, string? qualificationName)
{
logger.LogInformation("Filtering options passed in - level: {Level}, startDateMonth: {StartDateMonth}, startDateYear: {StartDateYear}, awardingOrganisation: {AwardingOrganisation}",
logger.LogInformation("Filtering options passed in - level: {Level}, startDateMonth: {StartDateMonth}, startDateYear: {StartDateYear}, awardingOrganisation: {AwardingOrganisation}, qualificationName: {qualificationName}",
level,
startDateMonth,
startDateYear,
awardingOrganisation);
awardingOrganisation,
qualificationName);

// create query builder
var queryBuilder = QueryBuilder.ContentTypeIs(ContentTypes.Qualification);
Expand All @@ -56,7 +58,13 @@ public async Task<List<Qualification>> GetFilteredQualifications(int? level, int

if (!string.IsNullOrEmpty(awardingOrganisation))
{
queryBuilder = queryBuilder.FieldEquals("fields.awardingOrganisationTitle", awardingOrganisation);
var awardingOrganisations = new List<string>
{
awardingOrganisation,
"All Higher Education Institutes",
"Various Awarding Organisations"
};
queryBuilder = queryBuilder.FieldIncludes("fields.awardingOrganisationTitle", awardingOrganisations);
}

// get qualifications
Expand All @@ -70,20 +78,43 @@ 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);

if (!startDateMonth.HasValue || !startDateYear.HasValue) return qualifications.ToList();
return filteredQualifications;
}

// apply start date filtering
var results = FilterQualificationsByDate(startDateMonth.Value, startDateYear.Value, qualifications);
private List<Qualification> FilterQualificationsByName(List<Qualification> qualifications, string? qualificationName)
{
if (string.IsNullOrEmpty(qualificationName))
{
return qualifications;
}

return results;
var matchedQualifications = new List<Qualification>();
foreach (var qualification in qualifications)
{
var weight = Fuzz.PartialRatio(qualificationName, qualification.QualificationName);
if (weight > 70)
{
matchedQualifications.Add(qualification);
}
}

return matchedQualifications;
}

private List<Qualification> FilterQualificationsByDate(int startDateMonth, int startDateYear,
ContentfulCollection<Qualification> qualifications)
private List<Qualification> FilterQualificationsByDate(int? startDateMonth, int? startDateYear,
List<Qualification> qualifications)
{
if (!startDateMonth.HasValue || !startDateYear.HasValue) return qualifications.ToList();

var results = new List<Qualification>();
var enteredStartDate = new DateOnly(startDateYear, startDateMonth, Day);
var enteredStartDate = new DateOnly(startDateYear.Value, startDateMonth.Value, Day);
foreach (var qualification in qualifications)
{
var qualificationStartDate = GetQualificationDate(qualification.FromWhichYear);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ namespace Dfe.EarlyYearsQualification.Content.Services;

public interface IContentFilterService
{
Task<List<Qualification>> GetFilteredQualifications(int? level, int? startDateMonth, int? startDateYear, string? awardingOrganisation);
Task<List<Qualification>> GetFilteredQualifications(int? level, int? startDateMonth, int? startDateYear, string? awardingOrganisation, string? qualificationName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Dfe.EarlyYearsQualification.Mock.Content;

public class MockContentfulFilterService : IContentFilterService
{
public Task<List<Qualification>> GetFilteredQualifications(int? level, int? startDateMonth, int? startDateYear, string? awardingOrganisation)
public Task<List<Qualification>> GetFilteredQualifications(int? level, int? startDateMonth, int? startDateYear, string? awardingOrganisation, string? qualificationName)
{
var qualifications =
new List<Qualification>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ public async Task<IActionResult> Get()

return View(model);
}

[HttpPost]
public IActionResult Refine(string refineSearch)
{
userJourneyCookieService.SetNameSearchCriteria(refineSearch);

return RedirectToAction("Get");
}

[HttpGet("qualification-details/{qualificationId}")]
public async Task<IActionResult> Index(string qualificationId)
Expand Down Expand Up @@ -69,9 +77,10 @@ private async Task<List<Qualification>> GetFilteredQualifications()
var level = userJourneyCookieService.GetLevelOfQualification();
var (startDateMonth, startDateYear) = userJourneyCookieService.GetWhenWasQualificationAwarded();
var awardingOrganisation = userJourneyCookieService.GetAwardingOrganisation();
var searchCriteria = userJourneyCookieService.GetSearchCriteria();

return await contentFilterService.GetFilteredQualifications(level, startDateMonth, startDateYear,
awardingOrganisation);
awardingOrganisation, searchCriteria);
}

private async Task<QualificationListModel> MapList(QualificationListPage content,
Expand All @@ -95,6 +104,7 @@ private async Task<QualificationListModel> MapList(QualificationListPage content
PostSearchCriteriaContent = await htmlRenderer.ToHtml(content.PostSearchCriteriaContent),
PostQualificationListContent = await htmlRenderer.ToHtml(content.PostQualificationListContent),
SearchCriteriaHeading = content.SearchCriteriaHeading,
SearchCriteria = userJourneyCookieService.GetSearchCriteria(),
Qualifications = basicQualificationsModels.OrderBy(x => x.QualificationName).ToList()
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ private async Task<List<Qualification>> GetFilteredQualifications()
{
int? level = userJourneyCookieService.GetLevelOfQualification();
(int? startDateMonth, int? startDateYear) = userJourneyCookieService.GetWhenWasQualificationAwarded();
return await contentFilterService.GetFilteredQualifications(level, startDateMonth, startDateYear, null);
return await contentFilterService.GetFilteredQualifications(level, startDateMonth, startDateYear, null, null);
}

private async Task<IActionResult> GetRadioView(string questionPageId, string actionName, string controllerName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,7 @@ public class QualificationListModel

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

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

public List<BasicQualificationModel> Qualifications { get; init; } = [];
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ public class UserJourneyModel
public string WhenWasQualificationAwarded { get; set; } = string.Empty;
public string LevelOfQualification { get; set; } = string.Empty;
public string WhatIsTheAwardingOrganisation { get; set; } = string.Empty;

public string SearchCriteria { get; set; } = string.Empty;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ public interface IUserJourneyCookieService
public void SetWhenWasQualificationAwarded(string date);
public void SetLevelOfQualification(string level);
public void SetAwardingOrganisation(string awardingOrganisation);

public void SetNameSearchCriteria(string searchCriteria);
public UserJourneyModel GetUserJourneyModelFromCookie();
public void ResetUserJourneyCookie();

public string? GetWhereWasQualificationAwarded();
public (int? startMonth, int? startYear) GetWhenWasQualificationAwarded();
public int? GetLevelOfQualification();
public string? GetAwardingOrganisation();

public string? GetSearchCriteria();
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ public void SetAwardingOrganisation(string awardingOrganisation)
SetJourneyCookie(model);
}

public void SetNameSearchCriteria(string searchCriteria)
{
var model = GetUserJourneyModelFromCookie();

model.SearchCriteria = searchCriteria;

SetJourneyCookie(model);
}

public UserJourneyModel GetUserJourneyModelFromCookie()
{
var cookie = context.HttpContext?.Request.Cookies[CookieKeyNames.UserJourneyKey];
Expand Down Expand Up @@ -132,6 +141,18 @@ public void ResetUserJourneyCookie()
return awardingOrganisation;
}

public string? GetSearchCriteria()
{
var cookie = GetUserJourneyModelFromCookie();
string? searchCriteria = null;
if (!string.IsNullOrEmpty(cookie.SearchCriteria))
{
searchCriteria = cookie.SearchCriteria;
}

return searchCriteria;
}

private void SetJourneyCookie(UserJourneyModel model)
{
var serializedCookie = JsonSerializer.Serialize(model);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,13 @@
</h3>
<div id="pre-search-content">@Html.Raw(Model.PreSearchBoxContent)</div>
<div class="govuk-form-group">
<input class="govuk-input govuk-!-width-three-quarters" id="refine-search" name="refine-search" aria-label="refine searYch box" type="text">
<button type="submit" class="govuk-button govuk-button--secondary" data-module="govuk-button">
@Model.SearchButtonText
</button>
@using (Html.BeginForm("Refine", "QualificationDetails", FormMethod.Post))
{
<input class="govuk-input govuk-!-width-three-quarters" id="refineSearch" name="refineSearch" aria-label="refine search box" type="text" value="@Model.SearchCriteria">
<button type="submit" class="govuk-button govuk-button--secondary" data-module="govuk-button">
@Model.SearchButtonText
</button>
}
</div>
@foreach (var qualification in Model.Qualifications)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public async Task Get_ReturnsView()

mockContentFilterService
.Setup(x => x.GetFilteredQualifications(It.IsAny<int?>(), It.IsAny<int?>(), It.IsAny<int?>(),
It.IsAny<string?>())).ReturnsAsync(new List<Qualification>());
It.IsAny<string?>(), It.IsAny<string?>())).ReturnsAsync(new List<Qualification>());

mockUserJourneyCookieService.Setup(x => x.GetUserJourneyModelFromCookie()).Returns(new UserJourneyModel());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,7 @@ public async Task WhatIsTheAwardingOrganisation_ContentServiceReturnsQuestionPag
It.IsAny<int?>(),
It.IsAny<int?>(),
It.IsAny<int?>(),
It.IsAny<string?>(),
It.IsAny<string?>()))
.ReturnsAsync([]);

Expand Down Expand Up @@ -672,7 +673,7 @@ public async Task
mockUserJourneyCookieService.Setup(x => x.GetUserJourneyModelFromCookie()).Returns(new UserJourneyModel());
mockContentFilterService
.Setup(x => x.GetFilteredQualifications(It.IsAny<int?>(), It.IsAny<int?>(), It.IsAny<int?>(),
It.IsAny<string?>()))
It.IsAny<string?>(), It.IsAny<string?>()))
.ReturnsAsync(listOfQualifications);

var controller = new QuestionsController(mockLogger.Object, mockContentService.Object, mockRenderer.Object,
Expand Down Expand Up @@ -738,7 +739,7 @@ public async Task
mockUserJourneyCookieService.Setup(x => x.GetUserJourneyModelFromCookie()).Returns(new UserJourneyModel());
mockContentFilterService
.Setup(x => x.GetFilteredQualifications(It.IsAny<int?>(), It.IsAny<int?>(), It.IsAny<int?>(),
It.IsAny<string?>()))
It.IsAny<string?>(), It.IsAny<string?>()))
.ReturnsAsync(listOfQualifications);

var controller = new QuestionsController(mockLogger.Object, mockContentService.Object, mockRenderer.Object,
Expand Down Expand Up @@ -789,7 +790,7 @@ public async Task Post_WhatIsTheAwardingOrganisation_InvalidModel_ReturnsQuestio
mockUserJourneyCookieService.Setup(x => x.GetUserJourneyModelFromCookie()).Returns(new UserJourneyModel());
mockContentFilterService
.Setup(x => x.GetFilteredQualifications(It.IsAny<int?>(), It.IsAny<int?>(), It.IsAny<int?>(),
It.IsAny<string?>()))
It.IsAny<string?>(), It.IsAny<string?>()))
.ReturnsAsync([]);

controller.ModelState.AddModelError("option", "test error");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public async Task GetFilteredQualifications_PassInLevel_ReturnsQualifications(
int level, string[] expectedQualifications)
{
var mockContentFilterService = new MockContentfulFilterService();
var results = await mockContentFilterService.GetFilteredQualifications(level, null, null, null);
var results = await mockContentFilterService.GetFilteredQualifications(level, null, null, null, null);

results.Count.Should().Be(2);
results[0].QualificationId.Should().Be(expectedQualifications[0]);
Expand Down
Loading

0 comments on commit e042f3b

Please sign in to comment.