feat(profile): add user profile management with first and last name fields
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
ALTER TABLE `User` ADD COLUMN `firstName` VARCHAR(191) NULL;
|
||||
ALTER TABLE `User` ADD COLUMN `lastName` VARCHAR(191) NULL;
|
||||
@@ -11,6 +11,8 @@ model User {
|
||||
id Int @id @default(autoincrement())
|
||||
username String @unique
|
||||
email String @unique
|
||||
firstName String?
|
||||
lastName String?
|
||||
passwordHash String
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
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,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,12 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { UsersService } from './users.service';
|
||||
import { UsersController } from './users.controller';
|
||||
import { PrismaModule } from '../prisma/prisma.module';
|
||||
|
||||
@Module({
|
||||
imports: [PrismaModule],
|
||||
providers: [UsersService],
|
||||
controllers: [UsersController],
|
||||
exports: [UsersService],
|
||||
})
|
||||
export class UsersModule {}
|
||||
|
||||
@@ -16,4 +16,8 @@ export class UsersService {
|
||||
create(data: { username: string; email: string; passwordHash: string }) {
|
||||
return this.prisma.user.create({ data });
|
||||
}
|
||||
|
||||
updateProfile(id: number, data: { firstName?: string; lastName?: string; email?: string }) {
|
||||
return this.prisma.user.update({ where: { id }, data });
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user