O módulo de Produtos oferece um CRUD completo para gerenciamento de produtos, com paginação eficiente, validações robustas e integração com DynamoDB.
┌─────────────────────┐ ┌─────────────────────┐ ┌─────────────────────┐
│ ProductsController │ │ ProductsService │ │ ProductsRepository │
│ │ │ │ │ │
│ • Route Handling │────│ • Business Logic │────│ • Data Access │
│ • Validation │ │ • Orchestration │ │ • ElectroDB │
│ • Swagger Docs │ │ • Error Handling │ │ • DynamoDB Ops │
│ • Auth Guards │ │ • Data Transform │ │ • Query Optimization│
└─────────────────────┘ └─────────────────────┘ └─────────────────────┘
interface IProduct {
productId: string;
title: string;
description: string;
price: number;
publishDate: string;
photoLink: string;
}
export const Product = new Entity({
model: {
entity: 'product',
version: '1',
service: 'ton-service',
},
attributes: {
productId: { type: 'string', required: true },
title: { type: 'string', required: true },
description: { type: 'string', required: true },
price: { type: 'number', required: true },
publishDate: { type: 'string', required: true },
photoLink: { type: 'string', required: true },
},
indexes: {
primary: {
pk: { field: 'pk', composite: ['productId'] },
sk: { field: 'sk', composite: [] },
},
},
});
Endpoint: GET /products
Features:
Implementação:
async findAll(limit: number, nextKey?: string): Promise<IPaginatedProducts> {
const result = await Product.scan.go({
limit,
cursor: nextKey || null,
});
return {
products: result.data,
nextKey: result.cursor || null,
};
}
Query Parameters:
limit: 1-100 (default: 10)nextKey: Cursor da página anteriorEndpoint: GET /products/:id
Features:
Implementação:
async findById(productId: string): Promise<IProduct> {
const result = await Product.get({ productId }).go();
if (!result.data) {
throw new NotFoundException(`Product with ID ${productId} not found`);
}
return result.data;
}
Endpoint: POST /products
Features:
Validações:
export class CreateProductDto {
@IsString()
@IsNotEmpty()
@MaxLength(200)
title: string;
@IsString()
@IsNotEmpty()
@MaxLength(1000)
description: string;
@IsNumber()
@Min(0.01)
@Max(999999.99)
price: number;
@IsISO8601()
publishDate: string;
@IsUrl()
photoLink: string;
}
Endpoint: PUT /products/:id
Features:
Implementação:
async update(productId: string, updateData: UpdateProductDto): Promise<IProduct> {
// Verifica se produto existe
await this.findById(productId);
const { data } = await Product.update({ productId })
.set(updateData)
.go({ response: 'updated_new' });
return data as IProduct;
}
Endpoint: DELETE /products/:id
Features: