final POC
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
import { Repository, LessThan } from 'typeorm';
|
||||
import { AppDataSource } from '../ormconfig';
|
||||
import { IGameSnapshotRepository } from '../../Domain/IRepository/IGameSnapshotRepository';
|
||||
import { GameSnapshotAggregate, SnapshotTrigger } from '../../Domain/Game/GameSnapshotAggregate';
|
||||
|
||||
export class GameSnapshotRepository implements IGameSnapshotRepository {
|
||||
private repository: Repository<GameSnapshotAggregate>;
|
||||
|
||||
constructor() {
|
||||
this.repository = AppDataSource.getRepository(GameSnapshotAggregate);
|
||||
}
|
||||
|
||||
async save(snapshot: GameSnapshotAggregate): Promise<GameSnapshotAggregate> {
|
||||
return await this.repository.save(snapshot);
|
||||
}
|
||||
|
||||
async findLatestByGameId(gameId: string): Promise<GameSnapshotAggregate | null> {
|
||||
return await this.repository.findOne({
|
||||
where: { gameid: gameId },
|
||||
order: { createdat: 'DESC' }
|
||||
});
|
||||
}
|
||||
|
||||
async findByGameId(gameId: string): Promise<GameSnapshotAggregate[]> {
|
||||
return await this.repository.find({
|
||||
where: { gameid: gameId },
|
||||
order: { turnNumber: 'ASC', createdat: 'ASC' }
|
||||
});
|
||||
}
|
||||
|
||||
async findByGameAndTrigger(gameId: string, trigger: SnapshotTrigger): Promise<GameSnapshotAggregate[]> {
|
||||
return await this.repository.find({
|
||||
where: {
|
||||
gameid: gameId,
|
||||
trigger: trigger
|
||||
},
|
||||
order: { createdat: 'DESC' }
|
||||
});
|
||||
}
|
||||
|
||||
async findByGameAndTurn(gameId: string, turnNumber: number): Promise<GameSnapshotAggregate | null> {
|
||||
return await this.repository.findOne({
|
||||
where: {
|
||||
gameid: gameId,
|
||||
turnNumber: turnNumber
|
||||
},
|
||||
order: { createdat: 'DESC' }
|
||||
});
|
||||
}
|
||||
|
||||
async deleteOldSnapshots(gameId: string, keepCount: number): Promise<void> {
|
||||
const snapshots = await this.repository.find({
|
||||
where: { gameid: gameId },
|
||||
order: { createdat: 'DESC' },
|
||||
select: ['id', 'createdat']
|
||||
});
|
||||
|
||||
if (snapshots.length > keepCount) {
|
||||
const idsToDelete = snapshots
|
||||
.slice(keepCount)
|
||||
.map(s => s.id);
|
||||
|
||||
if (idsToDelete.length > 0) {
|
||||
await this.repository.delete(idsToDelete);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async deleteByGameId(gameId: string): Promise<void> {
|
||||
await this.repository.delete({ gameid: gameId });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
import { Repository } from 'typeorm';
|
||||
import { AppDataSource } from '../ormconfig';
|
||||
import { ITurnHistoryRepository } from '../../Domain/IRepository/ITurnHistoryRepository';
|
||||
import { TurnHistoryAggregate } from '../../Domain/Game/TurnHistoryAggregate';
|
||||
|
||||
export class TurnHistoryRepository implements ITurnHistoryRepository {
|
||||
private repository: Repository<TurnHistoryAggregate>;
|
||||
|
||||
constructor() {
|
||||
this.repository = AppDataSource.getRepository(TurnHistoryAggregate);
|
||||
}
|
||||
|
||||
async save(turnHistory: TurnHistoryAggregate): Promise<TurnHistoryAggregate> {
|
||||
return await this.repository.save(turnHistory);
|
||||
}
|
||||
|
||||
async findByGameId(gameId: string): Promise<TurnHistoryAggregate[]> {
|
||||
return await this.repository.find({
|
||||
where: { gameid: gameId },
|
||||
order: { turnNumber: 'ASC', createdat: 'ASC' }
|
||||
});
|
||||
}
|
||||
|
||||
async findByGameAndPlayer(gameId: string, playerId: string): Promise<TurnHistoryAggregate[]> {
|
||||
return await this.repository.find({
|
||||
where: {
|
||||
gameid: gameId,
|
||||
playerid: playerId
|
||||
},
|
||||
order: { turnNumber: 'ASC', createdat: 'ASC' }
|
||||
});
|
||||
}
|
||||
|
||||
async findLastNTurns(gameId: string, limit: number): Promise<TurnHistoryAggregate[]> {
|
||||
return await this.repository.find({
|
||||
where: { gameid: gameId },
|
||||
order: { turnNumber: 'DESC', createdat: 'DESC' },
|
||||
take: limit
|
||||
});
|
||||
}
|
||||
|
||||
async countTurnsByGame(gameId: string): Promise<number> {
|
||||
return await this.repository.count({
|
||||
where: { gameid: gameId }
|
||||
});
|
||||
}
|
||||
|
||||
async deleteByGameId(gameId: string): Promise<void> {
|
||||
await this.repository.delete({ gameid: gameId });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user