# dohuyit/backend-qnhg architecture

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

- Interactive diagram: https://gitdiagram.com/dohuyit/backend-qnhg
- Repository: https://github.com/dohuyit/backend-qnhg

## Overview

##### Project type and purpose
- This is a full-stack Laravel web application that supports two user domains:
- An Admin panel (managing categories, dishes, combos, orders, reservations, users, permissions).
- A Client interface (browsing menu, managing cart, placing orders, making reservations).
- It exposes RESTful/HTTP APIs for both domains, real-time event broadcasting for order/reservation notifications, background jobs for email/notification delivery, and integrates with external services (mail, payment gateways, face recognition API).

##### File structure
- Top-level directories:
- app/Http/Controllers (separated into Admin and Client subfolders) – presentation layer.
- app/Services & app/Repositories – business logic and data access layers following the Service-Repository pattern.
- app/Models – Eloquent ORM models for each domain entity.
- app/Events & app/Listeners – event-driven architecture for side-effects (notifications, counters).
- routes/ – separate route files for admin, client, API, and web.
- resources/js & resources/views – frontend assets (Vue or plain JS & Blade templates).
- config/, bootstrap/, public/, storage/ – Laravel infrastructure and deployment.
- database/ – migrations, factories, seeders for schema and test data.
- artisan, composer.json, phpunit.xml, Vite/Tailwind configs – build, dependency, and tooling.

##### README for additional insights
- The README is the standard Laravel boilerplate and confirms the use of core features: routing, Eloquent ORM, migrations, queues, broadcasting.
- REALTIME_SETUP.md (not shown) likely describes configuration for Pusher or Socket server.

- Client UI (Browser or Mobile)
- Public Web UI (Blade templates)
- Admin SPAs/Pages (resources/js + Vite)
- HTTP/API Layer (Laravel Controllers: Admin & Client)
- Application Services (app/Services)
- Repositories & Models (app/Repositories + app/Models + Eloquent)
- Relational Database (MySQL/PostgreSQL)
- Cache/Session & Queue backend (Redis)
- Event Broadcasting (Laravel Echo + Pusher or Socket)
- External Services: Email SMTP, Payment Gateway, Face Recognition API
- Background Worker (laravel-queue: Consumers running queued Jobs & Listeners)

b. Relationships and interactions
- Controllers → Services (dependency injection) → Repositories
- Repositories → Database (ORM queries)
- Certain controller or service actions → dispatch Events → Queue → Listeners → Notifications / Mail / Real-time Broadcast
- Controllers → call external SDKs (PaymentService → Payment Gateway; FaceRecognitionService → Face API)
- Broadcasting: Event → Broadcast to WebSocket server → Client UI (real-time notifications)

c. Architectural patterns / design principles
- MVC at the controller-view-model level
- Service-Repository to decouple business logic from persistence
- Event-driven for side effects and real-time updates
- Layered Architecture: Presentation → Application → Domain/Data → Infrastructure
- Dependency Injection via Laravel’s IoC container
- Single codebase servicing multiple domains (admin / client) via route grouping and middleware

d. Key technologies / frameworks / libraries
- PHP 8.x, Laravel Framework
- Eloquent ORM, Migrations, Factories, Seeders
- Redis (cache, session, queues)
- Laravel Queue Workers (e.g., Supervisor)
- Laravel Echo + Pusher or Socket server for broadcasting
- Blade Templates & Vite (with TailwindCSS, JavaScript assets)
- SMTP (Mailgun/SendGrid) and Payment SDK (Stripe/PayPal)
- Face Recognition SDK via HTTP client

