diff --git a/src/Admin/NCafe.Admin.Api/Program.cs b/src/Admin/NCafe.Admin.Api/Program.cs index cb1d9d1..bc71dc9 100644 --- a/src/Admin/NCafe.Admin.Api/Program.cs +++ b/src/Admin/NCafe.Admin.Api/Program.cs @@ -11,7 +11,7 @@ // Add services to the container. builder.Services.AddEventStoreRepository(builder.Configuration) - .AddEventStoreProjectionService() + .AddEventStoreProjectionService(builder.Configuration) .AddInMemoryReadModelRepository() .AddHostedService(); diff --git a/src/Admin/NCafe.Admin.Api/appsettings.json b/src/Admin/NCafe.Admin.Api/appsettings.json index 8b9eb1e..b7b2dae 100644 --- a/src/Admin/NCafe.Admin.Api/appsettings.json +++ b/src/Admin/NCafe.Admin.Api/appsettings.json @@ -8,5 +8,8 @@ "AllowedHosts": "*", "ConnectionStrings": { "EventStore": "esdb://localhost:2113?tls=false" + }, + "EventStore": { + "EnableProjections": "true" } } diff --git a/src/Admin/NCafe.Admin.Domain.Tests/Commands/CreateProductTests.cs b/src/Admin/NCafe.Admin.Domain.Tests/Commands/CreateProductTests.cs index be316cc..33ca5ae 100644 --- a/src/Admin/NCafe.Admin.Domain.Tests/Commands/CreateProductTests.cs +++ b/src/Admin/NCafe.Admin.Domain.Tests/Commands/CreateProductTests.cs @@ -59,6 +59,6 @@ public async Task GivenValidProductInformation_ShouldCreateAndStoreProduct() // Assert A.CallTo(() => _repository.Save(A.That.Matches(p => p.Name == command.Name && p.Price == command.Price))) - .MustHaveHappenedOnceExactly(); ; + .MustHaveHappenedOnceExactly(); } } diff --git a/src/Barista/NCafe.Barista.Api/Program.cs b/src/Barista/NCafe.Barista.Api/Program.cs index 9e06f87..597d96d 100644 --- a/src/Barista/NCafe.Barista.Api/Program.cs +++ b/src/Barista/NCafe.Barista.Api/Program.cs @@ -21,7 +21,7 @@ // Add services to the container. builder.Services.AddEventStoreRepository(builder.Configuration) - .AddEventStoreProjectionService() + .AddEventStoreProjectionService(builder.Configuration) .AddInMemoryReadModelRepository() .AddHostedService(); diff --git a/src/Barista/NCafe.Barista.Api/appsettings.json b/src/Barista/NCafe.Barista.Api/appsettings.json index f7720fa..fbd7f8e 100644 --- a/src/Barista/NCafe.Barista.Api/appsettings.json +++ b/src/Barista/NCafe.Barista.Api/appsettings.json @@ -10,6 +10,9 @@ "EventStore": "esdb://localhost:2113?tls=false", "RabbitMq": "host=localhost;username=myuser;password=mypassword" }, + "EventStore": { + "EnableProjections": "true" + }, "RabbitMq": { "queuePrefix": "barista-service" } diff --git a/src/Cashier/NCafe.Cashier.Api.Tests/CashierWebApplicationFactory.cs b/src/Cashier/NCafe.Cashier.Api.Tests/CashierWebApplicationFactory.cs index 64e8981..dbefd1a 100644 --- a/src/Cashier/NCafe.Cashier.Api.Tests/CashierWebApplicationFactory.cs +++ b/src/Cashier/NCafe.Cashier.Api.Tests/CashierWebApplicationFactory.cs @@ -4,6 +4,7 @@ using Microsoft.Extensions.DependencyInjection; using NCafe.Cashier.Domain.ReadModels; using NCafe.Core.MessageBus; +using NCafe.Core.Projections; using NCafe.Core.ReadModels; using NCafe.Core.Repositories; @@ -15,6 +16,7 @@ public class CashierWebApplicationFactory private readonly IRepository _repository; private readonly IReadModelRepository _productRepository; private readonly IBusPublisher _busPublisher; + private readonly IProjectionService _productProjectionService; public IRepository FakeRepository => _repository; public IReadModelRepository FakeProductRepository => _productRepository; @@ -25,10 +27,12 @@ public CashierWebApplicationFactory() _repository = A.Fake(); _productRepository = A.Fake>(); _busPublisher = A.Fake(); + _productProjectionService = A.Fake>(); } protected override void ConfigureWebHost(IWebHostBuilder builder) { + Environment.SetEnvironmentVariable("EventStore__EnableProjections", "false"); Environment.SetEnvironmentVariable("RabbitMq__InitializeExchange", "false"); builder.ConfigureServices(services => @@ -45,9 +49,14 @@ protected override void ConfigureWebHost(IWebHostBuilder builder) d => d.ServiceType == typeof(IBusPublisher)); services.Remove(busPublisherDescriptor!); + var productProjectionServiceDescriptor = services.SingleOrDefault( + d => d.ServiceType == typeof(IProjectionService)); + services.Remove(productProjectionServiceDescriptor!); + services.AddSingleton(_repository); services.AddSingleton(_productRepository); services.AddSingleton(_busPublisher); + services.AddSingleton(_productProjectionService); }); builder.UseEnvironment("Tests"); diff --git a/src/Cashier/NCafe.Cashier.Api.Tests/PlaceOrderTests.cs b/src/Cashier/NCafe.Cashier.Api.Tests/PlaceOrderTests.cs index 51178dc..1829744 100644 --- a/src/Cashier/NCafe.Cashier.Api.Tests/PlaceOrderTests.cs +++ b/src/Cashier/NCafe.Cashier.Api.Tests/PlaceOrderTests.cs @@ -1,5 +1,6 @@ using FakeItEasy; using NCafe.Cashier.Domain.Entities; +using NCafe.Cashier.Domain.Messages; using Shouldly; using System.Net; using System.Net.Http.Json; @@ -28,5 +29,8 @@ public async Task PlaceOrder_ShouldReturnAcceptedStatus() // Assert response.StatusCode.ShouldBe(HttpStatusCode.Accepted); + + A.CallTo(() => _factory.FakeBusPublisher.Publish(A.That.Matches(p => p.Id == orderId))) + .MustHaveHappenedOnceExactly(); } } diff --git a/src/Cashier/NCafe.Cashier.Api/Program.cs b/src/Cashier/NCafe.Cashier.Api/Program.cs index e7d10e8..19e9276 100644 --- a/src/Cashier/NCafe.Cashier.Api/Program.cs +++ b/src/Cashier/NCafe.Cashier.Api/Program.cs @@ -13,7 +13,7 @@ // Add services to the container. builder.Services.AddEventStoreRepository(builder.Configuration) - .AddEventStoreProjectionService() + .AddEventStoreProjectionService(builder.Configuration) .AddInMemoryReadModelRepository() .AddHostedService(); diff --git a/src/Cashier/NCafe.Cashier.Api/appsettings.json b/src/Cashier/NCafe.Cashier.Api/appsettings.json index 139bc63..b4ce618 100644 --- a/src/Cashier/NCafe.Cashier.Api/appsettings.json +++ b/src/Cashier/NCafe.Cashier.Api/appsettings.json @@ -10,6 +10,9 @@ "EventStore": "esdb://localhost:2113?tls=false", "RabbitMq": "amqp://myuser:mypassword@localhost:5672" }, + "EventStore": { + "EnableProjections": "true" + }, "RabbitMq": { "ExchangeName": "cashier", "InitializeExchange": "true" diff --git a/src/Common/NCafe.Infrastructure/DependencyRegistration.cs b/src/Common/NCafe.Infrastructure/DependencyRegistration.cs index 4b5763a..dd390fc 100644 --- a/src/Common/NCafe.Infrastructure/DependencyRegistration.cs +++ b/src/Common/NCafe.Infrastructure/DependencyRegistration.cs @@ -30,10 +30,15 @@ public static IServiceCollection AddEventStoreRepository(this IServiceCollection return services; } - public static IServiceCollection AddEventStoreProjectionService(this IServiceCollection services) + public static IServiceCollection AddEventStoreProjectionService(this IServiceCollection services, IConfiguration configuration) where TModel : ReadModel { - return services.AddSingleton, EventStoreProjectionService>(); + if (configuration.GetValue("EventStore:EnableProjections", false)) + { + services.AddSingleton, EventStoreProjectionService>(); + } + + return services; } public static IServiceCollection AddRabbitMqPublisher(this IServiceCollection services, IConfiguration configuration)