82 lines
3.0 KiB
TypeScript
82 lines
3.0 KiB
TypeScript
import { Controller, Get, Post, Body, Patch, Param, Delete, Query } from '@nestjs/common';
|
|
import { UsersService } from './users.service';
|
|
import { CreateUserDto } from './dto/create-user.dto';
|
|
import { UpdateUserDto } from './dto/update-user.dto';
|
|
import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
|
|
import { Roles } from '../../common/decorators/roles.decorator';
|
|
import { PaginationDto } from '../../common/dto/pagination.dto';
|
|
|
|
@ApiTags('users')
|
|
@Controller('users')
|
|
export class UsersController {
|
|
constructor(private readonly usersService: UsersService) {}
|
|
|
|
@Get()
|
|
@Roles('admin')
|
|
@ApiOperation({ summary: 'Get all users with pagination and filters' })
|
|
@ApiResponse({ status: 200, description: 'Return paginated users.' })
|
|
async findAll(@Query() paginationDto: PaginationDto) {
|
|
const result = await this.usersService.findAll(paginationDto);
|
|
return {
|
|
message: 'Users fetched successfully',
|
|
data: result.data,
|
|
meta: result.meta
|
|
};
|
|
}
|
|
|
|
@Get(':id')
|
|
// @Roles('admin')
|
|
@ApiOperation({ summary: 'Get a user by ID' })
|
|
@ApiResponse({ status: 200, description: 'Return the user.' })
|
|
@ApiResponse({ status: 404, description: 'User not found.' })
|
|
async findOne(@Param('id') id: string) {
|
|
const data = await this.usersService.findOne(id);
|
|
return { message: 'User fetched successfully', data };
|
|
}
|
|
|
|
@Post()
|
|
@Roles('admin')
|
|
@ApiOperation({ summary: 'Create a new user' })
|
|
@ApiResponse({ status: 201, description: 'User created successfully.' })
|
|
async create(
|
|
@Body() createUserDto: CreateUserDto,
|
|
@Query('roleId') roleId?: string,
|
|
) {
|
|
const data = await this.usersService.create(
|
|
createUserDto,
|
|
roleId ? parseInt(roleId) : undefined,
|
|
);
|
|
return { message: 'User created successfully', data };
|
|
}
|
|
|
|
@Patch(':id')
|
|
@Roles('admin')
|
|
@ApiOperation({ summary: 'Update a user' })
|
|
@ApiResponse({ status: 200, description: 'User updated successfully.' })
|
|
@ApiResponse({ status: 404, description: 'User not found.' })
|
|
async update(@Param('id') id: string, @Body() updateUserDto: UpdateUserDto) {
|
|
const data = await this.usersService.update(id, updateUserDto);
|
|
return { message: 'User updated successfully', data };
|
|
}
|
|
|
|
@Patch('profile/:id')
|
|
// @Roles('admin')
|
|
@ApiOperation({ summary: 'Update a user' })
|
|
@ApiResponse({ status: 200, description: 'User updated successfully.' })
|
|
@ApiResponse({ status: 400, description: 'email already exists.' })
|
|
@ApiResponse({ status: 404, description: 'User not found.' })
|
|
async updateProfile(@Param('id') id: string, @Body() updateUserDto: UpdateUserDto) {
|
|
const data = await this.usersService.updateProfile(id, updateUserDto);
|
|
return { message: 'User updated successfully', data };
|
|
}
|
|
|
|
@Delete(':id')
|
|
@Roles('admin')
|
|
@ApiOperation({ summary: 'Delete a user' })
|
|
@ApiResponse({ status: 200, description: 'User deleted successfully.' })
|
|
@ApiResponse({ status: 404, description: 'User not found.' })
|
|
async remove(@Param('id') id: string) {
|
|
return await this.usersService.remove(id);
|
|
}
|
|
}
|