users.controller.ts 844 B

1234567891011121314151617181920212223242526
  1. import { Controller, Get, Param, NotFoundException } from '@nestjs/common';
  2. import { UsersService } from 'src/services/user.service';
  3. import { RegisteredUser } from 'src/interface/interface';
  4. @Controller('users')
  5. export class UsersController {
  6. constructor(private readonly usersService: UsersService) {}
  7. // GET /users/:id → Fetch user by ID
  8. @Get(':id')
  9. async getUserById(@Param('id') id: string): Promise<Omit<RegisteredUser, 'password'>> {
  10. const user = await this.usersService.findById(id);
  11. if (!user) {
  12. throw new NotFoundException(`User with ID ${id} not found`);
  13. }
  14. const { password, ...safeUser } = user;
  15. return safeUser;
  16. }
  17. // GET /users → (Optional) List all users (for debugging)
  18. @Get()
  19. getAllUsers(): Omit<RegisteredUser, 'password'>[] {
  20. return this.usersService.getAllUsers();
  21. }
  22. }