traintrape-moi/server/src/money-updates/money-updates.controller.ts

91 lines
3.4 KiB
TypeScript

import { Controller, Get, Post, Body, Patch, Param, Delete, HttpCode, UseGuards, Query, NotFoundException, ParseIntPipe, Req } from '@nestjs/common'
import { MoneyUpdatesService } from './money-updates.service'
import { CreateMoneyUpdateDto } from './dto/create-money-update.dto'
import { UpdateMoneyUpdateDto } from './dto/update-money-update.dto'
import { AuthenticatedRequest, JwtAuthGuard } from 'src/auth/jwt-auth.guard'
import { ApiBearerAuth, ApiNoContentResponse } from '@nestjs/swagger'
import { MoneyUpdateEntity } from './entities/money-update.entity'
import { ApiOkResponsePaginated, paginateOutput } from 'src/common/utils/pagination.utils'
import { QueryPaginationDto } from 'src/common/dto/pagination-query.dto'
import { PlayerFilterDto } from 'src/common/dto/player_filter.dto'
import { PaginateOutputDto } from 'src/common/dto/pagination-output.dto'
@Controller('money-updates')
export class MoneyUpdatesController {
constructor(private readonly moneyUpdatesService: MoneyUpdatesService) {}
/**
* Création d'une modification de solde
*
* @throws {400} Erreurs dans le formulaire de création
* @throws {401} Non authentifié⋅e
*/
@Post()
@HttpCode(201)
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
async create(@Req() request: AuthenticatedRequest, @Body() createMoneyUpdateDto: CreateMoneyUpdateDto): Promise<MoneyUpdateEntity> {
const moneyUpdate = await this.moneyUpdatesService.create(request.user, createMoneyUpdateDto)
return new MoneyUpdateEntity(moneyUpdate)
}
/**
* Recherche de modifications de solde
*
* @throws {401} Non authentifié⋅e
*/
@Get()
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
@ApiOkResponsePaginated(MoneyUpdateEntity)
async findAll(@Query() queryPagination: QueryPaginationDto, @Query() playerFilter: PlayerFilterDto): Promise<PaginateOutputDto<MoneyUpdateEntity>> {
const [trains, total] = await this.moneyUpdatesService.findAll(queryPagination, playerFilter)
return paginateOutput<MoneyUpdateEntity>(trains.map(train => new MoneyUpdateEntity(train)), total, queryPagination)
}
/**
* Recherche d'une modification de solde
*
* @throws {401} Non authentifié⋅e
* @throws {404} Modification de solde non trouvée
*/
@Get(':id')
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
async findOne(@Param('id', ParseIntPipe) id: number): Promise<MoneyUpdateEntity> {
const train = await this.moneyUpdatesService.findOne(id)
if (!train)
throw new NotFoundException(`Trajet en train inexistant avec l'identifiant ${id}`)
return new MoneyUpdateEntity(train)
}
/**
* Modification d'une modification de solde
*
* @throws {400} Erreurs dans le formulaire de modification
* @throws {401} Non authentifié⋅e
* @throws {404} Modification de solde non trouvée
*/
@Patch(':id')
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
async update(@Param('id', ParseIntPipe) id: number, @Body() updateMoneyUpdateDto: UpdateMoneyUpdateDto) {
return await this.moneyUpdatesService.update(id, updateMoneyUpdateDto)
}
/**
* Suppression d'une modification de solde
*
* @throws {401} Non authentifié⋅e
* @throws {404} Modification de solde non trouvée
*/
@Delete(':id')
@HttpCode(204)
@UseGuards(JwtAuthGuard)
@ApiNoContentResponse({ description: "La modification de solde a bien été supprimée" })
@ApiBearerAuth()
async remove(@Param('id', ParseIntPipe) id: number) {
await this.moneyUpdatesService.remove(id)
}
}