# leaopedro/car-rental-app architecture

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

- Interactive diagram: https://gitdiagram.com/leaopedro/car-rental-app
- Repository: https://github.com/leaopedro/car-rental-app

## Overview

##### Project type and purpose
- This is a full-stack MVP for a car-rental platform (“Carental”) with a React/Vite frontend and a Node.js/Express backend.
- The backend exposes REST endpoints for availability, booking and user management and can run with either an in-memory DB (for demos) or PostgreSQL (for production).
- The frontend is a single-page React application using a BookingContext and an API layer to interact with the backend.

##### File structure
Top level:
- backend/ – Node.js & Express API
– src/app.ts – Express app bootstrap
– src/routes/ – route handlers (availability, booking, users)
– src/services/ – business-logic layer
– src/models/ – data models (Booking, Car, User, types)
– src/db/ – DBClient interface and two implementations: inMemoryDb & postgresDb, plus migrations/seeds
– src/external/emailService.ts – integration point for sending emails
– tests/ – unit & integration tests for each layer
– knexfile.ts & docker-compose.yml – DB config & local Postgres container
- frontend/ – React + Vite SPA
– src/api/index.ts – thin fetch/axios wrapper targeting VITE_API_URL
– src/context/BookingContext.tsx – global state for booking flows
– src/components/ & src/pages/ – presentational and page components
– setupTests.ts & tests/ – component, page, context, and API layer tests
– Vite config & TypeScript configs

##### README for additional insights
- Emphasis on two interchangeable DB clients via a shared `DBClient` interface, enabling quick demos or real Postgres integration.
- The backend and frontend each have their own environment files and startup instructions, reinforcing the CI/CD separation.

## Diagram (Mermaid)

```mermaid
graph TD
    subgraph "Frontend"
        UI["UI Components"] 
        Pages["Page Components"]
        Context["BookingContext"]
        API["API Client"]
    end

    subgraph "Backend"
        Index["Server Entry (index.ts)"]
        App["Express App (app.ts)"]

        subgraph "Controllers"
            AvailRouter["AvailabilityRouter"]
            BookingRouter["BookingRouter"]
            UsersRouter["UsersRouter"]
        end

        subgraph "Services"
            AvailService["availabilityService"]
            BookingService["bookingService"]
            UserService["userService"]
        end

        subgraph "Models"
            CarModel["CarModel"]
            BookingModel["BookingModel"]
            UserModel["UserModel"]
        end

        DBClient["DBClient Interface"]

        subgraph "DB Implementations"
            InMem["InMemoryDb"]
            Postgres["PostgresDb"]
            Migrations["Migrations"]
            Seeds["Seeds"]
        end

        EmailService["Email Service"]
    end

    %% Frontend flow
    UI -->|"renders pages"| Pages
    Pages -->|"uses context"| Context
    Context -->|"calls"| API
    API -->|"HTTP requests"| App

    %% Backend startup
    Index --> App

    %% Routing layer
    App --> AvailRouter
    App --> BookingRouter
    App --> UsersRouter

    %% Controller to Service
    AvailRouter -->|"delegates to"| AvailService
    BookingRouter -->|"delegates to"| BookingService
    UsersRouter -->|"delegates to"| UserService

    %% Service to Model
    AvailService --> CarModel
    BookingService --> BookingModel
    UserService --> UserModel

    %% Model to DBClient
    CarModel --> DBClient
    BookingModel --> DBClient
    UserModel --> DBClient

    %% DBClient implementations
    DBClient -->|"uses implementation"| InMem
    DBClient -->|"uses implementation"| Postgres

    %% Migrations and seeds
    Postgres --> Migrations
    Postgres --> Seeds

    %% External email
    BookingService -->|"sends confirmation"| EmailService

    %% Classes
    classDef frontend fill:#cce5ff,stroke:#333,stroke-width:1px
    classDef controllers fill:#d4edda,stroke:#333,stroke-width:1px
    classDef services fill:#fff3cd,stroke:#333,stroke-width:1px
    classDef data fill:#fef2c0,stroke:#333,stroke-width:1px
    classDef external fill:#e2d6f0,stroke:#333,stroke-width:1px

    class UI,Pages,Context,API frontend
    class AvailRouter,BookingRouter,UsersRouter controllers
    class AvailService,BookingService,UserService services
    class CarModel,BookingModel,UserModel,DBClient,InMem,Postgres,Migrations,Seeds data
    class EmailService external

    %% Click Events
    click UI "https://github.com/leaopedro/car-rental-app/tree/main/frontend/src/components"
    click Pages "https://github.com/leaopedro/car-rental-app/tree/main/frontend/src/pages"
    click Context "https://github.com/leaopedro/car-rental-app/blob/main/frontend/src/context/BookingContext.tsx"
    click API "https://github.com/leaopedro/car-rental-app/blob/main/frontend/src/api/index.ts"
    click App "https://github.com/leaopedro/car-rental-app/blob/main/backend/src/app.ts"
    click Index "https://github.com/leaopedro/car-rental-app/blob/main/backend/src/index.ts"
    click AvailRouter "https://github.com/leaopedro/car-rental-app/blob/main/backend/src/routes/availability.ts"
    click BookingRouter "https://github.com/leaopedro/car-rental-app/blob/main/backend/src/routes/booking.ts"
    click UsersRouter "https://github.com/leaopedro/car-rental-app/blob/main/backend/src/routes/users.ts"
    click AvailService "https://github.com/leaopedro/car-rental-app/blob/main/backend/src/services/availabilityService.ts"
    click BookingService "https://github.com/leaopedro/car-rental-app/blob/main/backend/src/services/bookingService.ts"
    click UserService "https://github.com/leaopedro/car-rental-app/blob/main/backend/src/services/userService.ts"
    click CarModel "https://github.com/leaopedro/car-rental-app/blob/main/backend/src/models/carModel.ts"
    click BookingModel "https://github.com/leaopedro/car-rental-app/blob/main/backend/src/models/bookingModel.ts"
    click UserModel "https://github.com/leaopedro/car-rental-app/blob/main/backend/src/models/userModel.ts"
    click DBClient "https://github.com/leaopedro/car-rental-app/blob/main/backend/src/db/interface.ts"
    click InMem "https://github.com/leaopedro/car-rental-app/blob/main/backend/src/db/inMemoryDb.ts"
    click Postgres "https://github.com/leaopedro/car-rental-app/blob/main/backend/src/db/postgresDb.ts"
    click Migrations "https://github.com/leaopedro/car-rental-app/tree/main/backend/src/db/migrations"
    click Seeds "https://github.com/leaopedro/car-rental-app/tree/main/backend/src/db/seeds"
    click EmailService "https://github.com/leaopedro/car-rental-app/blob/main/backend/src/external/emailService.ts"
    click KNEX "https://github.com/leaopedro/car-rental-app/blob/main/backend/knexfile.ts"
    click DC "https://github.com/leaopedro/car-rental-app/blob/main/backend/docker-compose.yml"
```
