# marcelochia/api-controle-gastos architecture

> Architecture diagram of the GitHub repository marcelochia/api-controle-gastos, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-01-22). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/marcelochia/api-controle-gastos
- Repository: https://github.com/marcelochia/api-controle-gastos

## Overview

##### Main Components

a) Client Layer:
- External API consumers
- Supported HTTP methods (GET, POST, PUT, DELETE)

b) Application Layer:
- Authentication System (evident from LoginController and auth middleware)
- API Controllers Layer
- ExpensesController
- IncomesController
- SummaryController
- LoginController
- Request Validation Layer (HTTP/Requests folder)
- Resource Layer (for API responses)
- Models Layer
- User
- Expense
- Income
- Category

c) Database Layer:
- MySQL Database
- Key Tables:
- users
- expenses (despesas)
- incomes (receitas)
- categories (categorias)

**[Client Layer]**
- ↕
**[Application Layer]**
- ↕
**[Database Layer]**

##### Specific Elements

a) Security & Middleware Layer:
- Authentication flow
- CSRF protection
- API middleware stack

b) Core Business Logic:
- Expense management flow
- Income management flow
- Categorization system
- Summary generation

c) Data Flow:
- Request/response cycle
- Validation steps
- Resource transformation process

##### Important Relationships to Highlight

- Authentication flow between client and LoginController
- Request validation through FormRequests
- Model relationships (Category to Expense)
- Resource transformations for API responses

##### Technical Details

- PHP 8.1 environment
- Laravel 9 framework
- MySQL 8 database
- RESTful API endpoints
- Authentication mechanism
- Resource transformation layer

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Client
        API_CONSUMER[API Consumer]
        HTTP_METHODS["HTTP Methods<br/>GET, POST, PUT, DELETE"]
    end

    subgraph Security
        AUTH[Authentication]:::security
        CSRF["CSRF Protection"]:::security
        click AUTH "https://github.com/marcelochia/api-controle-gastos/blob/main/app/Http/Middleware/Authenticate.php"
        click CSRF "https://github.com/marcelochia/api-controle-gastos/blob/main/app/Http/Middleware/VerifyCsrfToken.php"
    end

    subgraph "API Layer"
        ROUTES["API Routes"]:::api
        click ROUTES "https://github.com/marcelochia/api-controle-gastos/blob/main/routes/api.php"

        subgraph Controllers
            LOGIN["LoginController"]:::controller
            EXPENSES["ExpensesController"]:::controller
            INCOMES["IncomesController"]:::controller
            SUMMARY["SummaryController"]:::controller
            click LOGIN "https://github.com/marcelochia/api-controle-gastos/blob/main/app/Http/Controllers/LoginController.php"
            click EXPENSES "https://github.com/marcelochia/api-controle-gastos/blob/main/app/Http/Controllers/ExpensesController.php"
            click INCOMES "https://github.com/marcelochia/api-controle-gastos/blob/main/app/Http/Controllers/IncomesController.php"
            click SUMMARY "https://github.com/marcelochia/api-controle-gastos/blob/main/app/Http/Controllers/SummaryController.php"
        end

        subgraph "Request Validation"
            EXP_REQ["ExpenseRequest"]:::validation
            INC_REQ["IncomeRequest"]:::validation
            click EXP_REQ "https://github.com/marcelochia/api-controle-gastos/blob/main/app/Http/Requests/ExpenseRequest.php"
            click INC_REQ "https://github.com/marcelochia/api-controle-gastos/blob/main/app/Http/Requests/IncomeRequest.php"
        end

        subgraph Resources
            EXP_RES["ExpenseResource"]:::resource
            click EXP_RES "https://github.com/marcelochia/api-controle-gastos/blob/main/app/Http/Resources/ExpenseResource.php"
        end
    end

    subgraph "Data Layer"
        subgraph Models
            USER_MODEL["User Model"]:::model
            EXP_MODEL["Expense Model"]:::model
            INC_MODEL["Income Model"]:::model
            CAT_MODEL["Category Model"]:::model
            click USER_MODEL "https://github.com/marcelochia/api-controle-gastos/blob/main/app/Models/User.php"
            click EXP_MODEL "https://github.com/marcelochia/api-controle-gastos/blob/main/app/Models/Expense.php"
            click INC_MODEL "https://github.com/marcelochia/api-controle-gastos/blob/main/app/Models/Income.php"
            click CAT_MODEL "https://github.com/marcelochia/api-controle-gastos/blob/main/app/Models/Category.php"
        end

        subgraph Database
            USERS_TABLE[("Users")]:::database
            EXPENSES_TABLE[("Expenses")]:::database
            INCOMES_TABLE[("Incomes")]:::database
            CATEGORIES_TABLE[("Categories")]:::database
            click USERS_TABLE "https://github.com/marcelochia/api-controle-gastos/blob/main/database/migrations/2014_10_12_000000_create_users_table.php"
            click EXPENSES_TABLE "https://github.com/marcelochia/api-controle-gastos/blob/main/database/migrations/2022_08_01_230006_create_despesas_table.php"
            click INCOMES_TABLE "https://github.com/marcelochia/api-controle-gastos/blob/main/database/migrations/2022_08_01_225705_create_receitas_table.php"
            click CATEGORIES_TABLE "https://github.com/marcelochia/api-controle-gastos/blob/main/database/migrations/2022_08_08_130308_create_categorias_table.php"
        end

        subgraph "Test Suite"
            LOGIN_TEST["Login Tests"]:::test
            EXP_TEST["Expense Tests"]:::test
            INC_TEST["Income Tests"]:::test
            click LOGIN_TEST "https://github.com/marcelochia/api-controle-gastos/blob/main/tests/Feature/Authentication/LoginTest.php"
            click EXP_TEST "https://github.com/marcelochia/api-controle-gastos/blob/main/tests/Feature/Expense/ExpenseTest.php"
            click INC_TEST "https://github.com/marcelochia/api-controle-gastos/blob/main/tests/Feature/Income/IncomeTest.php"
        end
    end

    %% Relationships
    API_CONSUMER --> HTTP_METHODS
    HTTP_METHODS --> AUTH
    AUTH --> ROUTES
    CSRF --> ROUTES
    
    ROUTES --> LOGIN
    ROUTES --> EXPENSES
    ROUTES --> INCOMES
    ROUTES --> SUMMARY
    
    EXPENSES --> EXP_REQ
    INCOMES --> INC_REQ
    
    EXPENSES --> EXP_RES
    
    EXP_REQ --> EXP_MODEL
    INC_REQ --> INC_MODEL
    
    EXP_MODEL --> EXPENSES_TABLE
    INC_MODEL --> INCOMES_TABLE
    USER_MODEL --> USERS_TABLE
    CAT_MODEL --> CATEGORIES_TABLE

    %% Styling
    classDef security fill:#FFE5CC,stroke:#FF9933
    classDef api fill:#DCEDC8,stroke:#8BC34A
    classDef controller fill:#B3E5FC,stroke:#03A9F4
    classDef validation fill:#FFE0B2,stroke:#FF9800
    classDef resource fill:#E1BEE7,stroke:#9C27B0
    classDef model fill:#C8E6C9,stroke:#4CAF50
    classDef database fill:#D1C4E9,stroke:#673AB7
    classDef test fill:#FFCDD2,stroke:#F44336
```
