diff --git a/Childrens-Social-Care-CPD-Tests/Controllers/ResourcesControllerTests.cs b/Childrens-Social-Care-CPD-Tests/Controllers/ResourcesControllerTests.cs index acd1bc04..bf011a98 100644 --- a/Childrens-Social-Care-CPD-Tests/Controllers/ResourcesControllerTests.cs +++ b/Childrens-Social-Care-CPD-Tests/Controllers/ResourcesControllerTests.cs @@ -14,7 +14,7 @@ namespace Childrens_Social_Care_CPD_Tests.Controllers; public class ResourcesControllerTests { - private IResourcesSearchStrategyFactory _searchStrategyFactory; + private IResourcesSearchStrategy _searchStrategy; private ResourcesController _resourcesController; private IRequestCookieCollection _cookies; private HttpContext _httpContext; @@ -31,9 +31,9 @@ public void SetUp() _httpRequest.Cookies.Returns(_cookies); _httpContext.Request.Returns(_httpRequest); controllerContext.HttpContext = _httpContext; - - _searchStrategyFactory = Substitute.For(); - _resourcesController = new ResourcesController(_searchStrategyFactory) + + _searchStrategy = Substitute.For(); + _resourcesController = new ResourcesController(_searchStrategy) { ControllerContext = controllerContext, TempData = Substitute.For() @@ -44,10 +44,8 @@ public void SetUp() public async Task Search_Returns_Strategy_Model() { // arrange - var strategy = Substitute.For(); - _searchStrategyFactory.Create().Returns(strategy); var model = new ResourcesListViewModel(null, null, null, null); - strategy.SearchAsync(Arg.Any(), Arg.Any()).Returns(model); + _searchStrategy.SearchAsync(Arg.Any(), Arg.Any()).Returns(model); // act var actual = await _resourcesController.Search(query: null) as ViewResult; diff --git a/Childrens-Social-Care-CPD-Tests/Core/Resources/ResourcesSearchStrategyFactoryTests.cs b/Childrens-Social-Care-CPD-Tests/Core/Resources/ResourcesSearchStrategyFactoryTests.cs deleted file mode 100644 index b8e9c525..00000000 --- a/Childrens-Social-Care-CPD-Tests/Core/Resources/ResourcesSearchStrategyFactoryTests.cs +++ /dev/null @@ -1,30 +0,0 @@ -using Castle.Core.Logging; -using Childrens_Social_Care_CPD.Configuration; -using Childrens_Social_Care_CPD.Core.Resources; -using Childrens_Social_Care_CPD.DataAccess; -using FluentAssertions; -using Microsoft.Extensions.Logging; -using NSubstitute; -using NUnit.Framework; -using System; - -namespace Childrens_Social_Care_CPD_Tests.Core.Resources; - -public class ResourcesSearchStrategyFactoryTests -{ - [TestCase(true, typeof(ResourcesDynamicTagsSearchStategy))] - [TestCase(false, typeof(ResourcesFixedTagsSearchStrategy))] - public void Creates_Correct_Strategy(bool isFeatureOn, Type type) - { - // arrange - var featuresConfig = Substitute.For(); - featuresConfig.IsEnabled(Features.ResourcesUseDynamicTags).Returns(isFeatureOn); - var sut = new ResourcesSearchStrategyFactory(featuresConfig, Substitute.For(), Substitute.For>()); - - // act - var actual = sut.Create(); - - // assert - actual.Should().BeOfType(type); - } -} diff --git a/Childrens-Social-Care-CPD/Controllers/ResourcesController.cs b/Childrens-Social-Care-CPD/Controllers/ResourcesController.cs index bd62688c..f8e26575 100644 --- a/Childrens-Social-Care-CPD/Controllers/ResourcesController.cs +++ b/Childrens-Social-Care-CPD/Controllers/ResourcesController.cs @@ -17,23 +17,23 @@ public ResourcesQuery() public class ResourcesController : Controller { - private readonly IResourcesSearchStrategyFactory _factory; + private readonly IResourcesSearchStrategy _strategy; - public ResourcesController(IResourcesSearchStrategyFactory factory) + public ResourcesController(IResourcesSearchStrategy strategy) { - _factory = factory; + ArgumentNullException.ThrowIfNull(strategy); + + _strategy = strategy; } [Route("resources", Name = "Resource")] [HttpGet] public async Task Search([FromQuery] ResourcesQuery query, bool preferencesSet = false, CancellationToken cancellationToken = default) { - var strategy = _factory.Create(); - var contextModel = new ContextModel(string.Empty, "Resources", "Resources", "Resources", true, preferencesSet); ViewData["ContextModel"] = contextModel; - var viewModel = await strategy.SearchAsync(query, cancellationToken); + var viewModel = await _strategy.SearchAsync(query, cancellationToken); return View(viewModel); } } diff --git a/Childrens-Social-Care-CPD/Core/Resources/IResourcesSearchStrategyFactory.cs b/Childrens-Social-Care-CPD/Core/Resources/IResourcesSearchStrategyFactory.cs deleted file mode 100644 index af410870..00000000 --- a/Childrens-Social-Care-CPD/Core/Resources/IResourcesSearchStrategyFactory.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace Childrens_Social_Care_CPD.Core.Resources; - -public interface IResourcesSearchStrategyFactory -{ - public IResourcesSearchStrategy Create(); -} \ No newline at end of file diff --git a/Childrens-Social-Care-CPD/Core/Resources/ResourcesSearchStrategyFactory.cs b/Childrens-Social-Care-CPD/Core/Resources/ResourcesSearchStrategyFactory.cs deleted file mode 100644 index 7c435952..00000000 --- a/Childrens-Social-Care-CPD/Core/Resources/ResourcesSearchStrategyFactory.cs +++ /dev/null @@ -1,25 +0,0 @@ -using Childrens_Social_Care_CPD.Configuration; -using Childrens_Social_Care_CPD.DataAccess; - -namespace Childrens_Social_Care_CPD.Core.Resources; - -internal class ResourcesSearchStrategyFactory : IResourcesSearchStrategyFactory -{ - private readonly IFeaturesConfig _featuresConfig; - private readonly IResourcesRepository _resourcesRepository; - private readonly ILogger _fixedLogger; - - public ResourcesSearchStrategyFactory(IFeaturesConfig featuresConfig, IResourcesRepository resourcesRepository, ILogger fixedLogger) - { - _featuresConfig = featuresConfig; - _resourcesRepository = resourcesRepository; - _fixedLogger = fixedLogger; - } - - public IResourcesSearchStrategy Create() - { - return _featuresConfig.IsEnabled(Features.ResourcesUseDynamicTags) - ? new ResourcesDynamicTagsSearchStategy(_resourcesRepository) - : new ResourcesFixedTagsSearchStrategy(_resourcesRepository, _fixedLogger) ; - } -} diff --git a/Childrens-Social-Care-CPD/WebApplicationBuilderExtensions.cs b/Childrens-Social-Care-CPD/WebApplicationBuilderExtensions.cs index 5132234d..7eff7dd5 100644 --- a/Childrens-Social-Care-CPD/WebApplicationBuilderExtensions.cs +++ b/Childrens-Social-Care-CPD/WebApplicationBuilderExtensions.cs @@ -9,6 +9,7 @@ using GraphQL.Client.Http; using GraphQL.Client.Serializer.SystemTextJson; using Microsoft.ApplicationInsights.AspNetCore.Extensions; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging.ApplicationInsights; using Microsoft.Extensions.Logging.AzureAppServices; using System.Diagnostics.CodeAnalysis; @@ -29,8 +30,18 @@ public static void AddDependencies(this WebApplicationBuilder builder) builder.Services.AddSingleton(); builder.Services.AddTransient(); builder.Services.AddTransient(); - builder.Services.AddTransient(); - builder.Services.AddScoped(); + builder.Services.AddTransient(); + + // Resources search feature + builder.Services.AddScoped(); + builder.Services.AddScoped(); + builder.Services.AddScoped(services => + { + var featuresConfig = services.GetService(); + return featuresConfig.IsEnabled(Features.ResourcesUseDynamicTags) + ? services.GetService() + : services.GetService(); + }); builder.Services.AddScoped(services => { var config = services.GetService();