Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CHORE] improvements for transport layer documentation #20

Merged
merged 2 commits into from
May 19, 2024
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
2 changes: 1 addition & 1 deletion src/config/client.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ClientWriterService } from '@/domain/interactor/client/client-writer.se
import { ClientReaderServicePort } from '@/domain/interactor/port/client/client-reader-service.port';
import { ClientWriterServicePort } from '@/domain/interactor/port/client/client-writer-service.port';
import { IClientRepository } from '@/domain/repository/client/client.repository';
import { ClientController } from '@/transport/controller/client/client.controller';
import { ClientController } from '@/transport/controller/client.controller';
import { Module } from '@nestjs/common';
import { PrismaService } from './prisma.config';

Expand Down
2 changes: 1 addition & 1 deletion src/config/payment.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { PaymentWriterService } from '@/domain/interactor/payment/payment-writer
import { PaymentWriterServicePort } from '@/domain/interactor/port/payment/payment-writer-service.port';
import { IPaymentRepository } from '@/domain/repository/payment/payment.repository';
import { PrismaPaymentRepository } from '@/infrastructure/persistence/prisma/prisma-payment.repository';
import { PaymentController } from '@/transport/controller/payment/payment.controller';
import { PaymentController } from '@/transport/controller/payment.controller';
import { Module } from '@nestjs/common';

