src/users/users.controller.ts
users
Controller for handling user-related routes
Methods |
createUser | ||||||||
createUser(createUserDto: CreateUserDto)
|
||||||||
Decorators :
@Post()
|
||||||||
Defined in src/users/users.controller.ts:41
|
||||||||
Route for creating a new user
Parameters :
Returns :
any
The created user |
deleteUserById | ||||||||
deleteUserById(id: number)
|
||||||||
Decorators :
@Delete(':id')
|
||||||||
Defined in src/users/users.controller.ts:90
|
||||||||
Route for deleting a user by their ID
Parameters :
Returns :
any
A success message or throws an exception if not found |
Async getUserById | ||||||||
getUserById(id: number)
|
||||||||
Decorators :
@Get(':id')
|
||||||||
Defined in src/users/users.controller.ts:62
|
||||||||
Route for retrieving a user by their ID
Parameters :
Returns :
unknown
The user with the specified ID or throws an exception if not found |
getUsers |
getUsers()
|
Decorators :
@Get()
|
Defined in src/users/users.controller.ts:51
|
Route for retrieving all users
Returns :
any
An array of all users |
updateUserById | ||||||||||||
updateUserById(id: number, updateUserDto: UpdateUserDto)
|
||||||||||||
Decorators :
@Patch(':id')
|
||||||||||||
Defined in src/users/users.controller.ts:76
|
||||||||||||
Route for updating a user by their ID
Parameters :
Returns :
any
The updated user |
import {
Body,
Controller,
Delete,
Get,
HttpException,
HttpStatus,
Param,
ParseIntPipe,
Patch,
Post,
UsePipes,
ValidationPipe,
} from '@nestjs/common';
import { CreateUserDto } from './dtos/create-user.dto';
import { UpdateUserDto } from './dtos/update-user.dto';
import { UsersService } from './users.service';
/**
* Controller for handling user-related routes
*/
@Controller('users')
export class UsersController {
/**
* Constructor that injects UsersService
*
* @param usersService - An instance of UsersService
*/
constructor(private usersService: UsersService) {}
/**
* Route for creating a new user
* - Applies validation pipe to validate the request body
*
* @param createUserDto - The DTO for creating a user
* @returns The created user
*/
@Post()
@UsePipes(new ValidationPipe())
createUser(@Body() createUserDto: CreateUserDto) {
return this.usersService.createUser(createUserDto);
}
/**
* Route for retrieving all users
*
* @returns An array of all users
*/
@Get()
getUsers() {
return this.usersService.getUsers();
}
/**
* Route for retrieving a user by their ID
*
* @param id - The ID of the user
* @returns The user with the specified ID or throws an exception if not found
*/
@Get(':id')
async getUserById(@Param('id', ParseIntPipe) id: number) {
const user = await this.usersService.getUserById(id);
if (!user) throw new HttpException('User not found', HttpStatus.NOT_FOUND);
return user;
}
/**
* Route for updating a user by their ID
*
* @param id - The ID of the user
* @param updateUserDto - The DTO for updating a user
* @returns The updated user
*/
@Patch(':id')
updateUserById(
@Param('id', ParseIntPipe) id: number,
@Body() updateUserDto: UpdateUserDto,
) {
return this.usersService.updateUserById(id, updateUserDto);
}
/**
* Route for deleting a user by their ID
*
* @param id - The ID of the user
* @returns A success message or throws an exception if not found
*/
@Delete(':id')
deleteUserById(@Param('id', ParseIntPipe) id: number) {
return this.usersService.deleteUserById(id);
}
}
export default UsersController;