Skip to content

Commit

Permalink
Now uses the maxCount parameter for querying report-games
Browse files Browse the repository at this point in the history
  • Loading branch information
szv committed Jun 27, 2023
1 parent 06ffd88 commit a19bcce
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
43 changes: 43 additions & 0 deletions src/Services/Report/src/Data/Repositories/Args/GetGamesArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
namespace CodeBreaker.Services.Report.Data.Repositories.Args;

public class GetGamesArgs
{
private DateTime _to = DateTime.Today.AddDays(1);
private int _maxCount = 1000;

public GetGamesArgs()
{
}

public GetGamesArgs(DateTime from, DateTime to)
{
From = from;
To = to;
}

public DateTime From { get; protected init; } = DateTime.Today.AddDays(-10);

public DateTime To
{
get => _to;
init
{
if (value < From)
throw new ArgumentOutOfRangeException(nameof(To), $"Must not be smaller than {nameof(From)}");

_to = value;
}
}

public int MaxCount
{
get => _maxCount;
init
{
if (value < 0)
throw new ArgumentOutOfRangeException(nameof(MaxCount), "Must not be smaller than 0");

_maxCount = value;
}
}
}
6 changes: 4 additions & 2 deletions src/Services/Report/src/Data/Repositories/GameRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@
using CodeBreaker.Services.Report.Data.Models;
using CodeBreaker.Services.Report.Data.Models.Fields;
using CodeBreaker.Services.Report.Data.Models.KeyPegs;
using CodeBreaker.Services.Report.Data.Repositories.Args;

namespace CodeBreaker.Services.Report.Data.Repositories;

public class GameRepository(ReportDbContext dbContext) : IGameRepository
{
public IAsyncEnumerable<Game> GetAsync(DateTime from, DateTime to, CancellationToken cancellationToken = default) =>
public IAsyncEnumerable<Game> GetAsync(GetGamesArgs args, CancellationToken cancellationToken = default) =>
dbContext.Games
.Include(x => x.Code)
.Include(game => game.Moves.OrderBy(move => move.CreatedAt))
.ThenInclude(move => move.Fields.OrderBy(field => field.Position))
.Include(game => game.Moves.OrderBy(move => move.CreatedAt))
.ThenInclude(move => move.KeyPegs!.OrderBy(keyPeg => keyPeg.Position))
.Where(x => x.Start >= from && x.Start < to)
.Where(x => x.Start >= args.From && x.Start < args.To)
.Take(args.MaxCount)
.AsAsyncEnumerable();

public async Task<Game> GetAsync(Guid gameId, CancellationToken cancellationToken = default) =>
Expand Down
3 changes: 2 additions & 1 deletion src/Services/Report/src/Data/Repositories/IGameRepository.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using CodeBreaker.Services.Report.Data.Models;
using CodeBreaker.Services.Report.Data.Repositories.Args;

namespace CodeBreaker.Services.Report.Data.Repositories;
public interface IGameRepository
{
Task<Game> CreateAsync(Game game, CancellationToken cancellationToken = default);
Task DeleteAsync(Guid gameId, CancellationToken cancellationToken = default);
IAsyncEnumerable<Game> GetAsync(DateTime from, DateTime to, CancellationToken cancellationToken = default);
IAsyncEnumerable<Game> GetAsync(GetGamesArgs args, CancellationToken cancellationToken = default);
Task<Game> GetAsync(Guid gameId, CancellationToken cancellationToken = default);
Task UpdateAsync(Guid gameId, Game game, CancellationToken cancellationToken = default);
Task AddMoveAsync(Guid gameId, Move move, CancellationToken cancellationToken = default);
Expand Down
2 changes: 1 addition & 1 deletion src/Services/Report/src/WebApi/Services/GameService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace CodeBreaker.Services.Report.WebApi.Services;
internal class GameService(IGameRepository repository) : IGameService
{
public IAsyncEnumerable<Game> GetGamesAsync(GetGamesArgs args, CancellationToken cancellationToken = default) =>
repository.GetAsync(args.From, args.To, cancellationToken);
repository.GetAsync(new Data.Repositories.Args.GetGamesArgs (args.From, args.To) { MaxCount = args.MaxCount }, cancellationToken);

public async Task<Game> GetGameAsync(GetGameArgs args, CancellationToken cancellationToken = default) =>
await repository.GetAsync(args.Id, cancellationToken);
Expand Down

0 comments on commit a19bcce

Please sign in to comment.