# breaksmodded/mi_catalogo_backend architecture

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

- Interactive diagram: https://gitdiagram.com/breaksmodded/mi_catalogo_backend
- Repository: https://github.com/breaksmodded/mi_catalogo_backend

## Overview

##### Project Type and Purpose
- This is the backend service of a full-stack application (“Mi Catálogo”) that provides a RESTful API for movie/series catalog management.
- Core features: media CRUD, advanced search, intelligent translations, dynamic posters, hybrid caching (Redis + in-memory), PostgreSQL storage, external TMDb integration.

##### High-Level Components
a. Frontend (React) – external client that consumes the API
b. API Gateway (FastAPI) – entry point; hosts all endpoints (`main.py`)
c. Router/Controller Layer – Pydantic-validated endpoints defined across modules (`schemas.py`, `main.py`)
d. Service Layer
- CRUD Service (`crud.py`) – media, lists, tags, favorites
- Translation Service (`translation_service.py`) – TMDb translation calls + DB cache
- Poster Service (`poster_cache.py`) – hybrid Redis+memory cache + TMDb poster selection
e. Data Access Layer
- Database (`database.py`, SQLAlchemy models in `models.py` with optimized indexes)
- Caches: Redis instance (optional) + in-memory fallback
f. External Services
- TMDb API – for metadata, translations, poster images
- Supabase/PostgreSQL – primary persistence

##### Relationships and Data Flows
- Frontend → FastAPI HTTP requests → Controller
- Controller → Service Layer (translation, poster, CRUD)
- Service Layer →
- Check in-memory cache → Redis → Database → TMDb (for misses) → backfill caches
- Direct DB queries for CRUD & search (with optimized indexes)
- FastAPI returns JSON responses to frontend

##### Architectural Patterns and Principles
- Layered Architecture: Controllers → Services → Repositories/ORM
- Single source of truth in PostgreSQL; caches for performance
- Circuit-breaker style fallback: Redis → in-memory → DB → TMDb
- Bulk/batch operations for posters & translations
- Dependency injection via FastAPI’s `Depends`
- Clear separation: media management vs. translation vs. poster logic

##### Technologies and Frameworks
- FastAPI (ASGI framework)
- Pydantic (request/response validation)
- SQLAlchemy 2.0 (ORM, DB models in `models.py`)
- PostgreSQL (Supabase) with custom indexes
- Redis (cache) + Python dict (fallback)
- Requests + BeautifulSoup4 (external API & scraping)
- python-dotenv (environment config)

##### Detailed Component Breakdown
- Controller Layer:
- main.py – entry point, mounts routers
- schemas.py – request/response models
- Service Layer:
- crud.py – generic CRUD operations
- translation_service.py – translation logic + DB cache interactions
- poster_cache.py – poster selection + hybrid cache logic
- Data Layer:
- database.py – engine, session management, index definitions
- models.py – SQLAlchemy table definitions (media, content_translations, lists, tags)
- External:
- TMDb API – translation & poster metadata
- Redis – optional for high-speed caching
- Supabase/PostgreSQL – primary data store

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% External Clients
    ReactFrontend["React Frontend"]:::external
    TMDbAPI["TMDb API"]:::external

    %% Backend Boundary
    subgraph "Mi Catálogo Backend (FastAPI)"
        direction TB

        %% Controller Layer
        subgraph "Controllers"
            direction TB
            Main["main.py"]:::backend
            Schemas["schemas.py"]:::backend
        end

        %% Service Layer
        subgraph "Services"
            direction TB
            CRUDService["CRUD Service"]:::service
            TranslationService["Translation Service"]:::service
            PosterService["Poster Service"]:::service
        end

        %% Data Access Layer
        subgraph "Data Access"
            direction TB
            DatabaseModule["database.py"]:::data
            Models["models.py"]:::data
            Config["config.py"]:::backend
            MemoryCache["In-Memory Cache"]:::cache
        end
    end

    %% Data Stores
    RedisCache["Redis Cache"]:::cache
    Postgres["PostgreSQL (Supabase)"]:::db

    %% Flows
    ReactFrontend -->|"HTTP Request"| Main
    Main -->|"uses schemas"| Schemas
    Main -->|"Depends injection"| CRUDService
    Main -->|"Depends injection"| TranslationService
    Main -->|"Depends injection"| PosterService

    %% CRUD flow
    CRUDService -->|"SQL query"| DatabaseModule
    DatabaseModule -->|"optimized indexes"| Postgres

    %% Translation flow
    TranslationService -->|"check cache"| MemoryCache
    MemoryCache -.->|"miss"| RedisCache
    RedisCache -.->|"miss"| DatabaseModule
    DatabaseModule -.->|"miss"| TMDbAPI
    TMDbAPI -->|"translations data"| DatabaseModule
    DatabaseModule -->|"cache fill"| RedisCache
    RedisCache -->|"cache fill"| MemoryCache

    %% Poster flow
    PosterService -->|"check cache"| MemoryCache
    MemoryCache -.->|"miss"| RedisCache
    RedisCache -.->|"miss"| DatabaseModule
    DatabaseModule -.->|"miss"| TMDbAPI
    TMDbAPI -->|"poster data"| DatabaseModule
    DatabaseModule -->|"cache fill"| RedisCache
    RedisCache -->|"cache fill"| MemoryCache

    %% Direct external storage
    DatabaseModule -->|"persists"| Postgres

    %% Click Events
    click Main "https://github.com/breaksmodded/mi_catalogo_backend/blob/main/main.py"
    click Schemas "https://github.com/breaksmodded/mi_catalogo_backend/blob/main/schemas.py"
    click CRUDService "https://github.com/breaksmodded/mi_catalogo_backend/blob/main/crud.py"
    click TranslationService "https://github.com/breaksmodded/mi_catalogo_backend/blob/main/translation_service.py"
    click PosterService "https://github.com/breaksmodded/mi_catalogo_backend/blob/main/poster_cache.py"
    click DatabaseModule "https://github.com/breaksmodded/mi_catalogo_backend/blob/main/database.py"
    click Models "https://github.com/breaksmodded/mi_catalogo_backend/blob/main/models.py"
    click Config "https://github.com/breaksmodded/mi_catalogo_backend/blob/main/config.py"

    %% Styles
    classDef external fill:#a2d5c6,stroke:#333,stroke-width:1px
    classDef backend fill:#89c2d9,stroke:#333,stroke-width:1px
    classDef service fill:#bbe1fa,stroke:#333,stroke-width:1px
    classDef data fill:#ffd97d,stroke:#333,stroke-width:1px
    classDef cache fill:#f6bd60,stroke:#333,stroke-width:1px
    classDef db fill:#f78fb3,stroke:#333,stroke-width:1px
```
