52 lines
1.7 KiB
TypeScript
52 lines
1.7 KiB
TypeScript
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 });
|
|
}
|
|
}
|