From 3e5e5356861f4827313f5fdcf22fe701c1187142 Mon Sep 17 00:00:00 2001 From: Timo Notheisen Date: Mon, 12 Feb 2024 13:17:30 +0100 Subject: [PATCH] refactor: do not throw NotFoundException in TiersRepository.Find --- .../Persistence/Repository/ITiersRepository.cs | 2 +- .../IdentityCreatedIntegrationEventHandler.cs | 5 +++-- .../TierDeletedIntegrationEventHandler.cs | 3 ++- ...TierOfIdentityChangedIntegrationEventHandler.cs | 3 ++- .../Tiers/Commands/CreateQuotaForTier/Handler.cs | 5 +++-- .../Tiers/Queries/GetTierById/Handler.cs | 6 ++++-- .../TiersQueryableExtensions.cs | 14 -------------- .../Persistence/Repository/TiersRepository.cs | 4 ++-- .../TestDoubles/AddMockTiersRepository.cs | 2 +- .../FindTierQuotaDefinitionsStubRepository.cs | 2 +- .../TestDoubles/FindTiersStubRepository.cs | 4 ++-- .../TierDeletedIntegrationEventHandlerTests.cs | 9 +-------- .../DeleteTierQuotaDefinition/HandlerTests.cs | 2 +- 13 files changed, 23 insertions(+), 38 deletions(-) delete mode 100644 Modules/Quotas/src/Quotas.Infrastructure/Persistence/Database/QueryableExtensions/TiersQueryableExtensions.cs diff --git a/Modules/Quotas/src/Quotas.Application/Infrastructure/Persistence/Repository/ITiersRepository.cs b/Modules/Quotas/src/Quotas.Application/Infrastructure/Persistence/Repository/ITiersRepository.cs index 4128d0053c..3210a561bf 100644 --- a/Modules/Quotas/src/Quotas.Application/Infrastructure/Persistence/Repository/ITiersRepository.cs +++ b/Modules/Quotas/src/Quotas.Application/Infrastructure/Persistence/Repository/ITiersRepository.cs @@ -4,7 +4,7 @@ namespace Backbone.Modules.Quotas.Application.Infrastructure.Persistence.Reposit public interface ITiersRepository { Task Add(Tier tier, CancellationToken cancellationToken); - Task Find(string id, CancellationToken cancellationToken, bool track = false); + Task Find(string id, CancellationToken cancellationToken, bool track = false); Task FindTierQuotaDefinition(string id, CancellationToken cancellationToken, bool track = false); Task RemoveById(TierId tierId); Task Update(Tier tier, CancellationToken cancellationToken); diff --git a/Modules/Quotas/src/Quotas.Application/IntegrationEvents/Incoming/IdentityCreated/IdentityCreatedIntegrationEventHandler.cs b/Modules/Quotas/src/Quotas.Application/IntegrationEvents/Incoming/IdentityCreated/IdentityCreatedIntegrationEventHandler.cs index bcd6c80528..e458776087 100644 --- a/Modules/Quotas/src/Quotas.Application/IntegrationEvents/Incoming/IdentityCreated/IdentityCreatedIntegrationEventHandler.cs +++ b/Modules/Quotas/src/Quotas.Application/IntegrationEvents/Incoming/IdentityCreated/IdentityCreatedIntegrationEventHandler.cs @@ -1,4 +1,5 @@ -using Backbone.BuildingBlocks.Application.Abstractions.Infrastructure.EventBus; +using Backbone.BuildingBlocks.Application.Abstractions.Exceptions; +using Backbone.BuildingBlocks.Application.Abstractions.Infrastructure.EventBus; using Backbone.Modules.Quotas.Application.Infrastructure.Persistence.Repository; using Backbone.Modules.Quotas.Domain.Aggregates.Identities; using Backbone.Modules.Quotas.Domain.Aggregates.Tiers; @@ -28,7 +29,7 @@ public async Task Handle(IdentityCreatedIntegrationEvent integrationEvent) var identity = new Identity(integrationEvent.Address, new TierId(integrationEvent.Tier)); - var tier = await _tiersRepository.Find(identity.TierId, CancellationToken.None, track: true); + var tier = await _tiersRepository.Find(identity.TierId, CancellationToken.None, track: true) ?? throw new NotFoundException(nameof(Tier)); foreach (var tierQuotaDefinition in tier.Quotas) { diff --git a/Modules/Quotas/src/Quotas.Application/IntegrationEvents/Incoming/TierDeleted/TierDeletedIntegrationEventHandler.cs b/Modules/Quotas/src/Quotas.Application/IntegrationEvents/Incoming/TierDeleted/TierDeletedIntegrationEventHandler.cs index a835736ff7..a364bec729 100644 --- a/Modules/Quotas/src/Quotas.Application/IntegrationEvents/Incoming/TierDeleted/TierDeletedIntegrationEventHandler.cs +++ b/Modules/Quotas/src/Quotas.Application/IntegrationEvents/Incoming/TierDeleted/TierDeletedIntegrationEventHandler.cs @@ -1,3 +1,4 @@ +using Backbone.BuildingBlocks.Application.Abstractions.Exceptions; using Backbone.BuildingBlocks.Application.Abstractions.Infrastructure.EventBus; using Backbone.Modules.Quotas.Application.Infrastructure.Persistence.Repository; using Backbone.Modules.Quotas.Domain.Aggregates.Tiers; @@ -17,7 +18,7 @@ public TierDeletedIntegrationEventHandler(ILogger Handle(CreateQuotaForTierCommand reque { _logger.LogTrace("Handling CreateQuotaForTierCommand ..."); - var tier = await _tiersRepository.Find(request.TierId, cancellationToken, true); + var tier = await _tiersRepository.Find(request.TierId, cancellationToken, true) ?? throw new NotFoundException(nameof(Tier)); var parseMetricKeyResult = MetricKey.Parse(request.MetricKey); diff --git a/Modules/Quotas/src/Quotas.Application/Tiers/Queries/GetTierById/Handler.cs b/Modules/Quotas/src/Quotas.Application/Tiers/Queries/GetTierById/Handler.cs index 8dd0c3cfe1..a18f44153b 100644 --- a/Modules/Quotas/src/Quotas.Application/Tiers/Queries/GetTierById/Handler.cs +++ b/Modules/Quotas/src/Quotas.Application/Tiers/Queries/GetTierById/Handler.cs @@ -1,5 +1,7 @@ -using Backbone.Modules.Quotas.Application.DTOs; +using Backbone.BuildingBlocks.Application.Abstractions.Exceptions; +using Backbone.Modules.Quotas.Application.DTOs; using Backbone.Modules.Quotas.Application.Infrastructure.Persistence.Repository; +using Backbone.Modules.Quotas.Domain.Aggregates.Tiers; using MediatR; namespace Backbone.Modules.Quotas.Application.Tiers.Queries.GetTierById; @@ -16,7 +18,7 @@ public Handler(ITiersRepository tiersRepository, IMetricsRepository metricsRepos public async Task Handle(GetTierByIdQuery request, CancellationToken cancellationToken) { - var tier = await _tiersRepository.Find(request.Id, cancellationToken); + var tier = await _tiersRepository.Find(request.Id, cancellationToken) ?? throw new NotFoundException(nameof(Tier)); var metricsKeys = tier.Quotas.Select(q => q.MetricKey).Distinct(); var metrics = await _metricsRepository.FindAllWithKeys(metricsKeys, cancellationToken); diff --git a/Modules/Quotas/src/Quotas.Infrastructure/Persistence/Database/QueryableExtensions/TiersQueryableExtensions.cs b/Modules/Quotas/src/Quotas.Infrastructure/Persistence/Database/QueryableExtensions/TiersQueryableExtensions.cs deleted file mode 100644 index 634ba87250..0000000000 --- a/Modules/Quotas/src/Quotas.Infrastructure/Persistence/Database/QueryableExtensions/TiersQueryableExtensions.cs +++ /dev/null @@ -1,14 +0,0 @@ -using Backbone.BuildingBlocks.Application.Abstractions.Exceptions; -using Backbone.Modules.Quotas.Domain.Aggregates.Tiers; -using Microsoft.EntityFrameworkCore; - -namespace Backbone.Modules.Quotas.Infrastructure.Persistence.Database.QueryableExtensions; - -public static class TiersQueryableExtensions -{ - public static async Task FirstWithId(this IQueryable query, string id, CancellationToken cancellationToken) - { - var tier = await query.FirstOrDefaultAsync(m => m.Id == id, cancellationToken) ?? throw new NotFoundException(nameof(Tier)); - return tier; - } -} diff --git a/Modules/Quotas/src/Quotas.Infrastructure/Persistence/Repository/TiersRepository.cs b/Modules/Quotas/src/Quotas.Infrastructure/Persistence/Repository/TiersRepository.cs index 3d49c6c7e8..ae6eb87133 100644 --- a/Modules/Quotas/src/Quotas.Infrastructure/Persistence/Repository/TiersRepository.cs +++ b/Modules/Quotas/src/Quotas.Infrastructure/Persistence/Repository/TiersRepository.cs @@ -30,11 +30,11 @@ public async Task Add(Tier tier, CancellationToken cancellationToken) await _dbContext.SaveChangesAsync(cancellationToken); } - public async Task Find(string id, CancellationToken cancellationToken, bool track = false) + public async Task Find(string id, CancellationToken cancellationToken, bool track = false) { var tier = await (track ? _tiers : _readOnlyTiers) .IncludeAll(_dbContext) - .FirstWithId(id, cancellationToken); + .FirstOrDefaultAsync(m => m.Id == id, cancellationToken); return tier; } diff --git a/Modules/Quotas/test/Quotas.Application.Tests/TestDoubles/AddMockTiersRepository.cs b/Modules/Quotas/test/Quotas.Application.Tests/TestDoubles/AddMockTiersRepository.cs index 3969171b24..382312bc4a 100644 --- a/Modules/Quotas/test/Quotas.Application.Tests/TestDoubles/AddMockTiersRepository.cs +++ b/Modules/Quotas/test/Quotas.Application.Tests/TestDoubles/AddMockTiersRepository.cs @@ -15,7 +15,7 @@ public Task Add(Tier tier, CancellationToken cancellationToken) return Task.CompletedTask; } - public Task Find(string id, CancellationToken cancellationToken, bool track = false) + public Task Find(string id, CancellationToken cancellationToken, bool track = false) { throw new NotImplementedException(); } diff --git a/Modules/Quotas/test/Quotas.Application.Tests/TestDoubles/FindTierQuotaDefinitionsStubRepository.cs b/Modules/Quotas/test/Quotas.Application.Tests/TestDoubles/FindTierQuotaDefinitionsStubRepository.cs index ca156fa7ac..a424c89cf4 100644 --- a/Modules/Quotas/test/Quotas.Application.Tests/TestDoubles/FindTierQuotaDefinitionsStubRepository.cs +++ b/Modules/Quotas/test/Quotas.Application.Tests/TestDoubles/FindTierQuotaDefinitionsStubRepository.cs @@ -17,7 +17,7 @@ public Task Add(Tier tier, CancellationToken cancellationToken) throw new NotImplementedException(); } - public Task Find(string id, CancellationToken cancellationToken, bool track = false) + public Task Find(string id, CancellationToken cancellationToken, bool track = false) { throw new NotImplementedException(); } diff --git a/Modules/Quotas/test/Quotas.Application.Tests/TestDoubles/FindTiersStubRepository.cs b/Modules/Quotas/test/Quotas.Application.Tests/TestDoubles/FindTiersStubRepository.cs index 4f1d9c20d8..a220368d24 100644 --- a/Modules/Quotas/test/Quotas.Application.Tests/TestDoubles/FindTiersStubRepository.cs +++ b/Modules/Quotas/test/Quotas.Application.Tests/TestDoubles/FindTiersStubRepository.cs @@ -17,9 +17,9 @@ public Task Add(Tier tier, CancellationToken cancellationToken) throw new NotImplementedException(); } - public Task Find(string id, CancellationToken cancellationToken, bool track = false) + public Task Find(string id, CancellationToken cancellationToken, bool track = false) { - return Task.FromResult(_tier); + return Task.FromResult((Tier?)_tier); } public Task FindTierQuotaDefinition(string id, CancellationToken cancellationToken, bool track = false) diff --git a/Modules/Quotas/test/Quotas.Application.Tests/Tests/IntegrationEvents/TierDeleted/TierDeletedIntegrationEventHandlerTests.cs b/Modules/Quotas/test/Quotas.Application.Tests/Tests/IntegrationEvents/TierDeleted/TierDeletedIntegrationEventHandlerTests.cs index b50c99fffc..81035ee391 100644 --- a/Modules/Quotas/test/Quotas.Application.Tests/Tests/IntegrationEvents/TierDeleted/TierDeletedIntegrationEventHandlerTests.cs +++ b/Modules/Quotas/test/Quotas.Application.Tests/Tests/IntegrationEvents/TierDeleted/TierDeletedIntegrationEventHandlerTests.cs @@ -8,13 +8,6 @@ namespace Backbone.Modules.Quotas.Application.Tests.Tests.IntegrationEvents.TierDeleted; public class TierDeletedIntegrationEventHandlerTests { - private readonly ILogger _logger; - - public TierDeletedIntegrationEventHandlerTests() - { - _logger = A.Fake>(); - } - [Fact] public async Task Deletes_tier_after_consuming_integration_event() { @@ -35,6 +28,6 @@ public async Task Deletes_tier_after_consuming_integration_event() private TierDeletedIntegrationEventHandler CreateHandler(ITiersRepository tiersRepository) { - return new(_logger, tiersRepository); + return new TierDeletedIntegrationEventHandler(A.Dummy>(), tiersRepository); } } diff --git a/Modules/Quotas/test/Quotas.Application.Tests/Tests/Quotas/DeleteTierQuotaDefinition/HandlerTests.cs b/Modules/Quotas/test/Quotas.Application.Tests/Tests/Quotas/DeleteTierQuotaDefinition/HandlerTests.cs index 9a1b8f1550..08825ae490 100644 --- a/Modules/Quotas/test/Quotas.Application.Tests/Tests/Quotas/DeleteTierQuotaDefinition/HandlerTests.cs +++ b/Modules/Quotas/test/Quotas.Application.Tests/Tests/Quotas/DeleteTierQuotaDefinition/HandlerTests.cs @@ -114,7 +114,7 @@ public async Task Fails_to_delete_tier_quota_definition_for_missing_tier() var command = new DeleteTierQuotaDefinitionCommand(tierId, "SomeTierQuotaDefinitionId"); var tiersRepository = A.Fake(); - A.CallTo(() => tiersRepository.Find(tierId, A._, A._)).Returns(Task.FromResult(null!)); + A.CallTo(() => tiersRepository.Find(tierId, A._, A._)).Returns(Task.FromResult(null)); var handler = CreateHandler(tiersRepository);