# fan9704/drawing-competition-backend-v2 architecture

> Architecture diagram of the GitHub repository fan9704/drawing-competition-backend-v2, 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/fan9704/drawing-competition-backend-v2
- Repository: https://github.com/fan9704/drawing-competition-backend-v2

## Overview

##### Project Type and Purpose
- This is a backend web service for a drawing or challenge-based application refactored using FastAPI.
- Its purpose is to expose RESTful endpoints (with Swagger documentation available at /docs) that handle functionalities for challenges, teams, submissions, clips, rounds, and statistics.
- The project leverages FastAPI, Tortoise ORM for persistence, and Pydantic for data validation, with the use of JWT for authorization and middleware for internationalization (i18n).

##### Analyzing the File Structure
- The top-level organization clearly separates the application code under the “src” directory, testing under “tests”, Docker-related deployment configuration (Dockerfile and docker-compose.yaml), and project metadata/configurations.
- Key directories in “src” include:
- configs – Contains configuration for the application such as database (db.py), JWT settings, and OpenAPI configurations.
- dependencies – Manages external dependencies (repositories and utilities) that might be injected into services or routers.
- exceptions – Custom exception handling logic to streamline error responses.
- middlewares – Likely routing and language selection middleware (e.g., language.py).
- models – Two distinct parts: “pydantic” models for request/response validation and “tortoise” models for database ORM mapping; enums reside here as well.
- repositories – Abstract data access into CRUD operations for challenges, rounds, submissions, and teams.
- routers – FastAPI route definitions that map endpoints (challenge, clip, round, statistic, team, etc.) to business logic.
- services – Contains the business logic for processing clips and team operations.
- utils – Generic utility functions like those for API routing, clip operations, image judging logic, and JWT handling.
- The project also includes language-driven functionality (translations directory) and extensive automated tests (unit, integration, e2e) that further validate each architectural component.

