Co-authored-by: Julien Martin <julien.martin@ptits-pas.fr> Co-committed-by: Julien Martin <julien.martin@ptits-pas.fr>
102 lines
2.8 KiB
TypeScript
102 lines
2.8 KiB
TypeScript
import {
|
|
Body,
|
|
Controller,
|
|
Delete,
|
|
Get,
|
|
Param,
|
|
ParseUUIDPipe,
|
|
Patch,
|
|
Post,
|
|
UseGuards,
|
|
UseInterceptors,
|
|
UploadedFile,
|
|
} from '@nestjs/common';
|
|
import { FileInterceptor } from '@nestjs/platform-express';
|
|
import { ApiBearerAuth, ApiTags, ApiConsumes } from '@nestjs/swagger';
|
|
import { diskStorage } from 'multer';
|
|
import { extname } from 'path';
|
|
import { EnfantsService } from './enfants.service';
|
|
import { CreateEnfantsDto } from './dto/create_enfants.dto';
|
|
import { UpdateEnfantsDto } from './dto/update_enfants.dto';
|
|
import { RoleType, Users } from 'src/entities/users.entity';
|
|
import { User } from 'src/common/decorators/user.decorator';
|
|
import { AuthGuard } from 'src/common/guards/auth.guard';
|
|
import { Roles } from 'src/common/decorators/roles.decorator';
|
|
import { RolesGuard } from 'src/common/guards/roles.guard';
|
|
|
|
@ApiBearerAuth('access-token')
|
|
@ApiTags('Enfants')
|
|
@UseGuards(AuthGuard, RolesGuard)
|
|
@Controller('enfants')
|
|
export class EnfantsController {
|
|
constructor(private readonly enfantsService: EnfantsService) { }
|
|
|
|
@Roles(RoleType.PARENT)
|
|
@Post()
|
|
@ApiConsumes('multipart/form-data')
|
|
@UseInterceptors(
|
|
FileInterceptor('photo', {
|
|
storage: diskStorage({
|
|
destination: './uploads/photos',
|
|
filename: (req, file, cb) => {
|
|
const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1e9);
|
|
const ext = extname(file.originalname);
|
|
cb(null, `enfant-${uniqueSuffix}${ext}`);
|
|
},
|
|
}),
|
|
fileFilter: (req, file, cb) => {
|
|
if (!file.mimetype.match(/\/(jpg|jpeg|png|gif)$/)) {
|
|
return cb(new Error('Seules les images sont autorisées'), false);
|
|
}
|
|
cb(null, true);
|
|
},
|
|
limits: {
|
|
fileSize: 5 * 1024 * 1024,
|
|
},
|
|
}),
|
|
)
|
|
create(
|
|
@Body() dto: CreateEnfantsDto,
|
|
@UploadedFile() photo: Express.Multer.File,
|
|
@User() currentUser: Users,
|
|
) {
|
|
return this.enfantsService.create(dto, currentUser, photo);
|
|
}
|
|
|
|
@Roles(RoleType.ADMINISTRATEUR, RoleType.GESTIONNAIRE, RoleType.SUPER_ADMIN)
|
|
@Get()
|
|
findAll() {
|
|
return this.enfantsService.findAll();
|
|
}
|
|
|
|
@Roles(
|
|
RoleType.PARENT,
|
|
RoleType.ADMINISTRATEUR,
|
|
RoleType.SUPER_ADMIN,
|
|
RoleType.GESTIONNAIRE
|
|
)
|
|
@Get(':id')
|
|
findOne(
|
|
@Param('id', new ParseUUIDPipe()) id: string,
|
|
@User() currentUser: Users
|
|
) {
|
|
return this.enfantsService.findOne(id, currentUser);
|
|
}
|
|
|
|
@Roles(RoleType.ADMINISTRATEUR, RoleType.SUPER_ADMIN, RoleType.PARENT)
|
|
@Patch(':id')
|
|
update(
|
|
@Param('id', new ParseUUIDPipe()) id: string,
|
|
@Body() dto: UpdateEnfantsDto,
|
|
@User() currentUser: Users,
|
|
) {
|
|
return this.enfantsService.update(id, dto, currentUser);
|
|
}
|
|
|
|
@Roles(RoleType.SUPER_ADMIN)
|
|
@Delete(':id')
|
|
remove(@Param('id', new ParseUUIDPipe()) id: string) {
|
|
return this.enfantsService.remove(id);
|
|
}
|
|
}
|