Skip to content

Commit

Permalink
refactor: do not throw NotFoundException in TiersRepository.Find
Browse files Browse the repository at this point in the history
  • Loading branch information
tnotheis committed Feb 12, 2024
1 parent 3c3f612 commit 3e5e535
Show file tree
Hide file tree
Showing 13 changed files with 23 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Backbone.Modules.Quotas.Application.Infrastructure.Persistence.Reposit
public interface ITiersRepository
{
Task Add(Tier tier, CancellationToken cancellationToken);
Task<Tier> Find(string id, CancellationToken cancellationToken, bool track = false);
Task<Tier?> Find(string id, CancellationToken cancellationToken, bool track = false);
Task<TierQuotaDefinition> FindTierQuotaDefinition(string id, CancellationToken cancellationToken, bool track = false);
Task RemoveById(TierId tierId);
Task Update(Tier tier, CancellationToken cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -17,7 +18,7 @@ public TierDeletedIntegrationEventHandler(ILogger<TierDeletedIntegrationEventHan

public async Task Handle(TierDeletedIntegrationEvent integrationEvent)
{
var tier = await _tiersRepository.Find(integrationEvent.Id, CancellationToken.None);
var tier = await _tiersRepository.Find(integrationEvent.Id, CancellationToken.None) ?? throw new NotFoundException(nameof(Tier));

await _tiersRepository.RemoveById(tier.Id);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
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;
using Backbone.Modules.Quotas.Domain.Metrics;

namespace Backbone.Modules.Quotas.Application.IntegrationEvents.Incoming.TierOfIdentityChanged;
Expand All @@ -21,7 +22,7 @@ public TierOfIdentityChangedIntegrationEventHandler(IIdentitiesRepository identi
public async Task Handle(TierOfIdentityChangedIntegrationEvent @event)
{
var identity = await _identitiesRepository.Find(@event.IdentityAddress, CancellationToken.None, track: true) ?? throw new NotFoundException(nameof(Identity));
var newTier = await _tiersRepository.Find(@event.NewTier, CancellationToken.None, track: true);
var newTier = await _tiersRepository.Find(@event.NewTier, CancellationToken.None, track: true) ?? throw new NotFoundException(nameof(Tier));

await identity.ChangeTier(newTier, _metricCalculatorFactory, CancellationToken.None);

Expand Down
Original file line number Diff line number Diff line change
@@ -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.BuildingBlocks.Domain;
using Backbone.Modules.Quotas.Application.DTOs;
using Backbone.Modules.Quotas.Application.Infrastructure.Persistence.Repository;
Expand Down Expand Up @@ -29,7 +30,7 @@ public async Task<TierQuotaDefinitionDTO> 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);

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -16,7 +18,7 @@ public Handler(ITiersRepository tiersRepository, IMetricsRepository metricsRepos

public async Task<TierDetailsDTO> 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);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ public async Task Add(Tier tier, CancellationToken cancellationToken)
await _dbContext.SaveChangesAsync(cancellationToken);
}

public async Task<Tier> Find(string id, CancellationToken cancellationToken, bool track = false)
public async Task<Tier?> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public Task Add(Tier tier, CancellationToken cancellationToken)
return Task.CompletedTask;
}

public Task<Tier> Find(string id, CancellationToken cancellationToken, bool track = false)
public Task<Tier?> Find(string id, CancellationToken cancellationToken, bool track = false)
{
throw new NotImplementedException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public Task Add(Tier tier, CancellationToken cancellationToken)
throw new NotImplementedException();
}

public Task<Tier> Find(string id, CancellationToken cancellationToken, bool track = false)
public Task<Tier?> Find(string id, CancellationToken cancellationToken, bool track = false)
{
throw new NotImplementedException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ public Task Add(Tier tier, CancellationToken cancellationToken)
throw new NotImplementedException();
}

public Task<Tier> Find(string id, CancellationToken cancellationToken, bool track = false)
public Task<Tier?> Find(string id, CancellationToken cancellationToken, bool track = false)
{
return Task.FromResult(_tier);
return Task.FromResult((Tier?)_tier);
}

public Task<TierQuotaDefinition> FindTierQuotaDefinition(string id, CancellationToken cancellationToken, bool track = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@
namespace Backbone.Modules.Quotas.Application.Tests.Tests.IntegrationEvents.TierDeleted;
public class TierDeletedIntegrationEventHandlerTests
{
private readonly ILogger<TierDeletedIntegrationEventHandler> _logger;

public TierDeletedIntegrationEventHandlerTests()
{
_logger = A.Fake<ILogger<TierDeletedIntegrationEventHandler>>();
}

[Fact]
public async Task Deletes_tier_after_consuming_integration_event()
{
Expand All @@ -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<ILogger<TierDeletedIntegrationEventHandler>>(), tiersRepository);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<ITiersRepository>();
A.CallTo(() => tiersRepository.Find(tierId, A<CancellationToken>._, A<bool>._)).Returns(Task.FromResult<Tier>(null!));
A.CallTo(() => tiersRepository.Find(tierId, A<CancellationToken>._, A<bool>._)).Returns(Task.FromResult<Tier?>(null));

var handler = CreateHandler(tiersRepository);

Expand Down

0 comments on commit 3e5e535

Please sign in to comment.