ptitspas-ynov-back-sosso/src/routes/enfants/enfants.controller.ts

70 lines
3.3 KiB
TypeScript

import { Body, Controller, Delete, Get, Param, ParseUUIDPipe, Patch, Post, UseGuards } from '@nestjs/common';
import { ApiBearerAuth, ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
import { RolesGuard } from 'src/common/guards/roles.guard';
import { EnfantsService } from './enfants.service';
import { Roles } from 'src/common/decorators/roles.decorator';
import { RoleType, Users } from 'src/entities/users.entity';
import { CreateEnfantsDto } from './dto/create_enfants.dto';
import { EnfantResponseDto } from './dto/enfants_response.dto';
import { mapEnfantsToResponseDto, mapEnfantToResponseDto } from './enfants.mapper';
import { AuthGuard } from 'src/common/guards/auth.guard';
import { User } from 'src/common/decorators/user.decorator';
@ApiBearerAuth('access-token')
@ApiTags('Enfants')
@UseGuards(AuthGuard, RolesGuard)
@Controller('enfants')
export class EnfantsController {
constructor(private readonly enfantsService: EnfantsService) { }
@Post()
@ApiOperation({ summary: 'Inscrire un enfant' })
@ApiResponse({ status: 201, type: EnfantResponseDto, description: 'L\'enfant a été inscrit avec succès.' })
@Roles(RoleType.PARENT, RoleType.SUPER_ADMIN, RoleType.GESTIONNAIRE)
async create(
@Body() dto: CreateEnfantsDto,
@User() currentUser: Users, // <-- ton décorateur
): Promise<EnfantResponseDto> {
const enfant = await this.enfantsService.create(dto, currentUser);
return mapEnfantToResponseDto(enfant);
}
@Get()
@ApiOperation({ summary: 'Récupérer tous les enfants avec leurs liens parents' })
@ApiResponse({ status: 200, type: [EnfantResponseDto], description: 'Liste de tous les enfants avec leurs liens parents.' })
@Roles(RoleType.GESTIONNAIRE, RoleType.SUPER_ADMIN)
async findAll(): Promise<EnfantResponseDto[]> {
const enfants = await this.enfantsService.findAll();
return mapEnfantsToResponseDto(enfants);
}
@Get(':id')
@ApiOperation({ summary: 'Récupérer un enfant par son ID' })
@ApiResponse({ status: 200, type: EnfantResponseDto, description: 'Détails de l\'enfant avec ses liens parents.' })
@Roles(RoleType.GESTIONNAIRE, RoleType.SUPER_ADMIN, RoleType.PARENT)
async findOne(@Param('id', new ParseUUIDPipe()) id: string): Promise<EnfantResponseDto> {
const enfant = await this.enfantsService.findOne(id);
return mapEnfantToResponseDto(enfant);
}
@Patch(':id')
@ApiOperation({ summary: 'Mettre à jour un enfant' })
@ApiResponse({ status: 200, type: EnfantResponseDto, description: 'L\'enfant a été mis à jour avec succès.' })
@Roles(RoleType.GESTIONNAIRE, RoleType.SUPER_ADMIN)
async update(
@Param('id', new ParseUUIDPipe()) id: string,
@Body() dto: Partial<CreateEnfantsDto>,
): Promise<EnfantResponseDto> {
const enfant = await this.enfantsService.update(id, dto);
return mapEnfantToResponseDto(enfant);
}
@Delete(':id')
@ApiOperation({ summary: 'Supprimer un enfant' })
@ApiResponse({ status: 204, description: 'L\'enfant a été supprimé avec succès.' })
@Roles(RoleType.GESTIONNAIRE, RoleType.SUPER_ADMIN)
async remove(@Param('id', new ParseUUIDPipe()) id: string): Promise<void> {
return this.enfantsService.remove(id);
}
}