# hdang1612/ptit_dormitory architecture

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

- Interactive diagram: https://gitdiagram.com/hdang1612/ptit_dormitory
- Repository: https://github.com/hdang1612/ptit_dormitory

## Overview

##### Main Components
- Backend (ptit_dormitory_be):
- API Layer: The “routes” directory defines endpoints (e.g., authRoutes, userRoutes, areaRoute) that the client will call.
- Controller Layer: Under “controller”, you have components like authController, userController, areaController, etc. These handle incoming requests and coordinate between services and responses.
- Service Layer: The “services” directory contains business logic. For example, authServices and userServices encapsulate the core operations.
- Data/Model Layer: The “models” directory defines the database schema and objects (e.g., Users, RoomDetail, Role, etc.) which are used by services for database operations.
- Middleware & Utilities: Files under “middleware” (for auth, error handling, file upload) and “utils” (like apiError) offer cross-cutting functionalities.
- Database Configuration: The “config/db.js” file serves the purpose of connecting to the database.
- File Upload Module: The “upload” directory shows that the system supports file uploads, dealing with dormitory/student related excel files.

- Frontend (ptit_dormitory_fe):
- UI and Layout Components: Located under “src/components” (e.g., Sidebar, TopTable, Header) and “src/view” (e.g., Home, Login, ContractList).
- Global Styles and Assets: The “src/assets”, “src/style”, and various CSS/SCSS files configure the look and feel of the application.
- State Management: The “src/redux” directory includes the Redux store, indicating the use of Redux for state management.
- Entry Points & Configuration: Files like “index.html”, “index.css”, “main.jsx”, and “vite.config.js” define the project startup and bundling via Vite.

##### Relationships and Interactions
- Frontend → Backend Communication:
- The frontend communicates with the backend via REST API calls, as defined by the routes (e.g., authRoutes, userRoutes).

- Internal Flow in the Backend:
- Incoming HTTP requests pass through middleware (authentication, file upload handling, error handling).
- They are then routed to controllers (e.g., authController, userController) where requests are processed.
- Controllers call the service layer (business logic) to perform operations like user management, room allocation, or handling dormitory records.
- The service layer interacts with the models to perform database operations using the configuration defined in “config/db.js”.

- Auxiliary Flows:
- File uploads: Highlight the standalone module “upload” which manages excel files and is probably used within some controller/service workflows.
- Constant and utility modules provide common data and error handling across the system, ensuring consistency in processing date conversions or error reporting.

##### Important Architectural Patterns and Design Principles
- Layered Architecture: Each layer (routes, controllers, services, models, middleware) is clearly separated, promoting maintainability and clarity.
- Separation of Concerns: Frontend and backend are developed and deployed separately, focusing on UI and business logic respectively.
- RESTful API: The backend is designed following RESTful principles, with clearly structured endpoints and controllers.
- Use of Middleware: Cross-cutting concerns (authentication, error handling) are abstracted away using middleware modules.
- State Management in Frontend: Utilize Redux for managing application state, ensuring that the UI is synchronized with the backend data.

##### Specific Elements
- Clear labels for each module/component (e.g., “Authentication Controller”, “User Service”, “Redux Store”, etc.).
- Frontend → API Routes → Controllers → Services → Database
- Middleware intercepting requests (between routes and controllers)
- Service calls back for error handling or file uploads.
- A third distinct style for database and configuration components.
- If possible, indicate any dependencies (e.g., the mapping between functions in controllers and services).

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Frontend"
        F1("UI Components & Views"):::frontend
        F2("Redux Store"):::frontend
        F3("Global Styles & Assets"):::frontend
        subgraph "Entry Points & Config"
            F4a("index.html"):::frontend
            F4b("main.jsx"):::frontend
            F4c("vite.config.js"):::frontend
        end
        F5("API Service"):::frontend
    end

    subgraph "Backend"
        B1("API Routes"):::backend
        B2("Middleware & Utilities"):::backend
        B3("Controllers"):::backend
        B4("Services"):::backend
        B5("Data Models"):::backend
        B6("DB Config"):::backend
        B7("File Upload Module"):::backend
    end

    DB("Database"):::database

    %% Data Flow Connections
    F5 -->|"HTTP_Request"| B1
    B1 -->|"passes_request"| B2
    B2 -->|"calls"| B3
    B3 -->|"delegates"| B4
    B4 -->|"queries"| B5
    B5 -->|"DB_operations"| DB
    B3 -->|"file_upload"| B7

    %% Click Events for Backend Components
    click B1 "https://github.com/hdang1612/ptit_dormitory/tree/main/ptit_dormitory_be/routes"
    click B2 "https://github.com/hdang1612/ptit_dormitory/tree/main/ptit_dormitory_be/middleware"
    click B3 "https://github.com/hdang1612/ptit_dormitory/tree/main/ptit_dormitory_be/controller"
    click B4 "https://github.com/hdang1612/ptit_dormitory/tree/main/ptit_dormitory_be/services"
    click B5 "https://github.com/hdang1612/ptit_dormitory/tree/main/ptit_dormitory_be/models"
    click B6 "https://github.com/hdang1612/ptit_dormitory/blob/main/ptit_dormitory_be/config/db.js"
    click B7 "https://github.com/hdang1612/ptit_dormitory/tree/main/ptit_dormitory_be/upload"

    %% Click Events for Frontend Components
    click F1 "https://github.com/hdang1612/ptit_dormitory/tree/main/ptit_dormitory_fe/src/components"
    click F2 "https://github.com/hdang1612/ptit_dormitory/tree/main/ptit_dormitory_fe/src/redux"
    click F3 "https://github.com/hdang1612/ptit_dormitory/tree/main/ptit_dormitory_fe/src/style"
    click F4a "https://github.com/hdang1612/ptit_dormitory/blob/main/ptit_dormitory_fe/index.html"
    click F4b "https://github.com/hdang1612/ptit_dormitory/blob/main/ptit_dormitory_fe/src/main.jsx"
    click F4c "https://github.com/hdang1612/ptit_dormitory/blob/main/ptit_dormitory_fe/vite.config.js"
    click F5 "https://github.com/hdang1612/ptit_dormitory/tree/main/ptit_dormitory_fe/src/service"

    %% Classes and Styles
    classDef frontend fill:#ADD8E6,stroke:#000,stroke-width:2px;
    classDef backend fill:#F4A460,stroke:#000,stroke-width:2px;
    classDef database fill:#90EE90,stroke:#000,stroke-width:2px;
```
