0% found this document useful (0 votes)
3 views14 pages

Camada: Domain: Entity

Uploaded by

Caue Santana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views14 pages

Camada: Domain: Entity

Uploaded by

Caue Santana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Camada: Domain

Entity:
export class newProfile {
constructor(
public readonly value: string,
public readonly description: string,
){}
}

export class Profile {


constructor(
public readonly id: number,
public readonly value?: string,
public readonly description?: string,
){}

}
Repository:
import { newProfile, Profile } from "src/Domain/Entities/Assist/profile.entity";

export interface ProfileRepository {


create(profile: newProfile): Promise<newProfile>;
update(data: Profile): Promise<Profile>;
delete(id: number): Promise<void>;
findAll(): Promise<Profile[]>;
findById(id: number): Promise<Profile | null>;
findByValue(value: string): Promise<Profile | null>;
}
Service:
import { Injectable, Inject, ConflictException, NotFoundException } from
"@nestjs/common";

import { newProfile, Profile } from "../../Entities/Assist/profile.entity";


import { ProfileRepository } from "../../Repositories/Assist/profile.repository";

@Injectable()
export class ProfileService {
constructor(
@Inject('ProfileRepository')
private readonly profileRepository: ProfileRepository

){}

async createProfile(data: newProfile): Promise<newProfile> {


const checkProfile = await this.profileRepository.findByValue(data.value);

if (checkProfile) {
throw new ConflictException(`Profile with value '${data.value}' already
exists.`);

} else {
const profile = new newProfile(data.value, data.description);
return this.profileRepository.create(data);

}
}

async updateProfile(data: Profile): Promise<Profile> {


const locateProfile = await this.profileRepository.findById(data.id);

if (!locateProfile) {
throw new NotFoundException(`Profile with ID '${data.id}' not found.`);

} else {
const checkProfile = await
this.profileRepository.findByValue(data.value);

if (checkProfile && checkProfile.id !== data.id) {


throw new ConflictException(`Profile with value '${data.value}' already
exists.`);

} else {
const profile = new Profile(+data.id, data.value, data.description);
return this.profileRepository.update(profile);
}
}
}

async deleteProfile(id: number): Promise<void> {


const locateProfile = await this.profileRepository.findById(id);

if (!locateProfile) {
throw new NotFoundException(`Profile with ID '${id}' not found.`);

} else {
return this.profileRepository.delete(+id);

}
}

async findAllProfile(): Promise<Profile[]> {


return this.profileRepository.findAll();

async findProfileById(id: number): Promise<Profile | null> {


return this.profileRepository.findById(+id);

async findProfileByValue(value: string): Promise<Profile | null> {


return this.profileRepository.findByValue(value);

}
}
Camada: Application

UseCases:

create-profile.use-case
import { Injectable } from "@nestjs/common";

import { newProfile } from "src/Domain/Entities/Assist/profile.entity";


import { ProfileService } from "src/Domain/Services/Assist/profile.service";

@Injectable()
export class CreateProfileUseCase {
constructor(private readonly profileService: ProfileService) { }

async execute(data: newProfile): Promise<newProfile> {


return await this.profileService.createProfile(data);
}
}

delete-profile.use-case
import { Injectable } from "@nestjs/common";

import { ProfileService } from "src/Domain/Services/Assist/profile.service";

@Injectable()
export class DeleteProfileUseCase {
constructor(private readonly profileService: ProfileService) { }

async execute(id: number): Promise<void> {


return await this.profileService.deleteProfile(+id);
}
}
find-all-profiles.use-case
import { Injectable } from "@nestjs/common";

import { Profile } from "src/Domain/Entities/Assist/profile.entity";


import { ProfileService } from "src/Domain/Services/Assist/profile.service";

@Injectable()
export class FindAllProfilesUseCase {
constructor(private readonly profileService: ProfileService) { }

async execute(): Promise<Profile[]> {


return await this.profileService.findAllProfile();
}
}

find-profile-by-id.use-case
import { Injectable } from "@nestjs/common";

import { Profile } from "src/Domain/Entities/Assist/profile.entity";


import { ProfileService } from "src/Domain/Services/Assist/profile.service";

@Injectable()
export class FindProfileByIdUseCase {
constructor(private readonly profileService: ProfileService) { }

async execute(id: number): Promise<Profile | null> {


return await this.profileService.findProfileById(+id);
}
}

find-profile-by-value.use-case
import { Injectable } from "@nestjs/common";

import { Profile } from "src/Domain/Entities/Assist/profile.entity";


import { ProfileService } from "src/Domain/Services/Assist/profile.service";

@Injectable()
export class FindProfileByValueUseCase {
constructor(private readonly profileService: ProfileService) { }

async execute(value: string): Promise<Profile | null> {


return await this.profileService.findProfileByValue(value);
}
}
update-profile.use-case
import { Injectable } from "@nestjs/common";

import { Profile } from "src/Domain/Entities/Assist/profile.entity";


import { ProfileService } from "src/Domain/Services/Assist/profile.service";
import { ProfileUpdateDto } from "src/Interface/dto/Assist/profile.update-dto";

@Injectable()
export class UpdateProfileUseCase {
constructor(private readonly profileService: ProfileService) { }

async execute(dataDto: ProfileUpdateDto): Promise<Profile> {

const profile: Profile = {


id: dataDto.id,
value: dataDto.value,
description: dataDto.description,
}

return await this.profileService.updateProfile(profile);


}
}
Camada: Interface

DTO:

profile.create-dto
import { IsAlpha, IsNotEmpty, IsString, Length, Matches } from "class-validator";

const minValueLenght = 3, maxValueLenght = 16;


const minDescLenght = 5, maxDescLenght = 64;

export class ProfileCreateDto {

@IsNotEmpty({ message: "O campo não pode estar vazio." })


@IsString({ message: "O dado deve ser convertido para string antes de ser
armazenado." })
@Length(minValueLenght, maxValueLenght, { message: `O campo deve
conter entre ${minValueLenght} e ${maxValueLenght}` })
@IsAlpha(undefined, { message: "O campo deve conter somente letras" })
value: string;

@IsNotEmpty({ message: "O campo não pode estar vazio." })


@IsString({ message: "O dado deve ser convertido para string antes de ser
armazenado." })
@Length(minDescLenght, maxDescLenght, { message: `O campo deve
conter entre ${minDescLenght} e ${maxDescLenght}` })
@Matches(/^[A-Za-z]+(?:[, ]+[A-Za-z]+)*$/, { message: "O campo 'description'
deve conter somente letras, vírgulas e espaços entre palavras." })
description: string;
}

profile.update-dto
import { PartialType } from '@nestjs/mapped-types';
import { ProfileCreateDto } from './profile.create-dto';
import { IsNotEmpty, IsInt } from 'class-validator';

export class ProfileUpdateDto extends PartialType(ProfileCreateDto) {

@IsNotEmpty({ message: "O campo 'id' não pode estar vazio." })


@IsInt({ message: "ID deve ser convertido para inteiro antes de ser
armazenado." })
id: number
}
Controllers:

import { Controller, Body, Param, Post, Patch, Delete, Get } from


"@nestjs/common";

import { CreateProfileUseCase } from


"src/Application/use-cases/Assist/profile/create-profile.use-case";
import { DeleteProfileUseCase } from
"src/Application/use-cases/Assist/profile/delete-profile.use-case";
import { FindAllProfilesUseCase } from
"src/Application/use-cases/Assist/profile/find-all-profiles.use-case";
import { FindProfileByIdUseCase } from
"src/Application/use-cases/Assist/profile/find-profile-by-id.use-case";
import { FindProfileByValueUseCase } from
"src/Application/use-cases/Assist/profile/find-profile-by-value.use-case";
import { UpdateProfileUseCase } from
"src/Application/use-cases/Assist/profile/update-profile.use-case";

import { ProfileCreateDto } from "src/Interface/dto/Assist/profile.create-dto";


import { ProfileUpdateDto } from "src/Interface/dto/Assist/profile.update-dto";

@Controller('profile')
export class ProfileController {
constructor(
private readonly createProfileUseCase: CreateProfileUseCase,
private readonly updateProfileUseCase: UpdateProfileUseCase,
private readonly deleteProfileUseCase: DeleteProfileUseCase,
private readonly findAllProfilesUseCase: FindAllProfilesUseCase,
private readonly findProfileByIdUseCase: FindProfileByIdUseCase,
private readonly findProfileByValueUseCase: FindProfileByValueUseCase,
){}

@Post()
async create(@Body() data: ProfileCreateDto) {
return this.createProfileUseCase.execute(data)
}

@Patch()
async update(@Body() data: ProfileUpdateDto) {
return this.updateProfileUseCase.execute(data)
}

@Delete()
async remove(@Body() data: ProfileUpdateDto) {
return this.deleteProfileUseCase.execute(+data.id);
}
@Get()
async findAll() {
return this.findAllProfilesUseCase.execute();
}

@Get(':id')
async findById(@Param('id') id: number) {
return this.findProfileByIdUseCase.execute(id);
}

@Get(':value')
async findByValue(@Param('value') value: string) {
return this.findProfileByValueUseCase.execute(value);
}
}
Camada: Infrastructre

Module:
import { Module } from "@nestjs/common";
import { PrismaModule } from "src/Infrastructure/Prisma/prisma.module";
import { ProfileService } from "src/Domain/Services/Assist/profile.service";
import { ProfileController } from
"src/Interface/Controllers/Assist/profile.controller";
import { PrismaProfileRepository } from
"src/Infrastructure/Repositories/Assist/prisma-profile.repository";
import { CreateProfileUseCase } from
"src/Application/use-cases/Assist/profile/create-profile.use-case";
import { UpdateProfileUseCase } from
"src/Application/use-cases/Assist/profile/update-profile.use-case";
import { DeleteProfileUseCase } from
"src/Application/use-cases/Assist/profile/delete-profile.use-case";
import { FindAllProfilesUseCase } from
"src/Application/use-cases/Assist/profile/find-all-profiles.use-case";
import { FindProfileByIdUseCase } from
"src/Application/use-cases/Assist/profile/find-profile-by-id.use-case";
import { FindProfileByValueUseCase } from
"src/Application/use-cases/Assist/profile/find-profile-by-value.use-case";

@Module({
imports: [PrismaModule],
exports: [ProfileService],
controllers: [ProfileController],

providers: [
ProfileService, PrismaProfileRepository,

CreateProfileUseCase,
UpdateProfileUseCase,
DeleteProfileUseCase,
FindAllProfilesUseCase,
FindProfileByIdUseCase,
FindProfileByValueUseCase,

{
provide: 'ProfileRepository',
useClass: PrismaProfileRepository
},
],
}) export class ProfileModule { }
Prisma Repository:
import { Injectable } from "@nestjs/common";
import { PrismaService } from "src/Infrastructure/Prisma/prisma.service";

import { newProfile, Profile } from "src/Domain/Entities/Assist/profile.entity";


import { ProfileRepository } from
"src/Domain/Repositories/Assist/profile.repository";

@Injectable()
export class PrismaProfileRepository implements ProfileRepository {
constructor(private readonly prisma: PrismaService) { }

async create(profile: newProfile): Promise<newProfile> {


const created = await this.prisma.profile.create({
data: {
value: profile.value,
description: profile.description
},
});
return new newProfile(created.value, created.description);
}

async update(profile: Profile): Promise<Profile> {


const updated = await this.prisma.profile.update({
where: { id: profile.id },
data: {
value: profile.value,
description: profile.description
},
});
return new Profile(updated.id, updated.value, updated.description);
}

async delete(id: number): Promise<void> {


await this.prisma.profile.delete({ where: { id } });
}

async findAll(): Promise<Profile[]> {


const profiles = await this.prisma.profile.findMany();
return profiles.map(profile => new Profile(profile.id, profile.value,
profile.description));
}

async findById(id: number): Promise<Profile | null> {


const profile = await this.prisma.profile.findUnique({ where: { id } });
if (!profile) return null;
return new Profile(profile.id, profile.value, profile.description)
}

async findByValue(value: string): Promise<Profile | null> {


const profile = await this.prisma.profile.findUnique({ where: { value } });
if (!profile) return null;

return new Profile(profile.id, profile.value, profile.description)


}
}
APP.MODULE
import { Module } from '@nestjs/common';
import { PrismaModule } from './Infrastructure/Prisma/prisma.module';

import { AccountStatusModule } from './Infrastructure/Modules/Assist/account-


status.module';
import { InterestModule } from './Infrastructure/Modules/Assist/interest.module';
import { LanguageModule } from
'./Infrastructure/Modules/Assist/language.module';
import { PrivacyModule } from './Infrastructure/Modules/Assist/privacy.module';
import { ProfileModule } from './Infrastructure/Modules/Assist/profile.module';
import { ReputationModule } from
'./Infrastructure/Modules/Assist/reputation.module';
import { RequestStatusModule } from './Infrastructure/Modules/Assist/request-
status.module';
import { RoleModule } from './Infrastructure/Modules/Assist/role.module';
import { StyleModule } from './Infrastructure/Modules/Assist/style.module';

import { CityModule } from './Infrastructure/Modules/Local/city.module';


import { CountryModule } from './Infrastructure/Modules/Local/country.module';
import { RegionModule } from './Infrastructure/Modules/Local/region.module';

import { UserModule } from './Infrastructure/Modules/User/user.module';

@Module({
imports: [
PrismaModule,

AccountStatusModule, InterestModule, LanguageModule, PrivacyModule,


ProfileModule, ReputationModule, RequestStatusModule, RoleModule,
StyleModule,
CityModule, CountryModule, RegionModule,

UserModule,

],
controllers: [],
providers: [],
})
export class AppModule { }

You might also like