From fb36f41c523ae54343e51f0a017e936d33942150 Mon Sep 17 00:00:00 2001 From: Leticia Dias Date: Wed, 20 Aug 2025 15:13:20 -0300 Subject: [PATCH 1/2] feat(schema): added new username column in User table --- .../20250819180624_add_username_column/migration.sql | 2 ++ .../20250819182148_add_unique_to_username/migration.sql | 8 ++++++++ prisma/schema.prisma | 1 + 3 files changed, 11 insertions(+) create mode 100644 prisma/migrations/20250819180624_add_username_column/migration.sql create mode 100644 prisma/migrations/20250819182148_add_unique_to_username/migration.sql diff --git a/prisma/migrations/20250819180624_add_username_column/migration.sql b/prisma/migrations/20250819180624_add_username_column/migration.sql new file mode 100644 index 0000000..5af7c52 --- /dev/null +++ b/prisma/migrations/20250819180624_add_username_column/migration.sql @@ -0,0 +1,2 @@ +-- AlterTable +ALTER TABLE "User" ADD COLUMN "username" TEXT; diff --git a/prisma/migrations/20250819182148_add_unique_to_username/migration.sql b/prisma/migrations/20250819182148_add_unique_to_username/migration.sql new file mode 100644 index 0000000..54942a5 --- /dev/null +++ b/prisma/migrations/20250819182148_add_unique_to_username/migration.sql @@ -0,0 +1,8 @@ +/* + Warnings: + + - A unique constraint covering the columns `[username]` on the table `User` will be added. If there are existing duplicate values, this will fail. + +*/ +-- CreateIndex +CREATE UNIQUE INDEX "User_username_key" ON "User"("username"); diff --git a/prisma/schema.prisma b/prisma/schema.prisma index a1a5150..9b2bdfc 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -20,6 +20,7 @@ model User { id Int @id @default(autoincrement()) email String @unique name String + username String? @unique description String? passwordHash String? role String? From 2ffb12cdd5eda784841d2e022d530bb0654f7601 Mon Sep 17 00:00:00 2001 From: Leticia Dias Date: Wed, 20 Aug 2025 15:15:01 -0300 Subject: [PATCH 2/2] feat(users): added username as a parameter to search for user information --- src/modules/users/dto/create-user.dto.ts | 4 +++ src/modules/users/users.controller.ts | 18 +++++------ src/modules/users/users.service.ts | 40 ++++++++++++++---------- 3 files changed, 37 insertions(+), 25 deletions(-) diff --git a/src/modules/users/dto/create-user.dto.ts b/src/modules/users/dto/create-user.dto.ts index de30f18..a91c14b 100644 --- a/src/modules/users/dto/create-user.dto.ts +++ b/src/modules/users/dto/create-user.dto.ts @@ -15,6 +15,10 @@ export class CreateUserDto { @IsNotEmpty() name: string; + @IsString() + @IsOptional() + username: string; + @IsOptional() @IsString() description?: string; diff --git a/src/modules/users/users.controller.ts b/src/modules/users/users.controller.ts index 63b2f1d..3994d08 100644 --- a/src/modules/users/users.controller.ts +++ b/src/modules/users/users.controller.ts @@ -26,18 +26,18 @@ export class UsersController { return this.userService.findAll(); } - @Get(':id') - findOne(@Param('id', ParseIdPipe) id: number) { - return this.userService.findOne(id); + @Get(':username') + findOne(@Param('username') username: string) { + return this.userService.findOne(username); } - @Patch(':id') - update(@Param('id', ParseIdPipe) id: number, @Body() dto: UpdateUserDto) { - return this.userService.update(id, dto); + @Patch(':username') + update(@Param('username') username: string, @Body() dto: UpdateUserDto) { + return this.userService.update(username, dto); } - @Delete(':id') - remove(@Param('id', ParseIdPipe) id: number) { - return this.userService.remove(id); + @Delete(':username') + remove(@Param('username') username: string) { + return this.userService.remove(username); } } diff --git a/src/modules/users/users.service.ts b/src/modules/users/users.service.ts index 1875728..829f020 100644 --- a/src/modules/users/users.service.ts +++ b/src/modules/users/users.service.ts @@ -17,6 +17,7 @@ export class UsersService { select: { id: true, name: true, + username: true, function: true, role: true, }, @@ -26,19 +27,24 @@ export class UsersService { }); } - async findOne(id: number) { + async findOne(username: string) { const user = await this.prisma.user.findUnique({ - where: { id }, + where: { username }, select: { name: true, function: true, description: true, - posts: { where: { isPublished: true }, select: { title: true } }, + posts: { + where: { isPublished: true }, + select: { title: true, slug: true }, + }, }, }); if (!user) { - throw new NotFoundException('User not found'); + throw new NotFoundException( + `Usuário com o nome de ${username} não encontrado.`, + ); } return user; @@ -48,33 +54,35 @@ export class UsersService { const user = this.prisma.user.findFirst({ where: { name } }); if (!user) { - throw new NotFoundException( - `Usuário com o nome "${name}" não encontrado.`, - ); + throw new NotFoundException(`Usuário com o nome ${name} não encontrado.`); } return user; } - async update(id: number, data: UpdateUserDto) { - const findUser = await this.findOne(id); + async update(username: string, data: UpdateUserDto) { + const findUser = await this.findOne(username); if (!findUser) { - throw new NotFoundException('User not found'); + throw new NotFoundException( + `Usuário com o nome de "${username}" não encontrado.`, + ); } - return this.prisma.user.update({ where: { id }, data }); + return this.prisma.user.update({ where: { username }, data }); } - async remove(id: number) { - const findUser = await this.findOne(id); + async remove(username: string) { + const findUser = await this.findOne(username); if (!findUser) { - throw new NotFoundException('User not found'); + throw new NotFoundException( + `Usuário com o nome de "${username}" não encontrado.`, + ); } - await this.prisma.user.delete({ where: { id } }); + await this.prisma.user.delete({ where: { username } }); - return { message: 'User successfully removed', id }; + return { message: 'Usuário removido com sucesso!', username }; } }