Add find by endpoints

This commit is contained in:
Anthony Mineo 2020-08-23 10:35:50 -04:00
parent 3b5f41f933
commit 2d90f9dde8
2 changed files with 30 additions and 5 deletions

View file

@ -10,13 +10,19 @@ export class GamesController {
// /games // /games
@Get() @Get()
findAll(@Query() paginationQuery: PaginationQueryDto) { findAll(@Query() paginationQuery: PaginationQueryDto) {
//const { limit, offset } = paginationQuery; const { limit = 10, offset = 0 } = paginationQuery;
return this.gameService.findAll({ limit: 100, offset: 0 }); return this.gameService.findAll({ limit, offset });
}
// /gametype/:gameId
@Get('gametype/:gametype')
findByType(@Param('gametype') gametype: string) {
return this.gameService.findByType(gametype);
} }
// /game/:gameId // /game/:gameId
@Get(':gameId') @Get(':gameId')
findOne(@Param('gameId') gameId: string) { findOne(@Param('gameId') gameId: string) {
return ''; return this.gameService.findOne(gameId);
} }
} }

View file

@ -1,4 +1,4 @@
import { Injectable } from '@nestjs/common'; import { Injectable, NotFoundException } from '@nestjs/common';
import { ConfigService } from '@nestjs/config'; import { ConfigService } from '@nestjs/config';
import { Connection, Repository } from 'typeorm'; import { Connection, Repository } from 'typeorm';
@ -18,9 +18,28 @@ export class GamesService {
const { limit, offset } = paginationQuery; const { limit, offset } = paginationQuery;
const games = await this.gamesRepository.find({ const games = await this.gamesRepository.find({
skip: offset, skip: offset,
take: limit take: limit,
order: {
gameId: 'DESC'
}
}); });
return games; return games;
} }
async findByType(gametype: string) {
const game = await this.gamesRepository.find({ where: { gametype: gametype } });
if (!game) {
throw new NotFoundException(`Game Type: ${gametype} not found`);
}
return game;
}
async findOne(gameId: string) {
const game = await this.gamesRepository.findOne(gameId);
if (!game) {
throw new NotFoundException(`Game ID: ${gameId} not found`);
}
return game;
}
} }