# pedrohaveloso/postin architecture

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

- Interactive diagram: https://gitdiagram.com/pedrohaveloso/postin
- Repository: https://github.com/pedrohaveloso/postin

## Overview

##### Project Overview and Main Components
- Frontend: Built using React, TypeScript, Vite, and TailwindCSS. It’s organized into clear segments: data sources (APIs), data models, repositories, presenters, and UI components/pages.
- Backend/API: A PHP API that provides endpoints for user authentication, posting, favoriting posts, etc. It’s implemented in PHP without a framework (using Composer for autoloading) and follows some clean architecture principles by separating models, repositories (with interfaces/implementations), services, and routing.
- Database: A MySQL database (as seen in api/db.sql) that stores user and post data.
- External Services: While nothing third‐party is explicitly mentioned, note that the PHP API is hosted externally, even if temporarily.

##### Breaking Down the File Structure
- For the API:
– The “api/app” directory is subdivided into Models, Repository (with separate directories for Database classes and Interfaces), Routes, Services, and Utilities. This indicates a layered architecture where Repository interfaces/implementations mediate data access, and Services encapsulate business logic.
– The “api/public” directory will serve static resources, and “server.php” acts as the API gateway.
- For the Frontend:
– The “src/data” part contains datasources (with TypeScript files for calling the API), models for post and user actions, and repository classes, which mirror the separation in the API.
– The “src/presenter” directory includes business logic for transferring data into UI-friendly formats (with providers like ApiProvider and AuthProvider).

a. Main Components:
- Frontend:
- UI Layer: Composed of pages (HomePage, ProfilePage, etc.) and components (LoginForm, RegisterForm, Navbar, etc.)
- Presentation Layer: Presenters and Providers (e.g., PostPresenter, UserPresenter, ApiProvider, AuthProvider) that manage state and route data.
- Data Layer: Datasources (API calls) and repositories that fetch or manipulate data.
- Backend:
- API Gateway: server.php (front controller)
- Routing: Router and Routes to dispatch API calls.
- Business Logic: Services handling users and posts.
- Persistence Layer: Repositories implementing interfaces, communicating with the MySQL database.
- Data Models: PHP Models (e.g., PersonLoginModel, PersonRegisterModel, PostCreateModel) representing domain entities.
- Database: MySQL data store, as per api/db.sql.

b. Relationships and Interactions:
- The Frontend (in React) makes HTTP calls through the API datasources to the PHP API.
- On the API side, the Router receives these calls, dispatches them to the appropriate Services which then consult the Repositories.
- Repositories interface with the database to perform CRUD operations.
- Data flow from UI → Presenter/Provider → Datasource → API request.
- API request routed internally: Controller/Router → Service → Repository → Database.
- Responses flowing back from the Database upward to the Presenter and eventually the UI.

c. Architectural Patterns/Design Principles:
- API shows an attempt at Clean Architecture and Separation of Concerns by splitting Models, Repositories (with interfaces and dependency inversion), and Services.
- The frontend is organized with separation between data (repositories/datasources), presentation (presenters and providers), and UI (components/pages).
- Important flows such as user authentication (login, registration) and posting as separate flows.

d. Relevant Technologies:
- Frontend: React, TypeScript, Vite, TailwindCSS.
- Backend: PHP, MySQL, Composer.
- Miscellaneous: ESLint (for static analysis) in the root, NodeJS for the development server.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend Section
    subgraph "Frontend"
        direction TB
        Client["React Application"]:::frontend
        subgraph "UI Layer"
            UI["UI Components & Pages"]:::frontend
        end
        subgraph "Presentation Layer"
            Presenter["Presenters & Providers"]:::presentation
        end
        subgraph "Data Layer"
            Datasource["Datasources"]:::data
            Models["Frontend Models"]:::data
            FrontRepo["Frontend Repositories"]:::data
        end
    end

    %% Connection within Frontend
    Client -->|"renders"| UI
    UI -->|"triggers"| Presenter
    Presenter -->|"fetches data from"| Datasource
    Presenter -->|"processes"| Models
    Datasource -->|"uses repository"| FrontRepo

    %% Transition from Frontend to Backend
    FrontRepo ---|"HTTP Request"| APIGateway

    %% Backend Section
    subgraph "Backend"
        direction TB
        APIGateway["API Gateway (server.php)"]:::backend
        Router["Routing (Router/Routes)"]:::backend
        Services["Business Logic (Services)"]:::backend
        DataModels["Data Models"]:::backend
        RepoInterfaces["Repository Interfaces"]:::persistence
        RepoImpl["Repository Implementations"]:::persistence
        Utils["Utility Functions"]:::utils
    end

    %% Backend Flow
    APIGateway -->|"forwards to"| Router
    Router -->|"dispatches to"| Services
    Services -->|"utilizes"| DataModels
    Services -->|"calls"| RepoInterfaces
    RepoInterfaces -->|"implemented by"| RepoImpl
    RepoImpl -->|"CRUD operations"| Database

    %% Supporting utilities
    APIGateway ---|"helpers"| Utils
    Router ---|"helpers"| Utils

    %% Database Section
    Database["MySQL Database (api/db.sql)"]:::database

    %% Return flow (responses)
    Database -- "results" --> RepoImpl
    RepoImpl -- "data" --> Services
    Services -- "response" --> Router
    Router -- "API response" --> APIGateway
    APIGateway -- "HTTP Response" --- Presenter

    %% Click Events for Frontend Components
    click UI "https://github.com/pedrohaveloso/postin/tree/bootcamp/src/ui"
    click Presenter "https://github.com/pedrohaveloso/postin/tree/bootcamp/src/presenter"
    click Datasource "https://github.com/pedrohaveloso/postin/tree/bootcamp/src/data/datasources"
    click Models "https://github.com/pedrohaveloso/postin/tree/bootcamp/src/data/models"
    click FrontRepo "https://github.com/pedrohaveloso/postin/tree/bootcamp/src/data/repositories"

    %% Click Events for Backend Components
    click APIGateway "https://github.com/pedrohaveloso/postin/blob/bootcamp/api/server.php"
    click Router "https://github.com/pedrohaveloso/postin/tree/bootcamp/api/app/Routes"
    click Services "https://github.com/pedrohaveloso/postin/tree/bootcamp/api/app/Services"
    click DataModels "https://github.com/pedrohaveloso/postin/tree/bootcamp/api/app/Models"
    click RepoInterfaces "https://github.com/pedrohaveloso/postin/tree/bootcamp/api/app/Repository/Interfaces"
    click RepoImpl "https://github.com/pedrohaveloso/postin/tree/bootcamp/api/app/Repository"
    click Utils "https://github.com/pedrohaveloso/postin/tree/bootcamp/api/app/Utils"

    %% Click Event for Database
    click Database "https://github.com/pedrohaveloso/postin/blob/bootcamp/api/db.sql"

    %% Styles
    classDef frontend fill:#a3d5ff,stroke:#1a73e8,stroke-width:2px;
    classDef presentation fill:#a7f1a1,stroke:#34a853,stroke-width:2px;
    classDef data fill:#e1c4ff,stroke:#9c27b0,stroke-width:2px;
    classDef backend fill:#ffcc80,stroke:#f57c00,stroke-width:2px;
    classDef persistence fill:#ffe0b2,stroke:#fb8c00,stroke-width:2px;
    classDef utils fill:#c8e6c9,stroke:#388e3c,stroke-width:2px;
    classDef database fill:#eeeeee,stroke:#757575,stroke-width:2px;
```
