# mayumisiano/nlw-connect-devstage architecture

> Architecture diagram of the GitHub repository mayumisiano/nlw-connect-devstage, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-09). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mayumisiano/nlw-connect-devstage
- Repository: https://github.com/mayumisiano/nlw-connect-devstage

## Overview

##### Main Components
- API Layer: Represented by the TechLibrary.Api project. This is where the Controllers (BooksController, CheckoutController, LoginController, UserController) directly expose the endpoints.
- Domain Layer: Found under TechLibrary.Api/Domain/Entities. Include the key entities (Book, Checkout, User) which encapsulate the core business data.
- Use Cases / Application Layer: The TechLibrary.Api/UseCases folder contains business logic for specific actions (filtering books, registering checkouts, handling login, and user registration). These are key application services orchestrating interactions between layers.
- Infrastructure Layer:
- Data Access: Under TechLibrary.Api/Infrastructure/DataAccess, which contains the TechLibraryDbContext (likely using an ORM such as Entity Framework).
- Security: Under TechLibrary.Api/Infrastructure/Security, which is split into Cryptography (e.g., BCryptAlgorithm) and Token management (JwtTokenGenerator). This shows dedicated security mechanisms for both password management and token-based authentication.
- Services: For example, the LoggedUserService under TechLibrary.Api/Services/LoggedUser encapsulates logic related to currently authenticated users.
- Communication Layer: The TechLibrary.Communication project holds definitions for request and response models. This serves as the DTO (Data Transfer Object) layer to standardize how data is sent and received.
- Exception Handling: The TechLibrary.Exceptions project defines custom exception types (ConflictException, NotFoundException, InvalidLoginException, etc.) used across the system.
- Configuration & Startup: Files like Program.cs, appsettings.json, and launchSettings.json indicate application configuration and startup routines.

##### Out Relationships and Interactions
- From Controllers to Use Cases.
- From Use Cases to the Domain Entities.
- From Use Cases to the DataAccess component (TechLibraryDbContext) when persisting or retrieving data.
- From Use Cases to Security components when performing authentication and token generation.
- The Communication project (with its Request/Response models) connects to the API layer, as these models define the contract over which Controllers exchange data.
- The Custom Exceptions can be represented as an intersecting utility used across the Use Cases and API layers to handle error scenarios gracefully.
- Optionally, include a configuration node representing appsettings to indicate externalized configuration and environment-based setups.

##### Architectural Patterns and Technologies
- The API is structured using the MVC-inspired pattern (Controllers, Use Cases, Domain) even though it is clearly an API back-end.
- The separation into Use Cases and Domain Entities suggests a Clean Architecture or Onion Architecture approach, where application logic (use cases) is decoupled from both the presentation (controllers) and infrastructure details.
- Security is handled using modular components (BCrypt for cryptographic hashing and JWT for token generation), emphasizing separation of security concerns.
- Data Access is isolated within its own namespace making it easier to swap out or modify the persistence style if needed.

## Diagram (Mermaid)

