Skip to content

Commit

Permalink
refact: somes routes
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustavo-Murdiga88 committed Mar 31, 2024
1 parent eb9d9fc commit 06d32e1
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 6 deletions.
5 changes: 5 additions & 0 deletions prisma/migrations/20240330173039_cascade/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- DropForeignKey
ALTER TABLE "avatars" DROP CONSTRAINT "avatars_user_id_fkey";

-- AddForeignKey
ALTER TABLE "avatars" ADD CONSTRAINT "avatars_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;
2 changes: 1 addition & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ model Avatar {
user_id String @unique()
url String?
user User @relation(fields: [user_id], references: [id])
user User @relation(fields: [user_id], references: [id], onDelete: Cascade)
@@map("avatars")
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Injectable } from "@nestjs/common";
import { Either, left, right } from "@/core/either";

import { UserRepository } from "../repositories/user-repository";
import { Deleter } from "../storage/delete";

type DeleteUserUsecaseResponse = Promise<Either<Error, null>>;
type DeleteUser = {
Expand All @@ -13,8 +14,11 @@ type DeleteUser = {
export class DeleteUserUseCase {
private repository: UserRepository;

constructor(repository: UserRepository) {
private deleter: Deleter;

constructor(repository: UserRepository, deleter: Deleter) {
this.repository = repository;
this.deleter = deleter;
}

async execute({ id }: DeleteUser): DeleteUserUsecaseResponse {
Expand All @@ -26,6 +30,10 @@ export class DeleteUserUseCase {

await this.repository.delete(id);

if (user.avatar.url !== "") {
await this.deleter.delete({ url: user.avatar.url });
}

return right(null);
}
}
3 changes: 3 additions & 0 deletions src/domain/register/application/use-cases/singin-usecase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,15 @@ export class SigninUsecase {
const token = await this.jwt.sign({
email: user.email,
id: user.id,
name: user.name,
avatarURL: user.avatar?.url ?? "",
});

const refreshToken = await this.jwt.sign(
{
email: user.email,
id: user.id,
name: user.name,
},
{ expiresIn: "7d" },
);
Expand Down
23 changes: 21 additions & 2 deletions src/infra/database/prisma/repositories/prisma-user-repository.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { faker } from "@faker-js/faker";
import { Injectable } from "@nestjs/common";

import { IPagination } from "@/core/pagination";
Expand All @@ -8,6 +7,7 @@ import {
SaveUserProps,
UserRepository,
} from "@/domain/register/application/repositories/user-repository";
import { Avatar } from "@/domain/register/enterprise/entities/avatar";
import { User } from "@/domain/register/enterprise/entities/user";
import { UserAvatar } from "@/domain/register/enterprise/value-objects/user-with-avatar";

Expand Down Expand Up @@ -39,18 +39,27 @@ export class PrismaUserRepository implements UserRepository {
where: {
email,
},
include: {
avatar: true,
},
});

if (!user) {
return null;
}

return User.create({
id: user.id,
age: user.age,
email: user.email,
name: user.name,
role: user.role,
password: user.password,
avatar: Avatar.create({
userId: user.id,
id: user.avatar?.id,
url: user.avatar?.url,
}),
});
}

Expand All @@ -59,6 +68,9 @@ export class PrismaUserRepository implements UserRepository {
where: {
id,
},
include: {
avatar: true,
},
});

if (!user) {
Expand All @@ -70,7 +82,12 @@ export class PrismaUserRepository implements UserRepository {
email: user.email,
name: user.name,
role: user.role,
password: faker.internet.password(),
password: user.password,
avatar: Avatar.create({
userId: user.id,
id: user.avatar?.id ?? undefined,
url: user.avatar?.url ?? undefined,
}),
});
}

Expand Down Expand Up @@ -107,6 +124,7 @@ export class PrismaUserRepository implements UserRepository {
where: {
name: {
contains: name,
mode: "insensitive",
},
},
});
Expand All @@ -115,6 +133,7 @@ export class PrismaUserRepository implements UserRepository {
where: {
name: {
contains: name,
mode: "insensitive",
},
},
include: {
Expand Down
3 changes: 2 additions & 1 deletion src/infra/http/controllers/create-user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export class CreateUserController {
type: "string",
enum: ["ALL", "EDIT", "DELETE"],
},
password: {
id: {
type: "string",
},
},
Expand Down Expand Up @@ -102,6 +102,7 @@ export class CreateUserController {
age: user.value.age,
email: user.value.email,
role: user.value.role,
avatar: user.value.avatar,
},
};
}
Expand Down
22 changes: 21 additions & 1 deletion src/infra/http/controllers/upload-avatar.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ import {
UsePipes,
} from "@nestjs/common";
import { FileInterceptor } from "@nestjs/platform-express/multer";
import { ApiBody, ApiConsumes, ApiProperty, ApiTags } from "@nestjs/swagger";
import {
ApiBody,
ApiConsumes,
ApiCreatedResponse,
ApiProperty,
ApiTags,
} from "@nestjs/swagger";

import { SaveAvatarUseCase } from "@/domain/register/application/use-cases/save-avatar-usecase";

Expand All @@ -32,6 +38,20 @@ export class UploadAvatarController {
@ApiTags("Dashskins")
@ApiConsumes("multipart/form-data")
@ApiProperty({ type: "string", format: "binary" })
@ApiCreatedResponse({
description: "Response when user try create a session",
status: 201,
schema: {
properties: {
url: {
type: "string",
},
id: {
type: "string",
},
},
},
})
@ApiBody({
description: "Upload avatar",
type: FileUpload,
Expand Down
1 change: 1 addition & 0 deletions src/infra/http/http.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { UploadAvatarController } from "./controllers/upload-avatar.controller";
CryptographyModule,
StorageModule,
ServeStaticModule.forRootAsync({
isGlobal: true,
imports: [EnvModule],
inject: [EnvService],
useFactory: (envService: EnvService) => {
Expand Down
2 changes: 2 additions & 0 deletions src/infra/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ async function bootstrap() {
const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup("api", app, document);

app.enableCors();

await app.listen(port);
}
bootstrap();

0 comments on commit 06d32e1

Please sign in to comment.