File

src/users/users.controller.ts

Prefix

users

Description

Controller for handling user-related routes

Index

Methods

Methods

createUser
createUser(createUserDto: CreateUserDto)
Decorators :
@Post()
@UsePipes(new ValidationPipe())

Route for creating a new user

  • Applies validation pipe to validate the request body
Parameters :
Name Type Optional Description
createUserDto CreateUserDto No
  • The DTO for creating a user
Returns : any

The created user

deleteUserById
deleteUserById(id: number)
Decorators :
@Delete(':id')

Route for deleting a user by their ID

Parameters :
Name Type Optional Description
id number No
  • The ID of the user
Returns : any

A success message or throws an exception if not found

Async getUserById
getUserById(id: number)
Decorators :
@Get(':id')

Route for retrieving a user by their ID

Parameters :
Name Type Optional Description
id number No
  • The ID of the user
Returns : unknown

The user with the specified ID or throws an exception if not found

getUsers
getUsers()
Decorators :
@Get()

Route for retrieving all users

Returns : any

An array of all users

updateUserById
updateUserById(id: number, updateUserDto: UpdateUserDto)
Decorators :
@Patch(':id')

Route for updating a user by their ID

Parameters :
Name Type Optional Description
id number No
  • The ID of the user
updateUserDto UpdateUserDto No
  • The DTO for updating a user
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;

results matching ""

    No results matching ""