```mermaid
graph TD
    %% Configuration & Startup
    subgraph "Configuration & Startup"
        Prog["Program.cs"]:::config
        App["appsettings.json"]:::config
        LS["launchSettings.json"]:::config
    end

    %% API Layer
    subgraph "API Layer"
        Books["BooksController"]:::api
        Checkout["CheckoutController"]:::api
        Login["LoginController"]:::api
        User["UserController"]:::api
    end

    %% Communication Layer
    subgraph "Communication Layer"
        Req["Requests DTOs"]:::comm
        Res["Responses DTOs"]:::comm
    end

    %% Use Cases / Application Layer
    subgraph "Use Cases / Application Layer"
        FB["FilterBookUseCase"]:::usecases
        CB["RegisterBookCheckoutUseCase"]:::usecases
        DL["DoLoginUseCase"]:::usecases
        RU["RegisterUserUseCase"]:::usecases
        RUV["RegisterUserValidator"]:::usecases
    end

    %% Domain Layer
    subgraph "Domain Layer"
        Book["Book"]:::domain
        CheckoutEnt["Checkout"]:::domain
        UserEnt["User"]:::domain
    end

    %% Infrastructure Layer
    subgraph "Infrastructure Layer"
        DB["TechLibraryDbContext"]:::infra
        Cryp["BCryptAlgorithm"]:::infra
        Jwt["JwtTokenGenerator"]:::infra
    end

    %% Services
    subgraph "Services"
        LUS["LoggedUserService"]:::services
    end

    %% Exception Handling
    subgraph "Exception Handling"
        Err["CustomExceptions"]:::exceptions
    end

    %% Connections from Configuration to API layer (initialization)
    Prog -->|"bootstraps"| Books

    %% API Controllers call Use Cases
    Books -->|"calls"| FB
    Checkout -->|"calls"| CB
    Login -->|"calls"| DL
    User -->|"calls"| RU

    %% API Controllers use Communication DTOs (example from BooksController)
    Books -->|"mapsInput"| Req
    Books -->|"mapsOutput"| Res

    %% Use Cases interact with Domain Entities
    FB -->|"handles"| Book
    CB -->|"handles"| CheckoutEnt
    DL -->|"handles"| UserEnt
    RU -->|"handles"| UserEnt

    %% Use Cases persist/retrieve data via the DataAccess component
    FB -->|"persists"| DB
    CB -->|"persists"| DB
    DL -->|"persists"| DB
    RU -->|"persists"| DB

    %% Security operations in Use Cases
    DL -->|"verifies"| Cryp
    DL -->|"generates"| Jwt
    RU -->|"hashes"| Cryp

    %% Use Cases throw exceptions on error
    FB -->|"throwsExceptions"| Err
    CB -->|"throwsExceptions"| Err
    DL -->|"throwsExceptions"| Err
    RU -->|"throwsExceptions"| Err

    %% Controllers use LoggedUserService for authentication context
    User -->|"retrievesUser"| LUS

    %% Click Events for API Layer (Controllers)
    click Books "https://github.com/mayumisiano/nlw-connect-devstage/tree/main/TechLibrary.Api/Controllers"
    click Checkout "https://github.com/mayumisiano/nlw-connect-devstage/tree/main/TechLibrary.Api/Controllers"
    click Login "https://github.com/mayumisiano/nlw-connect-devstage/tree/main/TechLibrary.Api/Controllers"
    click User "https://github.com/mayumisiano/nlw-connect-devstage/tree/main/TechLibrary.Api/Controllers"

    %% Click Events for Domain Layer (Entities)
    click Book "https://github.com/mayumisiano/nlw-connect-devstage/tree/main/TechLibrary.Api/Domain/Entities"
    click CheckoutEnt "https://github.com/mayumisiano/nlw-connect-devstage/tree/main/TechLibrary.Api/Domain/Entities"
    click UserEnt "https://github.com/mayumisiano/nlw-connect-devstage/tree/main/TechLibrary.Api/Domain/Entities"

    %% Click Events for Use Cases / Application Layer
    click FB "https://github.com/mayumisiano/nlw-connect-devstage/blob/main/TechLibrary.Api/UseCases/Book/Filter/FilterBookUseCase.cs"
    click CB "https://github.com/mayumisiano/nlw-connect-devstage/blob/main/TechLibrary.Api/UseCases/Checkouts/RegisterBookCheckoutUseCase.cs"
    click DL "https://github.com/mayumisiano/nlw-connect-devstage/blob/main/TechLibrary.Api/UseCases/Login/DoLogin/DoLoginUseCase.cs"
    click RU "https://github.com/mayumisiano/nlw-connect-devstage/blob/main/TechLibrary.Api/UseCases/Users/Register/RegisterUserUseCase.cs"
    click RUV "https://github.com/mayumisiano/nlw-connect-devstage/blob/main/TechLibrary.Api/UseCases/Users/Register/RegisterUserValidator.cs"

    %% Click Events for Infrastructure Layer
    click DB "https://github.com/mayumisiano/nlw-connect-devstage/blob/main/TechLibrary.Api/Infrastructure/DataAccess/TechLibraryDbContext.cs"
    click Cryp "https://github.com/mayumisiano/nlw-connect-devstage/blob/main/TechLibrary.Api/Infrastructure/Security/Cryptography/BCryptAlgorithm.cs"
    click Jwt "https://github.com/mayumisiano/nlw-connect-devstage/blob/main/TechLibrary.Api/Infrastructure/Security/Tokens/Access/JwtTokenGenerator.cs"

    %% Click Event for Services
    click LUS "https://github.com/mayumisiano/nlw-connect-devstage/blob/main/TechLibrary.Api/Services/LoggedUser/LoggedUserService.cs"

    %% Click Events for Communication Layer
    click Req "https://github.com/mayumisiano/nlw-connect-devstage/tree/main/TechLibrary.Communication/Requests"
    click Res "https://github.com/mayumisiano/nlw-connect-devstage/tree/main/TechLibrary.Communication/Responses"

    %% Click Event for Exception Handling
    click Err "https://github.com/mayumisiano/nlw-connect-devstage/blob/main/TechLibrary.Exceptions"

    %% Click Events for Configuration & Startup
    click Prog "https://github.com/mayumisiano/nlw-connect-devstage/blob/main/TechLibrary.Api/Program.cs"
    click App "https://github.com/mayumisiano/nlw-connect-devstage/blob/main/TechLibrary.Api/appsettings.json"
    click LS "https://github.com/mayumisiano/nlw-connect-devstage/blob/main/TechLibrary.Api/Properties/launchSettings.json"

    %% Styles
    classDef api fill:#add8e6,stroke:#000,stroke-width:1.5px;
    classDef usecases fill:#90ee90,stroke:#000,stroke-width:1.5px;
    classDef domain fill:#ffff99,stroke:#000,stroke-width:1.5px;
    classDef infra fill:#ffa500,stroke:#000,stroke-width:1.5px;
    classDef services fill:#dda0dd,stroke:#000,stroke-width:1.5px;
    classDef comm fill:#87ceeb,stroke:#000,stroke-width:1.5px;
    classDef exceptions fill:#d3d3d3,stroke:#000,stroke-width:1.5px;
    classDef config fill:#40e0d0,stroke:#000,stroke-width:1.5px;
```