##### Detail
- Sub-components that play a key architectural role (e.g., Queue Worker, Event Service Provider, Broadcast Server).
- Middleware flows (e.g., auth:sanctum, role checks) if space permits.
- Both synchronous (HTTP) and asynchronous (Queue/Broadcast) interactions.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Presentation Layer
    subgraph "Presentation Layer"
        direction TB
        ClientUI["Client UI (Browser/Mobile)"]:::frontend
        AdminUI["Admin UI (SPA/Blade)"]:::frontend
    end

    %% API/Controller Layer
    subgraph "API/Controller Layer"
        direction TB
        AdminControllers["Admin HTTP/API Layer\n(app/Http/Controllers/Admin)"]:::controller
        ClientControllers["Client HTTP/API Layer\n(app/Http/Controllers/Client)"]:::controller
        APIControllers["API Routes\n(routes/api.php)"]:::controller
        WebControllers["Web Routes\n(routes/web.php)"]:::controller
    end

    %% Business Layer
    subgraph "Business Layer"
        direction TB
        Services["Application Services\n(app/Services)"]:::service
        Repositories["Data Repositories\n(app/Repositories)"]:::service
    end

    %% Data Layer
    subgraph "Data Layer"
        direction TB
        Models["Eloquent Models\n(app/Models)"]:::model
        Database["Relational Database\n(MySQL/PostgreSQL)"]:::database
    end

    %% Infrastructure Layer
    subgraph "Infrastructure Layer"
        direction TB
        Redis["Redis\n(Cache/Session/Queue)"]:::infra
        QueueWorker["Queue Worker\n(Listeners & Jobs)"]:::infra
        BroadcastServer["Broadcast Server\n(Laravel Echo/Pusher)"]:::infra
        ExternalMail["Email SMTP\n(app/Mail & resources/views/emails)"]:::external
        ExternalPayment["Payment Gateway\n(Stripe/PayPal SDK)"]:::external
        ExternalFace["Face Recognition API"]:::external
    end

    %% Event Components
    subgraph "Event System"
        direction TB
        Events["Event Definitions\n(app/Events)"]:::event
        Listeners["Event Listeners\n(app/Listeners)"]:::event
    end

    %% Flows
    ClientUI -->|"HTTPS GET/POST"| ClientControllers
    AdminUI -->|"HTTPS GET/POST"| AdminControllers
    ClientControllers -->|"calls"| Services
    AdminControllers -->|"calls"| Services
    APIControllers -->|"routes to Controllers"| ClientControllers
    WebControllers -->|"routes to Controllers"| ClientControllers
    Services -->|"uses"| Repositories
    Repositories -->|"ORM Queries"| Models
    Models -->|"reads/writes"| Database
    ClientControllers -->|"dispatch"| Events
    AdminControllers -->|"dispatch"| Events
    Events -->|"queued"| QueueWorker
    QueueWorker -->|"processes"| Listeners
    Listeners -->|"sends"| ExternalMail
    Listeners -->|"sends"| ExternalPayment
    Listeners -->|"sends"| ExternalFace
    Events -->|"broadcast"| BroadcastServer
    BroadcastServer -->|"real-time"| ClientUI
    BroadcastServer -->|"real-time"| AdminUI
    Redis -.->|Cache/Session| ClientControllers
    Redis -.->|Cache/Session| AdminControllers
    Redis -.->|Queue Driver| QueueWorker

    %% Click Events
    click AdminControllers "https://github.com/dohuyit/backend-qnhg/tree/master/app/Http/Controllers/Admin"
    click ClientControllers "https://github.com/dohuyit/backend-qnhg/tree/master/app/Http/Controllers/Client"
    click Events "https://github.com/dohuyit/backend-qnhg/tree/master/app/Events"
    click Listeners "https://github.com/dohuyit/backend-qnhg/tree/master/app/Listeners"
    click Services "https://github.com/dohuyit/backend-qnhg/tree/master/app/Services"
    click Repositories "https://github.com/dohuyit/backend-qnhg/tree/master/app/Repositories"
    click Models "https://github.com/dohuyit/backend-qnhg/tree/master/app/Models"
    click APIControllers "https://github.com/dohuyit/backend-qnhg/blob/master/routes/api.php"
    click WebControllers "https://github.com/dohuyit/backend-qnhg/blob/master/routes/web.php"
    click AdminControllers "https://github.com/dohuyit/backend-qnhg/blob/master/routes/admin/admin.php"
    click ClientControllers "https://github.com/dohuyit/backend-qnhg/blob/master/routes/client/client.php"
    click ClientControllers "https://github.com/dohuyit/backend-qnhg/blob/master/routes/client/auth.php"
    click Services "https://github.com/dohuyit/backend-qnhg/blob/master/app/Providers/RepoServiceProvider.php"
    click Listeners "https://github.com/dohuyit/backend-qnhg/blob/master/app/Providers/EventServiceProvider.php"
    click ExternalMail "https://github.com/dohuyit/backend-qnhg/tree/master/app/Mail"
    click ExternalMail "https://github.com/dohuyit/backend-qnhg/tree/master/resources/views/emails"
    click Models "https://github.com/dohuyit/backend-qnhg/tree/master/database/migrations"
    click Models "https://github.com/dohuyit/backend-qnhg/tree/master/database/factories"
    click Models "https://github.com/dohuyit/backend-qnhg/tree/master/database/seeders"
    click Redis "https://github.com/dohuyit/backend-qnhg/blob/master/config/queue.php"
    click Redis "https://github.com/dohuyit/backend-qnhg/blob/master/config/cache.php"
    click BroadcastServer "https://github.com/dohuyit/backend-qnhg/blob/master/config/broadcasting.php"
    click BroadcastServer "https://github.com/dohuyit/backend-qnhg/blob/master/routes/channels.php"
    click QueueWorker "https://github.com/dohuyit/backend-qnhg/tree/master/app/Console/Commands"
    click ClientUI "https://github.com/dohuyit/backend-qnhg/blob/master/resources/views/*.blade.php"
    click AdminUI "https://github.com/dohuyit/backend-qnhg/tree/master/resources/js"
    click AdminUI "https://github.com/dohuyit/backend-qnhg/blob/master/resources/css/app.css"

    %% Styles
    classDef frontend fill:#BBDEFB,stroke:#1E88E5,color:#0D47A1
    classDef controller fill:#C8E6C9,stroke:#388E3C,color:#1B5E20
    classDef service fill:#FFE0B2,stroke:#F57C00,color:#E65100
    classDef model fill:#FFCCBC,stroke:#D84315,color:#BF360C
    classDef database fill:#FFF9C4,stroke:#FBC02D,color:#F57F17
    classDef infra fill:#CFD8DC,stroke:#607D8B,color:#263238
    classDef event fill:#E1BEE7,stroke:#8E24AA,color:#4A148C
    classDef external fill:#FFCDD2,stroke:#E53935,color:#B71C1C
```
