|
|
@ -11,7 +11,8 @@ import { |
|
|
|
Res, |
|
|
|
StreamableFile, |
|
|
|
BadRequestException, |
|
|
|
Redirect |
|
|
|
Redirect, |
|
|
|
type NotFoundException |
|
|
|
} from '@nestjs/common' |
|
|
|
|
|
|
|
import { FileInterceptor } from '@nestjs/platform-express' |
|
|
@ -30,59 +31,59 @@ import { ApiBody, ApiConsumes } from '@nestjs/swagger' |
|
|
|
import { type Request, Response } from 'express' |
|
|
|
import { createReadStream } from 'fs' |
|
|
|
import { join } from 'path' |
|
|
|
import type Result from 'src/pong/entity/result.entity' |
|
|
|
|
|
|
|
@Controller() |
|
|
|
export class UsersController { |
|
|
|
constructor( |
|
|
|
constructor ( |
|
|
|
private readonly usersService: UsersService, |
|
|
|
private readonly pongService: PongService |
|
|
|
) { } |
|
|
|
) {} |
|
|
|
|
|
|
|
@Get('all') |
|
|
|
async getAllUsers(): Promise<User[]> { |
|
|
|
async getAllUsers (): Promise<User[]> { |
|
|
|
return await this.usersService.findUsers() |
|
|
|
} |
|
|
|
|
|
|
|
@Get('online') |
|
|
|
async getOnlineUsers(): Promise<User[]> { |
|
|
|
async getOnlineUsers (): Promise<User[]> { |
|
|
|
return await this.usersService.findOnlineUsers() |
|
|
|
} |
|
|
|
|
|
|
|
@Get('friends') |
|
|
|
@UseGuards(AuthenticatedGuard) |
|
|
|
async getFriends(@FtUser() profile: Profile) { |
|
|
|
async getFriends (@FtUser() profile: Profile): Promise<User[]> { |
|
|
|
return await this.usersService.getFriends(profile.id) |
|
|
|
} |
|
|
|
|
|
|
|
@Get('invits') |
|
|
|
@UseGuards(AuthenticatedGuard) |
|
|
|
async getInvits(@FtUser() profile: Profile) { |
|
|
|
async getInvits (@FtUser() profile: Profile): Promise<User[]> { |
|
|
|
return await this.usersService.getInvits(profile.id) |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Get('leader') |
|
|
|
@UseGuards(AuthenticatedGuard) |
|
|
|
async getLeader() { |
|
|
|
async getLeader (): Promise<User[]> { |
|
|
|
return await this.usersService.getLeader() |
|
|
|
} |
|
|
|
|
|
|
|
@Get('leader/:id') |
|
|
|
@UseGuards(AuthenticatedGuard) |
|
|
|
async getRank(@Param('id', ParseIntPipe) id: number) { |
|
|
|
async getRank (@Param('id', ParseIntPipe) id: number): Promise<number> { |
|
|
|
return await this.usersService.getRank(id) |
|
|
|
} |
|
|
|
|
|
|
|
@Get('history') |
|
|
|
@UseGuards(AuthenticatedGuard) |
|
|
|
async getHistory() { |
|
|
|
async getHistory (): Promise<Result[]> { |
|
|
|
return await this.pongService.getHistory() |
|
|
|
} |
|
|
|
|
|
|
|
@Get('history/:id') |
|
|
|
@UseGuards(AuthenticatedGuard) |
|
|
|
async getHistoryById(@Param('id', ParseIntPipe) id: number) { |
|
|
|
return this.pongService.getHistoryById(id) |
|
|
|
async getHistoryById (@Param('id', ParseIntPipe) id: number): Promise<Result[]> { |
|
|
|
return await this.pongService.getHistoryById(id) |
|
|
|
} |
|
|
|
|
|
|
|
@Post('avatar') |
|
|
@ -107,43 +108,43 @@ export class UsersController { |
|
|
|
description: 'A new avatar for the user', |
|
|
|
type: AvatarUploadDto |
|
|
|
}) |
|
|
|
async addAvatar( |
|
|
|
async addAvatar ( |
|
|
|
@FtUser() profile: Profile, |
|
|
|
@UploadedFile() file: Express.Multer.File |
|
|
|
) { |
|
|
|
): Promise<void> { |
|
|
|
await this.usersService.addAvatar(profile.id, file.filename) |
|
|
|
} |
|
|
|
|
|
|
|
@Get('avatar') |
|
|
|
@UseGuards(AuthenticatedGuard) |
|
|
|
async getAvatar( |
|
|
|
async getAvatar ( |
|
|
|
@FtUser() profile: Profile, |
|
|
|
@Res({ passthrough: true }) response: Response |
|
|
|
) { |
|
|
|
): Promise<StreamableFile | null> { |
|
|
|
return await this.getAvatarById(profile.id, response) |
|
|
|
} |
|
|
|
|
|
|
|
@Get('user/:name') |
|
|
|
async getUserByName(@Param('name') username: string): Promise<User | null> { |
|
|
|
async getUserByName (@Param('name') username: string): Promise<User | null> { |
|
|
|
return await this.usersService.findUserByName(username) |
|
|
|
} |
|
|
|
|
|
|
|
@Get('invit/:id') |
|
|
|
@UseGuards(AuthenticatedGuard) |
|
|
|
async invitUser( |
|
|
|
async invitUser ( |
|
|
|
@FtUser() profile: Profile, |
|
|
|
@Param('id', ParseIntPipe) id: number |
|
|
|
) { |
|
|
|
): Promise<NotFoundException | null | undefined> { |
|
|
|
return await this.usersService.invit(profile.id, id) |
|
|
|
} |
|
|
|
|
|
|
|
@Get('avatar/:id') |
|
|
|
async getAvatarById( |
|
|
|
async getAvatarById ( |
|
|
|
@Param('id', ParseIntPipe) ftId: number, |
|
|
|
@Res({ passthrough: true }) response: Response |
|
|
|
) { |
|
|
|
): Promise<StreamableFile | null> { |
|
|
|
const user = await this.usersService.findUser(ftId) |
|
|
|
if (user == null) return |
|
|
|
if (user == null) return null |
|
|
|
const filename = user.avatar |
|
|
|
const stream = createReadStream(join(process.cwd(), 'avatars/' + filename)) |
|
|
|
response.set({ |
|
|
@ -154,15 +155,15 @@ export class UsersController { |
|
|
|
} |
|
|
|
|
|
|
|
@Get(':id') |
|
|
|
async getUserById( |
|
|
|
async getUserById ( |
|
|
|
@Param('id', ParseIntPipe) ftId: number |
|
|
|
): Promise<User | null> { |
|
|
|
return await this.usersService.findUser(ftId) |
|
|
|
} |
|
|
|
|
|
|
|
@Post(":id") |
|
|
|
@Post(':id') |
|
|
|
@UseGuards(AuthenticatedGuard) |
|
|
|
async createById(@Body() payload: UserDto) { |
|
|
|
async createById (@Body() payload: UserDto): Promise<User | null> { |
|
|
|
const user = await this.usersService.findUser(payload.ftId) |
|
|
|
if (user != null) { |
|
|
|
return await this.usersService.update(user, payload) |
|
|
@ -173,13 +174,13 @@ export class UsersController { |
|
|
|
|
|
|
|
@Get() |
|
|
|
@UseGuards(AuthenticatedGuard) |
|
|
|
async getUser(@FtUser() profile: Profile): Promise<User | null> { |
|
|
|
async getUser (@FtUser() profile: Profile): Promise<User | null> { |
|
|
|
return await this.usersService.findUser(profile.id) |
|
|
|
} |
|
|
|
|
|
|
|
@Post() |
|
|
|
@UseGuards(AuthenticatedGuard) |
|
|
|
async create(@Body() payload: UserDto, @FtUser() profile: Profile) { |
|
|
|
async create (@Body() payload: UserDto, @FtUser() profile: Profile): Promise<User | null> { |
|
|
|
const user = await this.usersService.findUser(profile.id) |
|
|
|
if (user != null) { |
|
|
|
return await this.usersService.update(user, payload) |
|
|
@ -187,5 +188,4 @@ export class UsersController { |
|
|
|
return await this.usersService.create(payload) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|