Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "User" ADD COLUMN "username" TEXT;
Original file line number Diff line number Diff line change
@@ -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");
1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
4 changes: 4 additions & 0 deletions src/modules/users/dto/create-user.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ export class CreateUserDto {
@IsNotEmpty()
name: string;

@IsString()
@IsOptional()
username: string;

@IsOptional()
@IsString()
description?: string;
Expand Down
18 changes: 9 additions & 9 deletions src/modules/users/users.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
40 changes: 24 additions & 16 deletions src/modules/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export class UsersService {
select: {
id: true,
name: true,
username: true,
function: true,
role: true,
},
Expand All @@ -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;
Expand All @@ -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 };
}
}
Loading