- At the top, show “Clients/Users” sending requests.
- Branch out to:
- “Routers” layer: separate nodes for each domain (challenge, clip, round, statistic, team, submission).
- “Services” layer: business logic nodes (clip logic, team operations).
- “Repositories” layer: corresponding CRUD operations that connect to models.
- “Models”: split into Pydantic (for API validation) and Tortoise ORM (for persistence with PostgreSQL).
- At the bottom, connect to “PostgreSQL Database” (and note connection requirements like timezone settings).
- On the side, list “External Dependencies” such as GhostScript, opencv-python, and other libraries.
- API request flows → Middleware (i18n, JWT) → Routers → Service → Repository → Database.
- Optionally, indicate testing components and containerization outside the primary application logic for deployment reference.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Main Flow
    A["Clients/Users"]:::client
    B["FastAPI Entry Point"]:::gateway
    C["Language Middleware"]:::middleware

    %% Routers Subgraph
    subgraph "Routers Layer"
        R1["Challenge Router"]:::routers
        R2["Clip Router"]:::routers
        R3["Round Router"]:::routers
        R4["Statistic Router"]:::routers
        R5["Submission Router"]:::routers
        R6["Team Router"]:::routers
    end

    %% Services Subgraph
    subgraph "Service Layer"
        S1["Clip Service"]:::services
        S2["Team Service"]:::services
    end

    %% Repositories Subgraph
    subgraph "Repository Layer"
        RE1["Challenge Repository"]:::repositories
        RE2["Round Repository"]:::repositories
        RE3["Submission Repository"]:::repositories
        RE4["Team Repository"]:::repositories
    end

    %% Models Subgraph
    subgraph "Models"
        M1["Pydantic Models"]:::models
        M2["Tortoise ORM Models"]:::models
        M3["Enums"]:::models
    end

    %% Supporting Components
    D["Configurations"]:::support
    E["Dependencies"]:::support
    F["Exception Handling"]:::support
    U["Utils"]:::support
    I["Translations"]:::support
    DC["Containerization"]:::support
    X["External Dependencies"]:::external

    %% Database
    PD["PostgreSQL Database"]:::database

    %% Connections - Main Request Flow
    A -->|"sends request"| B
    B -->|"applies"| C
    C -->|"routes to"| R1
    C --> R2
    C --> R3
    C --> R4
    C --> R5
    C --> R6

    %% Routers to Service Layer
    R2 -->|"delegates"| S1
    R6 -->|"delegates"| S2
    %% Other routers directly use repositories
    R1 -->|"calls CRUD"| RE1
    R3 -->|"calls CRUD"| RE2
    R5 -->|"calls CRUD"| RE3
    R4 -->|"calls CRUD"| RE1

    %% Services to Repositories
    S1 -->|"uses"| RE1
    S2 -->|"uses"| RE4

    %% Repositories to Models and Database
    RE1 -->|"maps to"| M2
    RE2 --> M2
    RE3 --> M2
    RE4 --> M2
    M2 -->|"persists data"| PD

    %% Routers use Pydantic Models for validation
    R1 ---|"validates with"| M1
    R2 --- M1
    R3 --- M1
    R4 --- M1
    R5 --- M1
    R6 --- M1

    %% Supporting Components Connections
    D -->|"configures"| B
    D --> RE1
    E -->|"injects into"| R1
    E --> R2
    E --> R3
    E --> R4
    E --> R5
    E --> R6
    F -->|"handles errors in"| B
    U -->|"assists"| S1
    U --> S2
    U -->|"supports"| R1
    I -->|"supports i18n in"| C
    DC -->|"containerizes"| B
    X -->|"integrates with"| B

    %% Click Events for Component Mapping
    click B "https://github.com/fan9704/drawing-competition-backend-v2/blob/master/src/main.py"
    click C "https://github.com/fan9704/drawing-competition-backend-v2/blob/master/src/middlewares/language.py"
    click R1 "https://github.com/fan9704/drawing-competition-backend-v2/blob/master/src/routers/challenge.py"
    click R2 "https://github.com/fan9704/drawing-competition-backend-v2/blob/master/src/routers/clip.py"
    click R3 "https://github.com/fan9704/drawing-competition-backend-v2/blob/master/src/routers/round.py"
    click R4 "https://github.com/fan9704/drawing-competition-backend-v2/blob/master/src/routers/statistic.py"
    click R5 "https://github.com/fan9704/drawing-competition-backend-v2/blob/master/src/routers/submission.py"
    click R6 "https://github.com/fan9704/drawing-competition-backend-v2/blob/master/src/routers/team.py"
    click S1 "https://github.com/fan9704/drawing-competition-backend-v2/blob/master/src/services/clip.py"
    click S2 "https://github.com/fan9704/drawing-competition-backend-v2/blob/master/src/services/team.py"
    click RE1 "https://github.com/fan9704/drawing-competition-backend-v2/blob/master/src/repositories/challenge.py"
    click RE2 "https://github.com/fan9704/drawing-competition-backend-v2/blob/master/src/repositories/round.py"
    click RE3 "https://github.com/fan9704/drawing-competition-backend-v2/blob/master/src/repositories/submission.py"
    click RE4 "https://github.com/fan9704/drawing-competition-backend-v2/blob/master/src/repositories/team.py"
    click M1 "https://github.com/fan9704/drawing-competition-backend-v2/tree/master/src/models/pydantic/"
    click M2 "https://github.com/fan9704/drawing-competition-backend-v2/tree/master/src/models/tortoise/"
    click M3 "https://github.com/fan9704/drawing-competition-backend-v2/tree/master/src/models/enums/"
    click D "https://github.com/fan9704/drawing-competition-backend-v2/tree/master/src/configs/"
    click E "https://github.com/fan9704/drawing-competition-backend-v2/tree/master/src/dependencies/"
    click F "https://github.com/fan9704/drawing-competition-backend-v2/tree/master/src/exceptions/"
    click U "https://github.com/fan9704/drawing-competition-backend-v2/tree/master/src/utils/"
    click I "https://github.com/fan9704/drawing-competition-backend-v2/tree/master/translations/"
    click DC "https://github.com/fan9704/drawing-competition-backend-v2/tree/master/Dockerfile"

    %% Styles
    classDef client fill:#FAD,stroke:#333,stroke-width:2px;
    classDef gateway fill:#BBF,stroke:#333,stroke-width:2px;
    classDef middleware fill:#CCF,stroke:#333,stroke-width:2px;
    classDef routers fill:#CFC,stroke:#333,stroke-width:2px;
    classDef services fill:#FCF,stroke:#333,stroke-width:2px;
    classDef repositories fill:#FCC,stroke:#333,stroke-width:2px;
    classDef models fill:#FFC,stroke:#333,stroke-width:2px;
    classDef support fill:#DEF,stroke:#333,stroke-width:2px;
    classDef database fill:#CFF,stroke:#333,stroke-width:2px;
    classDef external fill:#EFE,stroke:#333,stroke-width:2px;
```
