forked from Ynov/ptitspas-ynov-back
[BACK] Ajout/MAJ des routes enfants (CRUD)
This commit is contained in:
parent
7c06fc9c00
commit
a8ca887f3d
7
.gitignore
vendored
7
.gitignore
vendored
@ -55,3 +55,10 @@ pids
|
||||
# Diagnostic reports (https://nodejs.org/api/report.html)
|
||||
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
|
||||
.env
|
||||
|
||||
|
||||
# Tests bdd
|
||||
.vscode/
|
||||
BDD.sql
|
||||
migrations/
|
||||
src/seed/
|
||||
|
||||
38
src/routes/enfants/dto/enfants_response.dto.ts
Normal file
38
src/routes/enfants/dto/enfants_response.dto.ts
Normal file
@ -0,0 +1,38 @@
|
||||
import { ApiProperty } from "@nestjs/swagger";
|
||||
import { GenreType, StatutEnfantType } from "src/entities/children.entity";
|
||||
|
||||
export class EnfantResponseDto {
|
||||
@ApiProperty({ example: 'UUID-enfant' })
|
||||
id: string;
|
||||
|
||||
@ApiProperty({ enum: StatutEnfantType })
|
||||
status: StatutEnfantType;
|
||||
|
||||
@ApiProperty({ example: 'Georges', required: false })
|
||||
first_name?: string;
|
||||
|
||||
@ApiProperty({ example: 'Dupont', required: false })
|
||||
last_name?: string;
|
||||
|
||||
@ApiProperty({ enum: GenreType, required: false })
|
||||
gender?: GenreType;
|
||||
|
||||
@ApiProperty({ example: '2018-06-24', required: false })
|
||||
birth_date?: string;
|
||||
|
||||
@ApiProperty({ example: '2024-12-24', required: false })
|
||||
due_date?: string;
|
||||
|
||||
@ApiProperty({ example: 'https://monimage.com/photo.jpg', required: false })
|
||||
photo_url?: string;
|
||||
|
||||
@ApiProperty({ example: false })
|
||||
consent_photo: boolean;
|
||||
|
||||
@ApiProperty({ example: false })
|
||||
is_multiple: boolean;
|
||||
|
||||
@ApiProperty({ example: 'UUID-parent' })
|
||||
parent_id: string;
|
||||
|
||||
}
|
||||
@ -6,6 +6,8 @@ import { Roles } from 'src/common/decorators/roles.decorator';
|
||||
import { RoleType } from 'src/entities/users.entity';
|
||||
import { Children } from 'src/entities/children.entity';
|
||||
import { CreateEnfantsDto } from './dto/create_enfants.dto';
|
||||
import { EnfantResponseDto } from './dto/enfants_response.dto';
|
||||
import { mapEnfantsToResponseDto, mapEnfantToResponseDto } from './enfants.mapper';
|
||||
|
||||
@ApiBearerAuth()
|
||||
@ApiTags('Enfants')
|
||||
@ -13,43 +15,55 @@ import { CreateEnfantsDto } from './dto/create_enfants.dto';
|
||||
@Controller('enfants')
|
||||
export class EnfantsController {
|
||||
constructor(private readonly enfantsService: EnfantsService) { }
|
||||
|
||||
// Inscrire un enfant
|
||||
@Post()
|
||||
@ApiOperation({ summary: 'Inscrire un enfant' })
|
||||
@ApiResponse({ status: 201, type: Children, description: 'L\'enfant a été inscrit avec succès.' })
|
||||
@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): Promise<Children> {
|
||||
return this.enfantsService.create(dto);
|
||||
}
|
||||
|
||||
async create(@Body() dto: CreateEnfantsDto): Promise<EnfantResponseDto> {
|
||||
const enfant = await this.enfantsService.create(dto);
|
||||
|
||||
// Mapper l'entité enfant vers le DTO de réponse
|
||||
return mapEnfantToResponseDto(enfant);
|
||||
}
|
||||
|
||||
// Récupérer tous les enfants avec leurs liens parents
|
||||
@ApiOperation({ summary: 'Récupérer tous les enfants avec leurs liens parents' })
|
||||
@ApiResponse({ status: 200, type: [Children], description: 'Liste de tous les enfants avec leurs liens parents.' })
|
||||
@ApiResponse({ status: 200, type: [EnfantResponseDto], description: 'Liste de tous les enfants avec leurs liens parents.' })
|
||||
@Get()
|
||||
@Roles(RoleType.GESTIONNAIRE, RoleType.SUPER_ADMIN)
|
||||
async findAll(): Promise<Children[]> {
|
||||
return this.enfantsService.findAll();
|
||||
async findAll(): Promise<EnfantResponseDto[]> {
|
||||
const enfants = await this.enfantsService.findAll();
|
||||
|
||||
// Mapper les entités enfants vers les DTO de réponse
|
||||
return mapEnfantsToResponseDto(enfants);
|
||||
}
|
||||
|
||||
// Récupérer un enfant par son ID
|
||||
@Get(':id')
|
||||
@ApiOperation({ summary: 'Récupérer un enfant par son ID' })
|
||||
@ApiResponse({ status: 200, type: Children, description: 'Détails de l\'enfant avec ses liens parents.' })
|
||||
@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<Children> {
|
||||
return this.enfantsService.findOne(id);
|
||||
async findOne(@Param('id', new ParseUUIDPipe()) id: string): Promise<EnfantResponseDto> {
|
||||
const enfant = await this.enfantsService.findOne(id);
|
||||
|
||||
// Mapper l'entité enfant vers le DTO de réponse
|
||||
return mapEnfantToResponseDto(enfant);
|
||||
}
|
||||
|
||||
// Mettre à jour un enfant
|
||||
@Patch(':id')
|
||||
@ApiOperation({ summary: 'Mettre à jour un enfant' })
|
||||
@ApiResponse({ status: 200, type: Children, description: 'L\'enfant a été mis à jour avec succès.' })
|
||||
@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<Children> {
|
||||
return this.enfantsService.update(id, dto);
|
||||
): Promise<EnfantResponseDto> {
|
||||
const enfant = await this.enfantsService.update(id, dto);
|
||||
|
||||
// Mapper l'entité enfant vers le DTO de réponse
|
||||
return mapEnfantToResponseDto(enfant);
|
||||
}
|
||||
|
||||
// Supprimer un enfant
|
||||
|
||||
24
src/routes/enfants/enfants.mapper.ts
Normal file
24
src/routes/enfants/enfants.mapper.ts
Normal file
@ -0,0 +1,24 @@
|
||||
import { Children } from "src/entities/children.entity";
|
||||
import { EnfantResponseDto } from "./dto/enfants_response.dto";
|
||||
|
||||
// Fonction pour mapper une entité Children vers EnfantResponseDto
|
||||
export function mapEnfantToResponseDto(enfant: Children): EnfantResponseDto {
|
||||
return {
|
||||
id: enfant.id,
|
||||
status: enfant.status,
|
||||
first_name: enfant.first_name,
|
||||
last_name: enfant.last_name,
|
||||
gender: enfant.gender,
|
||||
birth_date: enfant.birth_date?.toISOString(),
|
||||
due_date: enfant.due_date?.toISOString(),
|
||||
photo_url: enfant.photo_url,
|
||||
consent_photo: enfant.consent_photo,
|
||||
is_multiple: enfant.is_multiple,
|
||||
parent_id: enfant.parentLinks?.[0]?.parentId ?? null,
|
||||
};
|
||||
}
|
||||
|
||||
export function mapEnfantsToResponseDto(enfants: Children[]): EnfantResponseDto[] {
|
||||
return enfants.map(mapEnfantToResponseDto);
|
||||
};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user