users.controller.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import {
  2. Controller,
  3. Put,
  4. Get,
  5. Body,
  6. Param,
  7. HttpStatus,
  8. NotFoundException,
  9. Delete,
  10. BadRequestException,
  11. } from '@nestjs/common';
  12. import { UsersService } from './users.service';
  13. import { UserProfileDto } from './dto/user-profile.dto';
  14. import { UserUpdateDto } from './dto/user-update.dto';
  15. import { IUsers } from './interfaces/users.interface';
  16. import { ApiTags } from '@nestjs/swagger';
  17. import { AuthGuard } from '../iam/login/decorators/auth-guard.decorator';
  18. import { AuthType } from '../iam/login/enums/auth-type.enum';
  19. @ApiTags('users')
  20. @AuthGuard(AuthType.Bearer)
  21. @Controller('users')
  22. export class UsersController {
  23. constructor(private readonly usersService: UsersService) {}
  24. @Get()
  25. public async findAllUser(): Promise<IUsers[]> {
  26. return this.usersService.findAll();
  27. }
  28. @Get('/:userId')
  29. public async findOneUser(@Param('userId') userId: string): Promise<IUsers> {
  30. return this.usersService.findById(userId);
  31. }
  32. @Get('/:userId/profile')
  33. public async getUser(@Param('userId') userId: string): Promise<any> {
  34. const user = await this.findOneUser(userId);
  35. if (!user) {
  36. throw new NotFoundException('User does not exist!');
  37. }
  38. return {
  39. user: user,
  40. status: HttpStatus.OK,
  41. };
  42. }
  43. @Put('/:userId/profile')
  44. public async updateProfileUser(
  45. @Param('userId') userId: string,
  46. @Body() userProfileDto: UserProfileDto,
  47. ): Promise<any> {
  48. try {
  49. await this.usersService.updateProfileUser(userId, userProfileDto);
  50. return {
  51. message: 'User Updated successfully!',
  52. status: HttpStatus.OK,
  53. };
  54. } catch (err) {
  55. throw new BadRequestException(err, 'Error: User not updated!');
  56. }
  57. }
  58. @Put('/:userId')
  59. public async updateUser(
  60. @Param('userId') userId: string,
  61. @Body() userUpdateDto: UserUpdateDto,
  62. ) {
  63. try {
  64. await this.usersService.updateUser(userId, userUpdateDto);
  65. return {
  66. message: 'User Updated successfully!',
  67. status: HttpStatus.OK,
  68. };
  69. } catch (err) {
  70. throw new BadRequestException(err, 'Error: User not updated!');
  71. }
  72. }
  73. @Delete('/:userId')
  74. public async deleteUser(@Param('userId') userId: string): Promise<void> {
  75. await this.usersService.deleteUser(userId);
  76. }
  77. }