|
| 1 | +using Codebreaker.GameAPIs.Data; |
| 2 | + |
| 3 | +namespace Codebreaker.Data.Postgres; |
| 4 | + |
| 5 | +public class GamesPostgresContext(DbContextOptions<GamesPostgresContext> options) : DbContext(options), IGamesRepository |
| 6 | +{ |
| 7 | + protected override void OnModelCreating(ModelBuilder modelBuilder) |
| 8 | + { |
| 9 | + modelBuilder.HasDefaultSchema("codebreaker"); |
| 10 | + modelBuilder.ApplyConfiguration(new GameConfiguration()); |
| 11 | + modelBuilder.ApplyConfiguration(new MoveConfiguration()); |
| 12 | + } |
| 13 | + |
| 14 | + public DbSet<Game> Games => Set<Game>(); |
| 15 | + public DbSet<Move> Moves => Set<Move>(); |
| 16 | + |
| 17 | + public async Task AddGameAsync(Game game, CancellationToken cancellationToken = default) |
| 18 | + { |
| 19 | + Games.Add(game); |
| 20 | + await SaveChangesAsync(cancellationToken); |
| 21 | + } |
| 22 | + |
| 23 | + public async Task AddMoveAsync(Game game, Move move, CancellationToken cancellationToken = default) |
| 24 | + { |
| 25 | + Moves.Add(move); |
| 26 | + Games.Update(game); |
| 27 | + |
| 28 | + await SaveChangesAsync(cancellationToken); |
| 29 | + } |
| 30 | + |
| 31 | + public async Task<bool> DeleteGameAsync(Guid id, CancellationToken cancellationToken = default) |
| 32 | + { |
| 33 | + var affected = await Games |
| 34 | + .Where(g => g.Id == id) |
| 35 | + .ExecuteDeleteAsync(cancellationToken); |
| 36 | + return affected == 1; |
| 37 | + } |
| 38 | + |
| 39 | + public async Task<Game?> GetGameAsync(Guid id, CancellationToken cancellationToken = default) |
| 40 | + { |
| 41 | + var game = await Games |
| 42 | + .Include("Moves") |
| 43 | + .TagWith(nameof(GetGameAsync)) |
| 44 | + .SingleOrDefaultAsync(g => g.Id == id, cancellationToken); |
| 45 | + return game; |
| 46 | + } |
| 47 | + |
| 48 | + public async Task<IEnumerable<Game>> GetGamesByDateAsync(string gameType, DateOnly date, CancellationToken cancellationToken = default) |
| 49 | + { |
| 50 | + var d = date.ToDateTime(TimeOnly.MinValue); |
| 51 | + var games = await Games |
| 52 | + .Where(g => g.GameType == gameType && g.StartTime.Date == d) |
| 53 | + .TagWith(nameof(GetGamesByDateAsync)) |
| 54 | + .ToListAsync(cancellationToken); |
| 55 | + return games; |
| 56 | + } |
| 57 | + |
| 58 | + public async Task<IEnumerable<Game>> GetGamesByPlayerAsync(string playerName, CancellationToken cancellationToken = default) |
| 59 | + { |
| 60 | + var games = await Games |
| 61 | + .Where(g => g.PlayerName == playerName) |
| 62 | + .TagWith(nameof(GetGamesByPlayerAsync)) |
| 63 | + .ToListAsync(cancellationToken); |
| 64 | + return games; |
| 65 | + } |
| 66 | + |
| 67 | + public async Task<IEnumerable<Game>> GetRunningGamesByPlayerAsync(string playerName, CancellationToken cancellationToken = default) |
| 68 | + { |
| 69 | + var games = await Games |
| 70 | + .Where(g => g.PlayerName == playerName && g.EndTime == null) |
| 71 | + .ToListAsync(cancellationToken); |
| 72 | + return games; |
| 73 | + } |
| 74 | + |
| 75 | + private const int MaxGamesReturned = 500; |
| 76 | + |
| 77 | + public async Task<IEnumerable<Game>> GetGamesAsync(GamesQuery gamesQuery, CancellationToken cancellationToken = default) |
| 78 | + { |
| 79 | + IQueryable<Game> query = Games |
| 80 | + .TagWith(nameof(GetGamesAsync)) |
| 81 | + .Include(g => g.Moves); |
| 82 | + |
| 83 | + // Apply Game filters if provided. |
| 84 | + if (gamesQuery.Date.HasValue) |
| 85 | + { |
| 86 | + DateTime begin = gamesQuery.Date.Value.ToDateTime(TimeOnly.MinValue); |
| 87 | + DateTime end = begin.AddDays(1); |
| 88 | + query = query.Where(g => g.StartTime < end && g.StartTime > begin); |
| 89 | + } |
| 90 | + if (gamesQuery.PlayerName != null) |
| 91 | + { |
| 92 | + query = query.Where(g => g.PlayerName == gamesQuery.PlayerName); |
| 93 | + } |
| 94 | + if (gamesQuery.GameType != null) |
| 95 | + { |
| 96 | + query = query.Where(g => g.GameType == gamesQuery.GameType); |
| 97 | + } |
| 98 | + if (gamesQuery.RunningOnly) |
| 99 | + { |
| 100 | + query = query.Where(g => g.EndTime == null); |
| 101 | + } |
| 102 | + if (gamesQuery.Ended) |
| 103 | + { |
| 104 | + query = query.Where(g => g.EndTime != null) |
| 105 | + .OrderBy(g => g.Duration); |
| 106 | + } |
| 107 | + else |
| 108 | + { |
| 109 | + query = query.OrderByDescending(g => g.StartTime); |
| 110 | + } |
| 111 | + |
| 112 | + query = query.Take(MaxGamesReturned); |
| 113 | + |
| 114 | + return await query.ToListAsync(cancellationToken); |
| 115 | + } |
| 116 | + |
| 117 | + public async Task<Game> UpdateGameAsync(Game game, CancellationToken cancellationToken = default) |
| 118 | + { |
| 119 | + Games.Update(game); |
| 120 | + await SaveChangesAsync(cancellationToken); |
| 121 | + return game; |
| 122 | + } |
| 123 | +} |
0 commit comments