feat(profile): add user profile management with first and last name fields

This commit is contained in:
Nils-Johan Gynther
2026-04-17 20:44:23 +02:00
parent 68b29f6d8e
commit a9e83544c5
9 changed files with 329 additions and 3 deletions
+52
View File
@@ -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,
};
}
}
+2
View File
@@ -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 {}
+4
View File
@@ -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 });
}
}