# lucasblackarken/locadoradecarro architecture

> Architecture diagram of the GitHub repository lucasblackarken/locadoradecarro, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-07). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/lucasblackarken/locadoradecarro
- Repository: https://github.com/lucasblackarken/locadoradecarro

## Overview

##### Project type and purpose
- This is a backend-only RESTful API for a car-rental system built with ASP.NET Core 8.0.
- It follows Domain-Driven Design (DDD), CQRS + MediatR, FluentValidation, Entity Framework Core, and uses IdentityServer4 for authentication (Client Credentials).
- It exposes endpoints to register cars, list available/unavailable cars, rent cars, return cars (with late fees), and manage rentals.

A. Presentation/API Layer
- CarrosController
- AluguelController
B. Application Layer (CQRS + MediatR)
- Commands & CommandValidators (FluentValidation)
- Queries
- CommandHandlers & QueryHandlers (MediatR handlers)
C. Domain Layer
- Entities (Carro, Aluguel, Cliente)
- DTOs (CarroDTO, AluguelDTO)
- Repository interfaces (ICarroRepository, IAluguelRepository)
D. Infrastructure Layer
- EF Core DbContext (AppDbContext)
- Repository implementations (CarroRepository, AluguelRepository)
- Migrations
E. Cross-cutting / Support
- IdentityServer4 (Auth Server – Client Credentials flow)
- AutoMapper (mapping between Entities↔DTOs)
- Utils
F. Data Store & External Services
- SQL Server database
- (Optional) Logging, Monitoring, Message bus, etc.

1. Client → API Controllers (HTTP + Bearer token from IdentityServer)
2. Controller → MediatR.Send(command/query)
3. MediatR Pipeline
- ValidationBehavior → FluentValidation validators
- Logging/AuditBehavior (if any)
- Handler execution
4. Handler → Domain/Repository interface → Infrastructure repository
5. Repository → AppDbContext → SQL Server
6. Handler → Constructs DTO → returns to Controller → serializes JSON → Client

##### Architectural patterns & design principles
- DDD: Separation of Domain, Application, Infrastructure, Presentation projects.
- CQRS: Commands (state-changing) vs. Queries (read-only).
- Mediator: Decouples sending requests from handling logic.
- FluentValidation: Pre-handler input validation pipeline.
- Repository pattern + EF Core: Abstract data access behind interfaces.
- SOLID & Clean Code: Single Responsibility, Dependency Inversion via interfaces & DI.

