diff --git a/Modules/Quotas/src/Quotas.Application/Infrastructure/Persistence/Repository/IChallengesRepository.cs b/Modules/Quotas/src/Quotas.Application/Infrastructure/Persistence/Repository/IChallengesRepository.cs index edc20d8e7a..ea6326d932 100644 --- a/Modules/Quotas/src/Quotas.Application/Infrastructure/Persistence/Repository/IChallengesRepository.cs +++ b/Modules/Quotas/src/Quotas.Application/Infrastructure/Persistence/Repository/IChallengesRepository.cs @@ -1,6 +1,9 @@ -namespace Backbone.Modules.Quotas.Application.Infrastructure.Persistence.Repository; +using System.Linq.Expressions; +using Backbone.Modules.Quotas.Domain.Aggregates.Challenges; + +namespace Backbone.Modules.Quotas.Application.Infrastructure.Persistence.Repository; public interface IChallengesRepository { - Task Count(string identityAddress, DateTime from, DateTime to, CancellationToken cancellationToken); + Task Count(Expression> filter, CancellationToken cancellationToken); } diff --git a/Modules/Quotas/src/Quotas.Application/Metrics/NumberOfCreatedChallengesMetricCalculator.cs b/Modules/Quotas/src/Quotas.Application/Metrics/NumberOfCreatedChallengesMetricCalculator.cs index 7701880353..181a557903 100644 --- a/Modules/Quotas/src/Quotas.Application/Metrics/NumberOfCreatedChallengesMetricCalculator.cs +++ b/Modules/Quotas/src/Quotas.Application/Metrics/NumberOfCreatedChallengesMetricCalculator.cs @@ -1,5 +1,6 @@ using Backbone.Modules.Quotas.Application.Infrastructure.Persistence.Repository; using Backbone.Modules.Quotas.Domain; +using Backbone.Modules.Quotas.Domain.Aggregates.Challenges; namespace Backbone.Modules.Quotas.Application.Metrics; @@ -14,7 +15,7 @@ public NumberOfCreatedChallengesMetricCalculator(IChallengesRepository challenge public async Task CalculateUsage(DateTime from, DateTime to, string identityAddress, CancellationToken cancellationToken) { - var numberOfCreatedChallenges = await _challengesRepository.Count(identityAddress, from, to, cancellationToken); + var numberOfCreatedChallenges = await _challengesRepository.Count(Challenge.WasCreatedInIntervalBy(from, to, identityAddress), cancellationToken); return numberOfCreatedChallenges; } } diff --git a/Modules/Quotas/src/Quotas.Domain/Aggregates/Challenges/Challenge.cs b/Modules/Quotas/src/Quotas.Domain/Aggregates/Challenges/Challenge.cs index 433299806f..af99ebcd53 100644 --- a/Modules/Quotas/src/Quotas.Domain/Aggregates/Challenges/Challenge.cs +++ b/Modules/Quotas/src/Quotas.Domain/Aggregates/Challenges/Challenge.cs @@ -1,4 +1,6 @@ -namespace Backbone.Modules.Quotas.Domain.Aggregates.Challenges; +using System.Linq.Expressions; + +namespace Backbone.Modules.Quotas.Domain.Aggregates.Challenges; public class Challenge { @@ -7,4 +9,9 @@ public class Challenge public required string Id { get; set; } public required DateTime ExpiresAt { get; set; } public required string? CreatedBy { get; set; } + + public static Expression> WasCreatedInIntervalBy(DateTime from, DateTime to, string identityAddress) + { + return c => c.ExpiresAt.AddMinutes(-EXPIRY_TIME_IN_MINUTES) > from && c.ExpiresAt.AddMinutes(-EXPIRY_TIME_IN_MINUTES) < to && c.CreatedBy == identityAddress; + } } diff --git a/Modules/Quotas/src/Quotas.Infrastructure/Persistence/Repository/ChallengesRepository.cs b/Modules/Quotas/src/Quotas.Infrastructure/Persistence/Repository/ChallengesRepository.cs index a1a1893686..0262719300 100644 --- a/Modules/Quotas/src/Quotas.Infrastructure/Persistence/Repository/ChallengesRepository.cs +++ b/Modules/Quotas/src/Quotas.Infrastructure/Persistence/Repository/ChallengesRepository.cs @@ -1,4 +1,5 @@ -using Backbone.Modules.Quotas.Application.Infrastructure.Persistence.Repository; +using System.Linq.Expressions; +using Backbone.Modules.Quotas.Application.Infrastructure.Persistence.Repository; using Backbone.Modules.Quotas.Domain.Aggregates.Challenges; using Backbone.Modules.Quotas.Infrastructure.Persistence.Database; using Microsoft.EntityFrameworkCore; @@ -14,12 +15,9 @@ public ChallengesRepository(QuotasDbContext dbContext) _readOnlyChallenges = dbContext.Challenges.AsNoTracking(); } - public async Task Count(string identityAddress, DateTime from, DateTime to, CancellationToken cancellationToken) + public async Task Count(Expression> filter, CancellationToken cancellationToken) { - var count = await _readOnlyChallenges - .CountAsync(c => c.ExpiresAt.AddMinutes(-Challenge.EXPIRY_TIME_IN_MINUTES) > from && c.ExpiresAt.AddMinutes(-Challenge.EXPIRY_TIME_IN_MINUTES) < to && c.CreatedBy == identityAddress, - cancellationToken); - + var count = await _readOnlyChallenges.CountAsync(filter, cancellationToken); return (uint)count; } }