ton-api

Arquitetura

Visão Geral

A Ton Products API segue uma arquitetura em camadas baseada nos princípios de Clean Architecture e Domain-Driven Design, garantindo separação de responsabilidades e alta testabilidade.

Diagrama de Arquitetura

┌─────────────────┐    ┌─────────────────┐    ┌─────────────────┐
│   Controller    │────│    Service      │────│   Repository    │
│                 │    │                 │    │                 │
│ • Validation    │    │ • Business      │    │ • Data Access   │
│ • HTTP Handling │    │   Logic         │    │ • ElectroDB     │
│ • Auth Guards   │    │ • Orchestration │    │ • DynamoDB      │
└─────────────────┘    └─────────────────┘    └─────────────────┘
         │                       │                       │
         │              ┌─────────────────┐              │
         └──────────────│   DTOs/Models   │──────────────┘
                        │                 │
                        │ • Interfaces    │
                        │ • Validation    │
                        │ • Type Safety   │
                        └─────────────────┘

Camadas da Aplicação

1. Controller Layer

@Controller('products')
@UseGuards(JwtAuthGuard)
export class ProductsController {
  async findAll(@Query() query: FindAllQueryDto) {
    return await this.productsService.findAll(query);
  }
}

2. Service Layer

@Injectable()
export class ProductsService {
  constructor(private readonly repository: ProductsRepository) {}
  
  async findAll(query: FindAllQueryDto): Promise<PaginatedResult> {
    // Business logic here
    return await this.repository.findAll(query);
  }
}

3. Repository Layer

@Injectable()
export class ProductsRepository {
  async findAll(limit: number, nextKey?: string) {
    return await Product.scan.go({ limit, cursor: nextKey });
  }
}

Padrões Arquiteturais

1. Dependency Injection

2. Repository Pattern

3. DTO Pattern

4. Guard Pattern

Módulos da Aplicação

Auth Module

@Module({
  imports: [JwtModule, UsersModule],
  controllers: [AuthController],
  providers: [AuthService, EncryptionService],
  exports: [AuthService],
})
export class AuthModule {}

Products Module

@Module({
  controllers: [ProductsController],
  providers: [ProductsService, ProductsRepository],
  exports: [ProductsService],
})
export class ProductsModule {}

Users Module

@Module({
  controllers: [],
  providers: [UsersService, UsersRepository],
  exports: [UsersService],
})
export class UsersModule {}

Segurança

Autenticação JWT

Criptografia

Validação

Banco de Dados

DynamoDB Design

Entidades

Table

PK: USER#{userId}
SK: PROFILE
GSI1PK: EMAIL#{email}
GSI1SK: PROFILE

PK: PRODUCT#{productId}
SK: METADATA

Testabilidade

Testes Unitários

Fluxo de Request

  1. HTTP Request → Controller
  2. Validation → DTO Validation
  3. Authentication → JWT Guard
  4. Business Logic → Service Layer
  5. Data Access → Repository Layer
  6. Database → DynamoDB
  7. Response → JSON/Error Handling

Escalabilidade

Horizontal Scaling

Performance

Monitoring