api-scout
Version:
🔍 Automatically scout, discover and generate beautiful interactive API documentation from your codebase. Supports Express.js, NestJS, FastAPI, Spring Boot with interactive testing and security analysis.
167 lines (154 loc) • 4.18 kB
text/typescript
import {
Controller,
Get,
Post,
Put,
Delete,
Body,
Param,
Query,
Headers,
UseGuards,
UseInterceptors,
UsePipes,
ValidationPipe,
HttpStatus,
HttpException
} from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { RolesGuard } from '../guards/roles.guard';
import { TransformInterceptor } from '../interceptors/transform.interceptor';
import { CreateUserDto, UpdateUserDto, UserResponseDto } from './dto/user.dto';
import { User } from '../entities/user.entity';
/**
* Users Controller
* Handles all user-related HTTP requests
*/
export class UsersController {
constructor(private readonly usersService: UsersService) {}
/**
* Get all users with pagination
* Returns a paginated list of users
*/
async findAll(
page: number = 1,
limit: number = 10,
search?: string,
role?: string
): Promise<UserResponseDto[]> {
return this.usersService.findAll({
page,
limit,
search,
role
});
}
/**
* Get user by ID
* Retrieves a specific user by their unique identifier
*/
async findOne( id: string): Promise<UserResponseDto> {
const user = await this.usersService.findOne(+id);
if (!user) {
throw new HttpException('User not found', HttpStatus.NOT_FOUND);
}
return user;
}
/**
* Create a new user
* Creates a new user account with validation
*/
async create(
createUserDto: CreateUserDto,
auth: string
): Promise<UserResponseDto> {
return this.usersService.create(createUserDto);
}
/**
* Update user information
* Updates an existing user's data
*/
async update(
id: string,
updateUserDto: UpdateUserDto,
userId: string
): Promise<UserResponseDto> {
return this.usersService.update(+id, updateUserDto);
}
/**
* Delete user account
* Permanently removes a user from the system
*/
async remove(
id: string,
auth: string
): Promise<{ message: string; deletedId: number }> {
await this.usersService.remove(+id);
return {
message: 'User successfully deleted',
deletedId: +id
};
}
/**
* Get user's posts
* Retrieves all posts created by a specific user
*/
async getUserPosts(
id: string,
status: 'published' | 'draft' | 'archived' = 'published',
page: number = 1,
limit: number = 10
): Promise<any[]> {
return this.usersService.getUserPosts(+id, {
status,
page,
limit
});
}
/**
* Update user avatar
* Uploads and updates user profile picture
*/
async updateAvatar(
id: string,
avatarData: { avatarUrl: string },
contentType: string
): Promise<{ message: string; avatarUrl: string }> {
const result = await this.usersService.updateAvatar(+id, avatarData.avatarUrl);
return {
message: 'Avatar updated successfully',
avatarUrl: result.avatarUrl
};
}
/**
* Get user statistics
* Returns various statistics about the user
*/
async getUserStats(
id: string,
period: '7d' | '30d' | '1y' = '30d'
): Promise<{
posts: number;
comments: number;
likes: number;
followers: number;
}> {
return this.usersService.getUserStats(+id, period);
}
}