# cleytonrr/my-contacts architecture

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

- Interactive diagram: https://gitdiagram.com/cleytonrr/my-contacts
- Repository: https://github.com/cleytonrr/my-contacts

## Overview

##### Main Components
- Frontend: The React application, which is composed of multiple components (e.g., App, ContactForm, Header, various pages like Home, NewContact, EditContact) and makes API calls through its services (ContactsService, CategoriesService).
- Backend: The Node.js/Express server that handles RESTful API endpoints. This includes:
- Controllers (e.g., CategoryController, ContactController) handling incoming requests.
- Middlewares (e.g., CORS, ErrorHandler) that manage pre/post API processing.
- Repositories (e.g., CategoriesRepository, ContactsRepository) that abstract the database operations.
- Utility functions and database configuration (with schema.sql for setting up PostgreSQL).
- Database: PostgreSQL which is managed from the backend by executing SQL scripts (schema.sql), and utilized via the database layer in the backend.
- External Components: Tools/inputs such as environment files (.env, etc.) that configure runtime and deployment settings.

##### Relationships and Interactions
- The Frontend sends HTTP requests (through the HttpClient utilities) to the Backend’s API endpoints.
- The Backend processes these requests via its controllers and middlewares, delegates data handling to the repositories, and interacts with PostgreSQL.
- Error handling is managed centrally in the backend with an error handling middleware.
- The frontend’s internal state management and routing (via router.js) guide the user interactions and present the UI accordingly.

##### Architectural Patterns / Design Principles
- Clear separation of concerns: The division between frontend and backend helps in isolating UI logic from business logic.
- MVC-like pattern on the backend: Controllers, repositories, and middlewares formulate a layered pattern for processing requests.
- Component-based architecture in the frontend: React components and pages are distinctly separated and modular.
- RESTful API communication between the frontend and backend.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend Subgraph
    subgraph "Frontend (React)"
        FA["Frontend (React)"]:::frontend
        UC["UI Components"]:::frontend
        C["Components"]:::frontend
        P["Pages"]:::frontend
        CS["ContactsService"]:::service
        CCS["CategoriesService"]:::service
        HC["HTTP Client"]:::service
        SM["Service Mappers"]:::service
    end

    %% Backend Subgraph
    subgraph "Backend (Node.js + Express)"
        BA["Backend (Node.js + Express)"]:::backend
        AR["API Routes"]:::backend
        CC["CategoryController"]:::controller
        CT["ContactController"]:::controller
        CM["CORS Middleware"]:::middleware
        EH["Error Handling Middleware"]:::middleware
        CR["CategoriesRepository"]:::repository
        CR2["ContactsRepository"]:::repository
        DU["Database Utilities"]:::repository
        DS["Database Schema"]:::repository
        UUID["isValidUUID Utility"]:::utility
        EC["Environment Config"]:::utility
    end

    %% Database Node
    DB["PostgreSQL Database"]:::db

    %% Frontend Internal Connections
    FA --> UC
    UC --> C
    UC --> P
    CS --> HC
    CCS --> HC

    %% Frontend to Backend: HTTP Requests from Services
    CS -->|"HTTP_Request"| AR
    CCS -->|"HTTP_Request"| AR

    %% Backend Internal Connections
    BA --> AR
    AR --> CC
    AR --> CT
    AR --> CM
    AR --> EH
    AR --> UUID
    EC --> AR
    CC --> CR
    CT --> CR2
    CR --> DU
    CR2 --> DU
    DU --> DB
    DS --> DB

    %% Styling
    classDef frontend fill:#e6f7ff,stroke:#1890ff,stroke-width:2px;
    classDef backend fill:#fffbe6,stroke:#faad14,stroke-width:2px;
    classDef db fill:#f6ffed,stroke:#52c41a,stroke-width:2px;
    classDef service fill:#ffe7e7,stroke:#f5222d,stroke-width:2px;
    classDef controller fill:#e6fffb,stroke:#13c2c2,stroke-width:2px;
    classDef middleware fill:#fff0f6,stroke:#ff85c0,stroke-width:2px;
    classDef repository fill:#f9f0ff,stroke:#722ed1,stroke-width:2px;
    classDef utility fill:#edefed,stroke:#8c8c8c,stroke-width:2px;

    %% Click Events for Frontend Components
    click FA "https://github.com/cleytonrr/my-contacts/tree/main/frontend/"
    click C "https://github.com/cleytonrr/my-contacts/tree/main/frontend/src/components/"
    click P "https://github.com/cleytonrr/my-contacts/tree/main/frontend/src/pages/"
    click CS "https://github.com/cleytonrr/my-contacts/blob/main/frontend/src/services/ContactsService.js"
    click CCS "https://github.com/cleytonrr/my-contacts/blob/main/frontend/src/services/CategoriesService.js"
    click HC "https://github.com/cleytonrr/my-contacts/blob/main/frontend/src/services/utils/HttpClient.js"
    click SM "https://github.com/cleytonrr/my-contacts/tree/main/frontend/src/services/mappers/"

    %% Click Events for Backend Components
    click BA "https://github.com/cleytonrr/my-contacts/tree/main/backend/"
    click CC "https://github.com/cleytonrr/my-contacts/blob/main/backend/src/app/controllers/CategoryController.js"
    click CT "https://github.com/cleytonrr/my-contacts/blob/main/backend/src/app/controllers/ContactController.js"
    click CM "https://github.com/cleytonrr/my-contacts/blob/main/backend/src/app/middlewares/cors.js"
    click EH "https://github.com/cleytonrr/my-contacts/blob/main/backend/src/app/middlewares/errorHandler.js"
    click CR "https://github.com/cleytonrr/my-contacts/blob/main/backend/src/app/repositories/CategoriesRepository.js"
    click CR2 "https://github.com/cleytonrr/my-contacts/blob/main/backend/src/app/repositories/ContactsRepository.js"
    click DU "https://github.com/cleytonrr/my-contacts/blob/main/backend/src/app/database/index.js"
    click DS "https://github.com/cleytonrr/my-contacts/blob/main/backend/src/app/database/schema.sql"
    click UUID "https://github.com/cleytonrr/my-contacts/blob/main/backend/src/app/utils/isValidUUID.js"
    click AR "https://github.com/cleytonrr/my-contacts/blob/main/backend/src/routes.js"
    click EC "https://github.com/cleytonrr/my-contacts/blob/main/backend/.env-example"
```
