Setup return games with details endpoint

This commit is contained in:
Anthony Mineo 2020-08-29 21:05:20 -04:00
parent f240ccd306
commit 96e15be86d
4 changed files with 39 additions and 5 deletions

View file

@ -13,6 +13,7 @@ import { Players } from '../players/entities/Players';
@Module({ @Module({
imports: [ TypeOrmModule.forFeature([ Games, GameDetail, Players ]), ConfigModule ], imports: [ TypeOrmModule.forFeature([ Games, GameDetail, Players ]), ConfigModule ],
controllers: [ GameController ], controllers: [ GameController ],
providers: [ GameService ] providers: [ GameService ],
exports: [ GameService ]
}) })
export class GameModule {} export class GameModule {}

View file

@ -6,20 +6,29 @@ import { PaginationQueryDto } from '../common/dto/pagination-query.dto';
@Controller('games') @Controller('games')
export class GamesController { export class GamesController {
constructor(private readonly gameService: GamesService) {} constructor(private readonly gamesService: GamesService) {}
// /games // /games
@Get() @Get()
@ApiOperation({ tags: [ 'Game' ], summary: 'Return the latest games' }) @ApiOperation({ tags: [ 'Game' ], summary: 'Return the latest games' })
findAll(@Query() paginationQuery: PaginationQueryDto) { findAll(@Query() paginationQuery: PaginationQueryDto) {
const { limit = 10, offset = 0 } = paginationQuery; const { limit = 10, offset = 0 } = paginationQuery;
return this.gameService.findAll({ limit, offset }); return this.gamesService.findAll({ limit, offset });
}
// /games/summary
@Get('summary')
@ApiOperation({ tags: [ 'Game' ], summary: 'Return the latest games with summary' })
findAllWithSummary(@Query() paginationQuery: PaginationQueryDto) {
const { limit = 10, offset = 0 } = paginationQuery;
return this.gamesService.findAllWithSummary({ limit, offset });
} }
// /gametype/:gametype // /gametype/:gametype
@Get('gametype/:gametype') @Get('gametype/:gametype')
@ApiOperation({ tags: [ 'Game' ], summary: 'Return the latest games by game type' }) @ApiOperation({ tags: [ 'Game' ], summary: 'Return the latest games by game type' })
findByType(@Param('gametype') gametype: string) { findByType(@Param('gametype') gametype: string) {
return this.gameService.findByType(gametype); return this.gamesService.findByType(gametype);
} }
} }

View file

@ -7,9 +7,10 @@ import { GamesService } from './games.service';
import { Games } from './entities/Games'; import { Games } from './entities/Games';
import { GameDetail } from '../game/entities/GameDetail'; import { GameDetail } from '../game/entities/GameDetail';
import { GameModule } from '../game/game.module';
@Module({ @Module({
imports: [ TypeOrmModule.forFeature([ Games, GameDetail ]), ConfigModule ], imports: [ TypeOrmModule.forFeature([ Games, GameDetail ]), ConfigModule, GameModule ],
controllers: [ GamesController ], controllers: [ GamesController ],
providers: [ GamesService ] providers: [ GamesService ]
}) })

View file

@ -4,6 +4,7 @@ import { InjectRepository } from '@nestjs/typeorm';
import { Connection, Repository } from 'typeorm'; import { Connection, Repository } from 'typeorm';
import { Games } from './entities/Games'; import { Games } from './entities/Games';
import { GameService } from '../game/game.service';
import { PaginationQueryDto } from '../common/dto/pagination-query.dto'; import { PaginationQueryDto } from '../common/dto/pagination-query.dto';
@Injectable() @Injectable()
@ -11,6 +12,7 @@ export class GamesService {
constructor( constructor(
private readonly connection: Connection, private readonly connection: Connection,
private readonly configService: ConfigService, private readonly configService: ConfigService,
private readonly gameService: GameService,
@InjectRepository(Games) private readonly gamesRepository: Repository<Games> @InjectRepository(Games) private readonly gamesRepository: Repository<Games>
) {} ) {}
@ -27,6 +29,27 @@ export class GamesService {
return games; return games;
} }
async findAllWithSummary(paginationQuery: PaginationQueryDto) {
const { limit, offset } = paginationQuery;
const games = await this.gamesRepository.find({
skip: offset,
take: limit,
order: {
gameId: 'DESC'
}
});
const withSummary = [];
for (const game of games) {
const summary = await this.gameService.findOne(game.gameId);
withSummary.push(summary);
}
// Game findOne service needs to bubble up the game details as parent object and set players below it
return withSummary;
}
async findByType(gametype: string) { async findByType(gametype: string) {
const game = await this.gamesRepository.find({ const game = await this.gamesRepository.find({
where: { gametype: gametype }, where: { gametype: gametype },