53 lines
1.3 KiB
TypeScript
53 lines
1.3 KiB
TypeScript
import { Controller, Get, Patch, Body } from '@nestjs/common';
|
|
import { IsEmail, IsOptional, IsString, MaxLength } from 'class-validator';
|
|
import { UsersService } from './users.service';
|
|
import { CurrentUser } from '../auth/decorators/current-user.decorator';
|
|
|
|
class UpdateProfileDto {
|
|
@IsOptional()
|
|
@IsString()
|
|
@MaxLength(100)
|
|
firstName?: string;
|
|
|
|
@IsOptional()
|
|
@IsString()
|
|
@MaxLength(100)
|
|
lastName?: string;
|
|
|
|
@IsOptional()
|
|
@IsEmail()
|
|
email?: string;
|
|
}
|
|
|
|
@Controller('api/users')
|
|
export class UsersController {
|
|
constructor(private readonly usersService: UsersService) {}
|
|
|
|
@Get('me')
|
|
async getMe(@CurrentUser() user: { userId: number; username: string }) {
|
|
const found = await this.usersService.findById(user.userId);
|
|
return {
|
|
id: found?.id,
|
|
username: found?.username,
|
|
email: found?.email,
|
|
firstName: found?.firstName,
|
|
lastName: found?.lastName,
|
|
};
|
|
}
|
|
|
|
@Patch('me')
|
|
async updateMe(
|
|
@CurrentUser() user: { userId: number; username: string },
|
|
@Body() dto: UpdateProfileDto,
|
|
) {
|
|
const updated = await this.usersService.updateProfile(user.userId, dto);
|
|
return {
|
|
id: updated.id,
|
|
username: updated.username,
|
|
email: updated.email,
|
|
firstName: updated.firstName,
|
|
lastName: updated.lastName,
|
|
};
|
|
}
|
|
}
|