import { Controller, Get, Post, Param, Delete, UseGuards, Session, HttpException, HttpStatus, } from '@nestjs/common'; import { GamesService } from './games.service'; import { AuthGuard } from './guards/auth.guard'; import { ApiOperation, ApiResponse } from '@nestjs/swagger'; import { DatabaseObjectDto } from './dto/database-object.dto'; @Controller('games') export class GamesController { constructor(private readonly gamesService: GamesService) {} @Post() @ApiOperation({ summary: 'Create game' }) @ApiResponse({ status: 201, description: 'Game successfully created.', type: DatabaseObjectDto, }) @ApiResponse({ status: 400, description: 'You can only create one game at a time', }) @ApiResponse({ status: 403, description: 'Forbidden.' }) @UseGuards(AuthGuard) async create(@Session() session: Record) { const games = await this.gamesService.findByOwner(session.user._id); if (games.length > 0) { throw new HttpException( 'You can only create one game at a time', HttpStatus.BAD_REQUEST, ); } return this.gamesService.create(session.user); } @Get() @ApiOperation({ summary: 'List all games' }) findAll() { const games = this.gamesService.findAll(); return games; } @Get(':id') findOne(@Param('id') id: string) { return this.gamesService.findById(id); } // @Patch(':id') // update(@Param('id') id: string, @Body() updateGameDto: UpdateGameDto) { // return this.gamesService.update(+id, updateGameDto); // } @Delete(':id') remove(@Param('id') id: string) { return this.gamesService.remove(+id); } }