Skip to content

Commit

Permalink
Added filters to the use-case request. (#47)
Browse files Browse the repository at this point in the history
* Added filters to the use-case request.

* change key-value pair to a list of FilterRequest

* change to ctor

---------

Co-authored-by: Spencer O'HEGARTY <[email protected]>
  • Loading branch information
CathLass and spencerohegartyDfE authored Sep 13, 2024
1 parent cd946a1 commit 6729df0
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public CognitiveSearchServiceAdapter(
/// Prescribes the context of the search including the keyword and collection target.
/// </param>
/// <returns>
/// A configured <<see cref="SearchResults"/> object hydrated from the results of the azure search.
/// A configured <see cref="SearchResults"/> object hydrated from the results of the azure search.
/// </returns>
/// <exception cref="ApplicationException">
/// An application exception is thrown if we either have no options configured, which
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/// <summary>
/// Prescribes the context of the search including
/// the keyword, search fields, and facets to use.
/// </summary>
public sealed class SearchServiceAdapterRequest
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
namespace Dfe.Data.SearchPrototype.SearchForEstablishments.ByKeyword.Usecase;

/// <summary>
/// Encapsulates a single filter to be applied to the search request
/// </summary>
public class FilterRequest
{
/// <summary>
/// The name of the filter field
/// </summary>
public string FilterName { get; }

/// <summary>
/// The readonly list of values of the filter to be included in the filter expression
/// </summary>
public IList<object> FilterValues => _filterValues.AsReadOnly();

private IList<object> _filterValues;

/// <summary>
/// Constructor that initialises the <see cref="FilterName"/> and the <see cref="FilterValues"/>
/// </summary>
/// <param name="filterName"></param>
/// <param name="filterValues"></param>
public FilterRequest(string filterName, IList<object> filterValues)
{
FilterName = filterName;
_filterValues = filterValues;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
public sealed class SearchByKeywordRequest
{
/// <summary>
/// The following arguments are passed via the constructor and used to create
/// The following search keyword argument is passed via the constructor and used to create
/// an immutable <see cref="SearchByKeywordRequest" /> instance which encapsulates
/// the parameters required to formulate a valid search request.
/// the search keyword required to formulate a valid (baseline) search request.
/// </summary>
/// <param name="searchKeyword">
/// The string keyword used to search the collection specified.
Expand All @@ -21,8 +21,29 @@ public SearchByKeywordRequest(string searchKeyword)
SearchKeyword = searchKeyword;
}

/// <summary>
/// The following search keyword and filter arguments are passed via the constructor and used to create
/// an immutable <see cref="SearchByKeywordRequest" /> instance which encapsulates the parameters required
/// to formulate a valid search request which are refined using the provisioned filters.
/// </summary>
/// <param name="searchKeyword">
/// The string keyword used to search the collection specified.
/// </param>
/// <param name="filterRequests">
/// The <see cref="FilterRequest"/> used to refine the search criteria.
/// </param>
public SearchByKeywordRequest(string searchKeyword, IList<FilterRequest> filterRequests) : this(searchKeyword)
{
FilterRequests = filterRequests;
}

/// <summary>
/// The string keyword used to search the collection specified.
/// </summary>
public string SearchKeyword { get; }

/// <summary>
/// The filter (key/values) used to refine the search criteria.
/// </summary>
public IList<FilterRequest>? FilterRequests { get; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Dfe.Data.SearchPrototype.SearchForEstablishments.ByKeyword.Usecase;
using FluentAssertions;
using Xunit;

namespace Dfe.Data.SearchPrototype.Tests.SearchForEstablishments.ByKeyword;

public class SearchByKeywordRequestTests
{
[Fact]
public void Constructor_WithFilterParam_PopulatesFilterRequests()
{
// arrange
var filterList = new List<FilterRequest>()
{
new FilterRequest("EducationPhase", new List<object>() {"Primary", "Secondary"}),
new FilterRequest("MaybeATypeCode", new List<object>() {1,2})
};

// act
var request = new SearchByKeywordRequest("searchKeyword", filterList);

// assert
request.FilterRequests.Should().NotBeNull();
foreach (var item in filterList)
{
var matchingRequest = request.FilterRequests!.First(x => x.FilterName == item.FilterName);
matchingRequest.FilterValues.Should().BeEquivalentTo(item.FilterValues);
}
}

[Fact]
public void Constructor_WithNoFilterParam_HasFilterRequestsNull()
{
// act
var request = new SearchByKeywordRequest("searchKeyword");

// assert
request.FilterRequests.Should().BeNull();
}
}

0 comments on commit 6729df0

Please sign in to comment.