feat(users): add Swagger decorators for API documentation in UsersController

This commit is contained in:
Mathis HERRIOT 2025-05-17 00:12:31 +02:00
parent 818a92f18c
commit b7d899e66e
No known key found for this signature in database
GPG Key ID: E7EB4A211D8D4907

View File

@ -9,10 +9,12 @@ import {
HttpCode, HttpCode,
HttpStatus, HttpStatus,
} from '@nestjs/common'; } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiParam } from '@nestjs/swagger';
import { UsersService } from '../services/users.service'; import { UsersService } from '../services/users.service';
import { CreateUserDto } from '../dto/create-user.dto'; import { CreateUserDto } from '../dto/create-user.dto';
import { UpdateUserDto } from '../dto/update-user.dto'; import { UpdateUserDto } from '../dto/update-user.dto';
@ApiTags('users')
@Controller('users') @Controller('users')
export class UsersController { export class UsersController {
constructor(private readonly usersService: UsersService) {} constructor(private readonly usersService: UsersService) {}
@ -20,6 +22,9 @@ export class UsersController {
/** /**
* Create a new user * Create a new user
*/ */
@ApiOperation({ summary: 'Create a new user' })
@ApiResponse({ status: 201, description: 'The user has been successfully created.' })
@ApiResponse({ status: 400, description: 'Bad request.' })
@Post() @Post()
@HttpCode(HttpStatus.CREATED) @HttpCode(HttpStatus.CREATED)
create(@Body() createUserDto: CreateUserDto) { create(@Body() createUserDto: CreateUserDto) {
@ -29,6 +34,8 @@ export class UsersController {
/** /**
* Get all users * Get all users
*/ */
@ApiOperation({ summary: 'Get all users' })
@ApiResponse({ status: 200, description: 'Return all users.' })
@Get() @Get()
findAll() { findAll() {
return this.usersService.findAll(); return this.usersService.findAll();
@ -37,6 +44,10 @@ export class UsersController {
/** /**
* Get a user by ID * Get a user by ID
*/ */
@ApiOperation({ summary: 'Get a user by ID' })
@ApiResponse({ status: 200, description: 'Return the user.' })
@ApiResponse({ status: 404, description: 'User not found.' })
@ApiParam({ name: 'id', description: 'The ID of the user' })
@Get(':id') @Get(':id')
findOne(@Param('id') id: string) { findOne(@Param('id') id: string) {
return this.usersService.findById(id); return this.usersService.findById(id);
@ -45,6 +56,11 @@ export class UsersController {
/** /**
* Update a user * Update a user
*/ */
@ApiOperation({ summary: 'Update a user' })
@ApiResponse({ status: 200, description: 'The user has been successfully updated.' })
@ApiResponse({ status: 400, description: 'Bad request.' })
@ApiResponse({ status: 404, description: 'User not found.' })
@ApiParam({ name: 'id', description: 'The ID of the user' })
@Patch(':id') @Patch(':id')
update(@Param('id') id: string, @Body() updateUserDto: UpdateUserDto) { update(@Param('id') id: string, @Body() updateUserDto: UpdateUserDto) {
return this.usersService.update(id, updateUserDto); return this.usersService.update(id, updateUserDto);
@ -53,6 +69,10 @@ export class UsersController {
/** /**
* Delete a user * Delete a user
*/ */
@ApiOperation({ summary: 'Delete a user' })
@ApiResponse({ status: 204, description: 'The user has been successfully deleted.' })
@ApiResponse({ status: 404, description: 'User not found.' })
@ApiParam({ name: 'id', description: 'The ID of the user' })
@Delete(':id') @Delete(':id')
@HttpCode(HttpStatus.NO_CONTENT) @HttpCode(HttpStatus.NO_CONTENT)
remove(@Param('id') id: string) { remove(@Param('id') id: string) {
@ -62,6 +82,10 @@ export class UsersController {
/** /**
* Update GDPR consent timestamp * Update GDPR consent timestamp
*/ */
@ApiOperation({ summary: 'Update GDPR consent timestamp' })
@ApiResponse({ status: 200, description: 'The GDPR consent timestamp has been successfully updated.' })
@ApiResponse({ status: 404, description: 'User not found.' })
@ApiParam({ name: 'id', description: 'The ID of the user' })
@Post(':id/gdpr-consent') @Post(':id/gdpr-consent')
@HttpCode(HttpStatus.OK) @HttpCode(HttpStatus.OK)
updateGdprConsent(@Param('id') id: string) { updateGdprConsent(@Param('id') id: string) {
@ -71,6 +95,10 @@ export class UsersController {
/** /**
* Export user data (for GDPR compliance) * Export user data (for GDPR compliance)
*/ */
@ApiOperation({ summary: 'Export user data (for GDPR compliance)' })
@ApiResponse({ status: 200, description: 'Return the user data.' })
@ApiResponse({ status: 404, description: 'User not found.' })
@ApiParam({ name: 'id', description: 'The ID of the user' })
@Get(':id/export-data') @Get(':id/export-data')
exportUserData(@Param('id') id: string) { exportUserData(@Param('id') id: string) {
return this.usersService.exportUserData(id); return this.usersService.exportUserData(id);