Skip to content

Commit

Permalink
refactor: use expression for challenge filter when counting
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-almeida-konkconsulting committed Jun 25, 2024
1 parent a09477f commit ff9b7f0
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -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<uint> Count(string identityAddress, DateTime from, DateTime to, CancellationToken cancellationToken);
Task<uint> Count(Expression<Func<Challenge, bool>> filter, CancellationToken cancellationToken);
}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -14,7 +15,7 @@ public NumberOfCreatedChallengesMetricCalculator(IChallengesRepository challenge

public async Task<uint> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -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<Func<Challenge, bool>> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -14,12 +15,9 @@ public ChallengesRepository(QuotasDbContext dbContext)
_readOnlyChallenges = dbContext.Challenges.AsNoTracking();
}

public async Task<uint> Count(string identityAddress, DateTime from, DateTime to, CancellationToken cancellationToken)
public async Task<uint> Count(Expression<Func<Challenge, bool>> 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;
}
}

0 comments on commit ff9b7f0

Please sign in to comment.