Spaces:
Sleeping
Sleeping
| import { | |
| Controller, | |
| Get, | |
| Post, | |
| Body, | |
| Param, | |
| Query, | |
| UseGuards, | |
| } from '@nestjs/common'; | |
| import { ApiTags, ApiBearerAuth } from '@nestjs/swagger'; | |
| import { UsersService } from './users.service'; | |
| import { SearchUsersDto } from './dto/search-users.dto'; | |
| import { JwtAuthGuard } from '../../common/guards/jwt-auth.guard'; | |
| import { CurrentUser } from '../../common/decorators/current-user.decorator'; | |
| interface UserProfileResponse { | |
| id: string; | |
| email?: string; | |
| display_name?: string; | |
| avatar_url?: string; | |
| bio?: string; | |
| created_at?: string; | |
| updated_at?: string; | |
| } | |
| interface UserStatusResponse { | |
| online: boolean; | |
| lastSeen?: string; | |
| } | |
| interface SearchUsersResponse { | |
| users: UserProfileResponse[]; | |
| total: number; | |
| limit: number; | |
| offset: number; | |
| } | |
| ('Users') | |
| () | |
| ('users') | |
| (JwtAuthGuard) | |
| export class UsersController { | |
| constructor(private readonly usersService: UsersService) {} | |
| /** | |
| * Search users by name or email | |
| * GET /users/search?q=&limit=&offset= | |
| */ | |
| ('search') | |
| async searchUsers( | |
| () query: SearchUsersDto, | |
| ('userId') currentUserId: string, | |
| ): Promise<SearchUsersResponse> { | |
| const result = await this.usersService.searchUsers(query, currentUserId); | |
| return { | |
| ...result, | |
| limit: query.limit || 20, | |
| offset: query.offset || 0, | |
| }; | |
| } | |
| /** | |
| * Get user profile by ID | |
| * GET /users/:userId | |
| */ | |
| (':userId') | |
| async getUserById( | |
| ('userId') userId: string, | |
| ): Promise<UserProfileResponse> { | |
| return this.usersService.getUserById(userId); | |
| } | |
| /** | |
| * Get user online status | |
| * GET /users/:userId/status | |
| */ | |
| (':userId/status') | |
| async getUserStatus( | |
| ('userId') userId: string, | |
| ): Promise<UserStatusResponse> { | |
| return this.usersService.getUserStatus(userId); | |
| } | |
| /** | |
| * Get multiple users online status (batch) | |
| * POST /users/status | |
| * Body: { userIds: string[] } | |
| */ | |
| ('status') | |
| async getUsersStatus( | |
| () dto: { userIds: string[] }, | |
| ): Promise<{ data: Array<{ userId: string; online: boolean; lastSeen?: string }> }> { | |
| const results = await Promise.all( | |
| (dto.userIds || []).map(async (userId) => { | |
| const status = await this.usersService.getUserStatus(userId); | |
| return { userId, ...status }; | |
| }), | |
| ); | |
| return { data: results }; | |
| } | |
| } | |