##### Detailed elements
- Layer names and key classes (e.g., AluguelCarroCommandHandler).
- Data flow: Command/Query → Handler → Database → DTO → Response.
- Authentication flow: Client credentials → IdentityServer → JWT → API.
- EF Core migrations block under Infrastructure.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% External
    Client["Client"]:::external
    IdentityServer4["IdentityServer4\n(Auth Server)"]:::external

    %% Presentation Layer
    subgraph "Presentation / API Layer"
        direction TB
        CarrosController["CarrosController"]:::presentation
        AluguelController["AluguelController"]:::presentation
    end

    %% Application Layer
    subgraph "Application Layer\n(CQRS + MediatR)"
        direction TB
        subgraph Commands
            direction TB
            CadastrarCarroCommand["CadastrarCarroCommand"]:::application
            AluguelCarroCommand["AluguelCarroCommand"]:::application
            DevolverCarroCommand["DevolverCarroCommand"]:::application
        end
        subgraph Queries
            direction TB
            ListarCarrosDisponiveisQuery["ListarCarrosDisponiveisQuery"]:::application
            ListarCarrosAlugadosQuery["ListarCarrosAlugadosQuery"]:::application
            ObterCarroPorIdQuery["ObterCarroPorIdQuery"]:::application
            ListarTodosAlugueisQuery["ListarTodosAlugueisQuery"]:::application
            ObterAluguelPorIdQuery["ObterAluguelPorIdQuery"]:::application
        end
        subgraph Validators
            direction TB
            CadastrarCarroCommandValidator["CadastrarCarroCommandValidator"]:::application
            AluguelCarroCommandValidator["AluguelCarroCommandValidator"]:::application
            DevolverCarroCommandValidator["DevolverCarroCommandValidator"]:::application
        end
        subgraph Handlers
            direction TB
            CadastrarCarroCommandHandler["CadastrarCarroCommandHandler"]:::application
            ListarCarrosDisponiveisQueryHandler["ListarCarrosDisponiveisQueryHandler"]:::application
            ListarCarrosAlugadosQueryHandler["ListarCarrosAlugadosQueryHandler"]:::application
            ObterCarroPorIdQueryHandler["ObterCarroPorIdQueryHandler"]:::application
            AluguelCarroCommandHandler["AluguelCarroCommandHandler"]:::application
            DevolverCarroCommandHandler["DevolverCarroCommandHandler"]:::application
            ListarTodosAlugueisQueryHandler["ListarTodosAlugueisQueryHandler"]:::application
            ObterAluguelPorIdQueryHandler["ObterAluguelPorIdQueryHandler"]:::application
        end
    end

    %% Domain Layer
    subgraph "Domain Layer"
        direction TB
        Entities["Entities\n(Carro, Aluguel, Cliente)"]:::domain
        DTOs["DTOs\n(CarroDTO, AluguelDTO)"]:::domain
        Interfaces["Repository Interfaces\n(ICarroRepository, IAluguelRepository)"]:::domain
    end

    %% Infrastructure Layer
    subgraph "Infrastructure Layer"
        direction TB
        AppDbContext["AppDbContext"]:::infra
        RepositoryImpl["Repository Implementations\n(CarroRepository, AluguelRepository)"]:::infra
        Migrations["Migrations"]:::infra
        SQLServer["SQL Server"]:::infra
    end

    %% Cross-cutting / Support
    subgraph "Cross-cutting / Support"
        direction TB
        AutoMapper["AutoMapper"]:::crosscut
        Utils["Utils"]:::crosscut
    end

    %% Connections
    Client -->|HTTP + Bearer| CarrosController
    Client -->|HTTP + Bearer| AluguelController
    CarrosController -->|MediatR.Send| CadastrarCarroCommand
    CarrosController -->|MediatR.Send| ListarCarrosDisponiveisQuery
    CarrosController -->|MediatR.Send| ListarCarrosAlugadosQuery
    CarrosController -->|MediatR.Send| ObterCarroPorIdQuery

    AluguelController -->|MediatR.Send| AluguelCarroCommand
    AluguelController -->|MediatR.Send| DevolverCarroCommand
    AluguelController -->|MediatR.Send| ListarTodosAlugueisQuery
    AluguelController -->|MediatR.Send| ObterAluguelPorIdQuery

    CarrosController -.->|Validate Token| IdentityServer4
    AluguelController -.->|Validate Token| IdentityServer4

    %% Validation and Handling Pipeline
    CadastrarCarroCommand -->|Validate| CadastrarCarroCommandValidator -->|Handle| CadastrarCarroCommandHandler
    AluguelCarroCommand -->|Validate| AluguelCarroCommandValidator -->|Handle| AluguelCarroCommandHandler
    DevolverCarroCommand -->|Validate| DevolverCarroCommandValidator -->|Handle| DevolverCarroCommandHandler

    ListarCarrosDisponiveisQuery -->|Handle| ListarCarrosDisponiveisQueryHandler
    ListarCarrosAlugadosQuery -->|Handle| ListarCarrosAlugadosQueryHandler
    ObterCarroPorIdQuery -->|Handle| ObterCarroPorIdQueryHandler
    ListarTodosAlugueisQuery -->|Handle| ListarTodosAlugueisQueryHandler
    ObterAluguelPorIdQuery -->|Handle| ObterAluguelPorIdQueryHandler

    %% Domain interactions
    CadastrarCarroCommandHandler -->|Uses| Interfaces
    ListarCarrosDisponiveisQueryHandler -->|Uses| Interfaces
    ListarCarrosAlugadosQueryHandler -->|Uses| Interfaces
    ObterCarroPorIdQueryHandler -->|Uses| Interfaces
    AluguelCarroCommandHandler -->|Uses| Interfaces
    DevolverCarroCommandHandler -->|Uses| Interfaces
    ListarTodosAlugueisQueryHandler -->|Uses| Interfaces
    ObterAluguelPorIdQueryHandler -->|Uses| Interfaces

    %% Infrastructure interactions
    Interfaces -->|Implemented by| RepositoryImpl
    RepositoryImpl -->|Uses| AppDbContext
    AppDbContext -->|Migrations| Migrations
    AppDbContext -->|EF Core queries| SQLServer

    %% Mapping
    AutoMapper -->|Maps Entities↔DTOs| Entities
    AutoMapper -->|Maps Entities↔DTOs| DTOs

    %% Utils
    RepositoryImpl -->|Helpers| Utils

    %% Click Events
    click CarrosController "https://github.com/lucasblackarken/locadoradecarro/blob/master/API/Controllers/CarrosController.cs"
    click AluguelController "https://github.com/lucasblackarken/locadoradecarro/blob/master/API/Controllers/AluguelController.cs"
    click CadastrarCarroCommand "https://github.com/lucasblackarken/locadoradecarro/blob/master/Application/Commands/Carro/CadastrarCarroCommand.cs"
    click AluguelCarroCommand "https://github.com/lucasblackarken/locadoradecarro/blob/master/Application/Commands/Aluguel/AluguelCarroCommand.cs"
    click DevolverCarroCommand "https://github.com/lucasblackarken/locadoradecarro/blob/master/Application/Commands/Aluguel/DevolverCarroCommand.cs"
    click ListarCarrosDisponiveisQuery "https://github.com/lucasblackarken/locadoradecarro/blob/master/Application/Query/Carro/ListarCarrosDisponiveisQuery.cs"
    click ListarCarrosAlugadosQuery "https://github.com/lucasblackarken/locadoradecarro/blob/master/Application/Query/Carro/ListarCarrosAlugadosQuery.cs"
    click ObterCarroPorIdQuery "https://github.com/lucasblackarken/locadoradecarro/blob/master/Application/Query/Carro/ObterCarroPorIdQuery.cs"
    click ListarTodosAlugueisQuery "https://github.com/lucasblackarken/locadoradecarro/blob/master/Application/Query/Aluguel/ListarTodosAlugueisQuery.cs"
    click ObterAluguelPorIdQuery "https://github.com/lucasblackarken/locadoradecarro/blob/master/Application/Query/Aluguel/ObterAluguelPorIdQuery.cs"
    click CadastrarCarroCommandHandler "https://github.com/lucasblackarken/locadoradecarro/blob/master/Application/Handler/Carro/CadastrarCarroCommandHandler.cs"
    click ListarCarrosDisponiveisQueryHandler "https://github.com/lucasblackarken/locadoradecarro/blob/master/Application/Handler/Carro/ListarCarrosDisponiveisQueryHandler.cs"
    click ListarCarrosAlugadosQueryHandler "https://github.com/lucasblackarken/locadoradecarro/blob/master/Application/Handler/Carro/ListarCarrosAlugadosQueryHandler.cs"
    click ObterCarroPorIdQueryHandler "https://github.com/lucasblackarken/locadoradecarro/blob/master/Application/Handler/Carro/ObterCarroPorIdQueryHandler.cs"
    click AluguelCarroCommandHandler "https://github.com/lucasblackarken/locadoradecarro/blob/master/Application/Handler/Aluguel/AluguelCarroCommandHandler.cs"
    click DevolverCarroCommandHandler "https://github.com/lucasblackarken/locadoradecarro/blob/master/Application/Handler/Aluguel/DevolverCarroCommandHandler.cs"
    click ListarTodosAlugueisQueryHandler "https://github.com/lucasblackarken/locadoradecarro/blob/master/Application/Handler/Aluguel/ListarTodosAlugueisQueryHandler.cs"
    click ObterAluguelPorIdQueryHandler "https://github.com/lucasblackarken/locadoradecarro/blob/master/Application/Handler/Aluguel/ObterAluguelPorIdQueryHandler.cs"
    click CadastrarCarroCommandValidator "https://github.com/lucasblackarken/locadoradecarro/blob/master/Application/Validator/Carro/CadastrarCarroCommandValidator.cs"
    click AluguelCarroCommandValidator "https://github.com/lucasblackarken/locadoradecarro/blob/master/Application/Validator/Aluguel/AluguelCarroCommandValidator.cs"
    click DevolverCarroCommandValidator "https://github.com/lucasblackarken/locadoradecarro/blob/master/Application/Validator/Aluguel/DevolverCarroCommandValidator.cs"
    click Entities "https://github.com/lucasblackarken/locadoradecarro/blob/master/Domain/Entities/Carro.cs"
    click DTOs "https://github.com/lucasblackarken/locadoradecarro/blob/master/Domain/DTO/CarroDTO.cs"
    click Interfaces "https://github.com/lucasblackarken/locadoradecarro/blob/master/Domain/Interfaces/ICarroRepository.cs"
    click AppDbContext "https://github.com/lucasblackarken/locadoradecarro/blob/master/Infrastructure/Context/AppDbContext.cs"
    click RepositoryImpl "https://github.com/lucasblackarken/locadoradecarro/blob/master/Domain/Repository/CarroRepository.cs"
    click Migrations "https://github.com/lucasblackarken/locadoradecarro/tree/master/Migrations/"
    click IdentityServer4 "https://github.com/lucasblackarken/locadoradecarro/blob/master/IdentityServer/Config.cs"

    %% Styles
    classDef external fill:#D2B4DE,stroke:#873600,color:#000
    classDef presentation fill:#AED6F1,stroke:#1B4F72,color:#000
    classDef application fill:#A9DFBF,stroke:#196F3D,color:#000
    classDef domain fill:#F9E79F,stroke:#B9770E,color:#000
    classDef infra fill:#D5D8DC,stroke:#424949,color:#000
    classDef crosscut fill:#F5B7B1,stroke:#78281F,color:#000
```