@Module({
Expand Down
2 changes: 1 addition & 1 deletion src/config/product.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ProductInMemoryAdapter } from '@/datasource/adapter/product-in-memory.a
import { ProductReaderServicePort } from '@/domain/interactor/port/product/product-reader-service.port';
import { ProductService } from '@/domain/interactor/product/product.service';
import { IProductRepository } from '@/domain/repository/product/product.repository';
import { ProductController } from '@/transport/controller/product/product.controller';
import { ProductController } from '@/transport/controller/product.controller';
import { Module } from '@nestjs/common';

@Module({
Expand Down
4 changes: 2 additions & 2 deletions src/datasource/adapter/client/client-postgre.adapter.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { PrismaService } from '@/config/prisma.config';
import { Client } from '@/domain/entity/client/client.entity';
import { IClientRepository } from '@/domain/repository/client/client.repository';
import { CreateClientDto } from '@/transport/dto/Client/create-client.dto';
import { UpdateClientDto } from '@/transport/dto/Client/update-client.dto';
import { CreateClientDto } from '@/transport/dto/client/client.dto';
import { UpdateClientDto } from '@/transport/dto/client/update-client.dto';
import { Injectable } from '@nestjs/common';

@Injectable()
Expand Down
11 changes: 5 additions & 6 deletions src/domain/interactor/client/client-writer.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Client } from '@/domain/entity/client/client.entity';
import { BadRequestException } from '@/config/exceptions/custom-exceptions/bad-request.exception';
import { ClientWriterService } from '@/domain/interactor/client/client-writer.service';
import { IClientRepository } from '@/domain/repository/client/client.repository';
import { PreconditionFailedException } from '@nestjs/common';

describe('ClientWriterService', () => {
let service: ClientWriterService;
Expand Down Expand Up @@ -40,15 +39,15 @@ describe('ClientWriterService', () => {
expect(clientRepository.create).toHaveBeenCalledWith(client);
});

it('should throw PreconditionFailedException for an invalid client', async () => {
it('should throw BadRequestException for an invalid client', async () => {
const client = {
id: '1',
name: '',
email: '',
cpf: '',
};

await expect(service.create(client)).rejects.toThrow(PreconditionFailedException);
await expect(service.create(client)).rejects.toThrow(BadRequestException);
});
});

Expand All @@ -75,7 +74,7 @@ describe('ClientWriterService', () => {
expect(clientRepository.update).toHaveBeenCalledWith(updatedClient);
});

it('should throw PreconditionFailedException for an invalid updated client', async () => {
it('should throw BadRequestException for an invalid updated client', async () => {
const existingClient = {
id: '1',
name: 'Gandalf The Grey',
Expand All @@ -93,7 +92,7 @@ describe('ClientWriterService', () => {
jest.spyOn(clientRepository, 'findById').mockResolvedValue(existingClient);

await expect(service.update(updatedClient)).rejects.toThrow(
PreconditionFailedException,
BadRequestException,
);

});
Expand Down
11 changes: 6 additions & 5 deletions src/domain/interactor/client/client-writer.service.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { BadRequestException } from '@/config/exceptions/custom-exceptions/bad-request.exception';
import { NotFoundException } from '@/config/exceptions/custom-exceptions/not-found.exception';
import { Client } from '@/domain/entity/client/client.entity';
import { ClientWriterServicePort } from '@/domain/interactor/port/client/client-writer-service.port';
import { IClientRepository } from '@/domain/repository/client/client.repository';
import { CreateClientDto } from '@/transport/dto/Client/create-client.dto';
import { UpdateClientDto } from '@/transport/dto/Client/update-client.dto';
import { PreconditionFailedException } from '@nestjs/common';
import { CreateClientDto } from '@/transport/dto/client/client.dto';
import { UpdateClientDto } from '@/transport/dto/client/update-client.dto';


export class ClientWriterService implements ClientWriterServicePort {
constructor(private readonly clientRepository: IClientRepository) {}
constructor(private readonly clientRepository: IClientRepository) { }

async create(createClientDTO: CreateClientDto): Promise<Client> {
const client = new Client(
Expand All @@ -27,7 +28,7 @@ export class ClientWriterService implements ClientWriterServicePort {

async update(client: UpdateClientDto): Promise<Client> {
const savedClient = await this.clientRepository.findById(client.id).catch((e) => {
throw new PreconditionFailedException('Client not found');
throw new NotFoundException({ description: 'Client not found' });
});

const editedClient = new Client(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Payment } from '@/domain/entity/payment/payment.entity';
import { PaymentWriterService } from '@/domain/interactor/payment/payment-writer.service';
import { IPaymentRepository } from '@/domain/repository/payment/payment.repository';
import { CreatePaymentDto } from '@/transport/dto/Payment/payment.dto';
import { CreatePaymentDto } from '@/transport/dto/payment/request/payment.dto';
import { HttpException } from '@nestjs/common';

describe('PaymentService', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/domain/interactor/payment/payment-writer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { InternalServerErrorException } from '@/config/exceptions/custom-excepti
import { Payment } from '@/domain/entity/payment/payment.entity';
import { PaymentWriterServicePort } from '@/domain/interactor/port/payment/payment-writer-service.port';
import { IPaymentRepository } from '@/domain/repository/payment/payment.repository';
import { CreatePaymentDto } from '@/transport/dto/Payment/payment.dto';
import { CreatePaymentDto } from '@/transport/dto/payment/request/payment.dto';

export class PaymentWriterService implements PaymentWriterServicePort {
constructor(private readonly paymentRepository: IPaymentRepository) {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Client } from '@/domain/entity/client/client.entity';
import { CreateClientDto } from '@/transport/dto/Client/create-client.dto';
import { UpdateClientDto } from '@/transport/dto/Client/update-client.dto';
import { CreateClientDto } from '@/transport/dto/client/client.dto';
import { UpdateClientDto } from '@/transport/dto/client/update-client.dto';

export abstract class ClientWriterServicePort {
abstract create(client: CreateClientDto): Promise<Client>;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Payment } from '@/domain/entity/payment/payment.entity';
import { CreatePaymentDto } from '@/transport/dto/Payment/payment.dto';
import { CreatePaymentDto } from '@/transport/dto/payment/request/payment.dto';

export abstract class PaymentWriterServicePort {
abstract create(payment: CreatePaymentDto): Promise<Payment>;
Expand Down
4 changes: 2 additions & 2 deletions src/domain/repository/client/client.repository.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Client } from '@/domain/entity/client/client.entity';
import { CreateClientDto } from '@/transport/dto/Client/create-client.dto';
import { UpdateClientDto } from '@/transport/dto/Client/update-client.dto';
import { CreateClientDto } from '@/transport/dto/client/client.dto';
import { UpdateClientDto } from '@/transport/dto/client/update-client.dto';

export abstract class IClientRepository {
abstract create(client: CreateClientDto): Promise<Client>;
Expand Down
2 changes: 1 addition & 1 deletion src/domain/repository/payment/payment.repository.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Payment } from '@/domain/entity/payment/payment.entity';
import { CreatePaymentDto } from '@/transport/dto/Payment/payment.dto';
import { CreatePaymentDto } from '@/transport/dto/payment/request/payment.dto';

export abstract class IPaymentRepository {
abstract create(payment: CreatePaymentDto): Promise<Payment>;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { PrismaService } from '@/config/prisma.config';
import { Payment } from '@/domain/entity/payment/payment.entity';
import { IPaymentRepository } from '@/domain/repository/payment/payment.repository';
import { CreatePaymentDto } from '@/transport/dto/Payment/payment.dto';
import { CreatePaymentDto } from '@/transport/dto/payment/request/payment.dto';
import { Injectable } from '@nestjs/common';

@Injectable()
Expand Down
7 changes: 7 additions & 0 deletions src/transport/constant/api-response.constant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const API_RESPONSE = {
OK_DESC: 'Successul request',
CREATED_DESC: 'Register was created successfully',
BAD_REQUEST_DESC: 'Invalid body request',
NOT_FOUND_DESC: 'A register for this identifier was not found',
INTERNAL_SERVER_EXCEPTION_DESC: 'Internal Server Error',
};
38 changes: 38 additions & 0 deletions src/transport/constant/client.constant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
export const CLIENT = {
API_PROPERTY: {
CREATE: {
SUMMARY: 'Creates a client register',
DESC: 'Creates a client register in database',
},
GET_ALL: {
SUMMARY: 'Gets all registered clients in database',
DESC: 'Fetches all clients registered in database',
},
GET_BY_ID: {
SUMMARY: 'Gets client by identifier',
DESC: 'Fetches client register from database by identifier',
},
UPDATE: {
SUMMARY: 'Updates client register',
DESC: 'Updates client register in database by identifier',
},
CLIENT: {
ID: {
DESC: 'Client identifier in database',
EXAMPLE: '5671843b-324b-40ae-aaa8-a3b404013703',
},
CPF: {
DESC: 'Client unique Cadastro de Pessoa física (CPF)',
EXAMPLE: '17256987564',
},
NAME: {
DESC: 'Client name',
EXAMPLE: 'Bilbo Baggins',
},
EMAIL: {
DESC: 'Client email',
EXAMPLE: '[email protected]',
},
},
},
};
22 changes: 22 additions & 0 deletions src/transport/constant/payment.constant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export const PAYMENT = {
API_PROPERTY: {
CREATE: {
SUMMARY: 'Creates a payment register',
DESC: 'Creates a payment register attached to an order in database',
},
PRODUCT: {
ID: {
DESC: 'Product identifier in database',
EXAMPLE: '5671843b-324b-40ae-aaa8-a3b404013703',
},
VALUE: {
DESC: 'Order value',
EXAMPLE: 39.99,
},
METHOD: {
DESC: 'Category in which the product is registered',
EXAMPLE: 'Credit Card',
},
},
},
};
90 changes: 90 additions & 0 deletions src/transport/controller/client.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { BadRequestException } from '@/config/exceptions/custom-exceptions/bad-request.exception';
import { InternalServerErrorException } from '@/config/exceptions/custom-exceptions/internal-server-error.exception';
import { NotFoundException } from '@/config/exceptions/custom-exceptions/not-found.exception';
import { Client } from '@/domain/entity/client/client.entity';
import { ClientReaderServicePort } from '@/domain/interactor/port/client/client-reader-service.port';
import { ClientWriterServicePort } from '@/domain/interactor/port/client/client-writer-service.port';
import { API_RESPONSE } from '@/transport/constant/api-response.constant';
import { CLIENT } from '@/transport/constant/client.constant';
import { CreateClientDto } from '@/transport/dto/client/client.dto';
import { UpdateClientDto } from '@/transport/dto/client/update-client.dto';
import { Body, Controller, Get, HttpStatus, Param, Patch, Post } from '@nestjs/common';
import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';

const { CREATE, GET_ALL, GET_BY_ID, UPDATE } = CLIENT.API_PROPERTY;
const {
OK_DESC,
CREATED_DESC,
BAD_REQUEST_DESC,
NOT_FOUND_DESC,
INTERNAL_SERVER_EXCEPTION_DESC,
} = API_RESPONSE;

@Controller('clients')
@ApiTags('clients')
export class ClientController {
constructor(
private readonly clientWriterService: ClientWriterServicePort,
private readonly clientReaderService: ClientReaderServicePort,
) {}

@Post()
@ApiOperation({ summary: CREATE.SUMMARY, description: CREATE.DESC })
@ApiResponse({
status: HttpStatus.CREATED,
description: CREATED_DESC,
type: () => Client,
})
@ApiResponse({
status: HttpStatus.BAD_REQUEST,
description: BAD_REQUEST_DESC,
type: () => BadRequestException,
})
async create(@Body() createClient: CreateClientDto) {
return this.clientWriterService.create({
...createClient,
});
}

@Get()
@ApiOperation({ summary: GET_ALL.SUMMARY, description: GET_ALL.DESC })
@ApiResponse({
status: HttpStatus.OK,
description: OK_DESC,
type: () => Client,
})
@ApiResponse({
status: HttpStatus.INTERNAL_SERVER_ERROR,
description: INTERNAL_SERVER_EXCEPTION_DESC,
type: () => InternalServerErrorException,
})
async findAll() {
return this.clientReaderService.findAll();
}

@Get(':id')
@ApiOperation({ summary: GET_BY_ID.SUMMARY, description: GET_BY_ID.DESC })
@ApiResponse({
status: HttpStatus.OK,
description: OK_DESC,
type: () => Client,
})
@ApiResponse({
status: HttpStatus.NOT_FOUND,
description: NOT_FOUND_DESC,
type: () => NotFoundException,
})
async findById(@Param('id') id: string) {
return this.clientReaderService.findById(id);
}

@Patch(':id')
@ApiOperation({ summary: UPDATE.SUMMARY, description: UPDATE.DESC })
@ApiResponse({ status: HttpStatus.OK, description: OK_DESC, type: () => Client })
async update(@Body() editClient: UpdateClientDto, @Param('id') id: string) {
return this.clientWriterService.update({
...editClient,
id: id,
});
}
}
67 changes: 0 additions & 67 deletions src/transport/controller/client/client.controller.ts

This file was deleted.

Loading
Loading