110 lines
3.9 KiB
TypeScript
110 lines
3.9 KiB
TypeScript
import { Controller, Get, Post, Body, Patch, Param, Delete, HttpCode, UseGuards, Query, NotFoundException, Req } from '@nestjs/common'
|
|
import { TrainsService } from './trains.service'
|
|
import { CreateTrainDto } from './dto/create-train.dto'
|
|
import { UpdateTrainDto } from './dto/update-train.dto'
|
|
import { TrainEntity } from './entities/train.entity'
|
|
import { AuthenticatedRequest, JwtAuthGuard } from 'src/auth/jwt-auth.guard'
|
|
import { ApiBearerAuth, ApiCreatedResponse, ApiNoContentResponse, ApiUnauthorizedResponse } from '@nestjs/swagger'
|
|
import { ApiOkResponsePaginated, paginateOutput } from 'src/common/utils/pagination.utils'
|
|
import { QueryPaginationDto } from 'src/common/dto/pagination-query.dto'
|
|
import { PaginateOutputDto } from 'src/common/dto/pagination-output.dto'
|
|
import { ImportTrainDto } from './dto/import-train.dto'
|
|
import { PlayerFilterDto } from 'src/common/dto/player_filter.dto'
|
|
|
|
@Controller('trains')
|
|
export class TrainsController {
|
|
constructor(private readonly trainsService: TrainsService) {}
|
|
|
|
/**
|
|
* Création d'un trajet en train manuellement
|
|
*
|
|
* @throws {400} Erreurs dans le formulaire de création
|
|
* @throws {401} Non authentifié⋅e
|
|
*/
|
|
@Post()
|
|
@HttpCode(201)
|
|
@UseGuards(JwtAuthGuard)
|
|
@ApiBearerAuth()
|
|
@ApiCreatedResponse({ type: TrainEntity, description: "Trajet en train créé avec succès" })
|
|
@ApiUnauthorizedResponse({ description: "Non authentifié⋅e" })
|
|
async create(@Body() createTrainDto: CreateTrainDto): Promise<TrainEntity> {
|
|
const train = await this.trainsService.create(createTrainDto)
|
|
return new TrainEntity(train)
|
|
}
|
|
|
|
/**
|
|
* Recherche de trajets en train
|
|
*
|
|
* @throws {401} Non authentifié⋅e
|
|
*/
|
|
@Get()
|
|
@UseGuards(JwtAuthGuard)
|
|
@ApiBearerAuth()
|
|
@ApiOkResponsePaginated(TrainEntity)
|
|
async findAll(@Query() queryPagination: QueryPaginationDto, @Query() { playerId }: PlayerFilterDto): Promise<PaginateOutputDto<TrainEntity>> {
|
|
const [trains, total] = await this.trainsService.findAll(queryPagination, { playerId })
|
|
return paginateOutput<TrainEntity>(trains.map(train => new TrainEntity(train)), total, queryPagination)
|
|
}
|
|
|
|
/**
|
|
* Recherche d'un trajet en train
|
|
*
|
|
* @throws {401} Non authentifié⋅e
|
|
* @throws {404} Trajet en train non trouvé
|
|
*/
|
|
@Get(':id')
|
|
@UseGuards(JwtAuthGuard)
|
|
@ApiBearerAuth()
|
|
async findOne(@Param('id') id: string): Promise<TrainEntity> {
|
|
const train = await this.trainsService.findOne(id)
|
|
if (!train)
|
|
throw new NotFoundException(`Trajet en train inexistant avec l'identifiant ${id}`)
|
|
return new TrainEntity(train)
|
|
}
|
|
|
|
/**
|
|
* Modification d'un trajet en train manuellement
|
|
*
|
|
* @throws {400} Erreurs dans le formulaire de création
|
|
* @throws {401} Non authentifié⋅e
|
|
* @throws {404} Trajet en train non trouvé
|
|
*/
|
|
@Patch(':id')
|
|
@UseGuards(JwtAuthGuard)
|
|
@ApiBearerAuth()
|
|
async update(@Param('id') id: string, @Body() updateTrainDto: UpdateTrainDto) {
|
|
return await this.trainsService.update(id, updateTrainDto)
|
|
}
|
|
|
|
/**
|
|
* Suppression d'un trajet en train
|
|
*
|
|
* @throws {401} Non authentifié⋅e
|
|
* @throws {404} Trajet en train non trouvé
|
|
*/
|
|
@Delete(':id')
|
|
@HttpCode(204)
|
|
@UseGuards(JwtAuthGuard)
|
|
@ApiBearerAuth()
|
|
@ApiNoContentResponse({ description: "Le trajet en train a bien été supprimé" })
|
|
async remove(@Param('id') id: string) {
|
|
await this.trainsService.remove(id)
|
|
}
|
|
|
|
/**
|
|
* Importation d'un trajet en train à partir de Rail Planner
|
|
*
|
|
* @throws {401} Non authentifié⋅e
|
|
* @throws {409} Le voyage Interrail est déjà importé
|
|
* @throws {422} Le voyage Interrail à importer contient plusieurs trajets ou plusieurs trains
|
|
*/
|
|
@Post("/import")
|
|
@HttpCode(201)
|
|
@UseGuards(JwtAuthGuard)
|
|
@ApiBearerAuth()
|
|
async import(@Req() request: AuthenticatedRequest, @Body() importTrainDto: ImportTrainDto): Promise<TrainEntity> {
|
|
const train = await this.trainsService.import(request.user, importTrainDto)
|
|
return new TrainEntity(train)
|
|
}
|
|
}
|