# fan9704/garbage_classifier_backend architecture

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

## Overview

##### Project Type and Purpose
- The README clearly states this is “Garbage Classifier Backend” and part of an integrated system that also contains an application, frontend, and hardware components.
- Its main purpose is to provide RESTful and GraphQL APIs for user management, machine control (lock/unlock, linking with users), wallet services, and bank account interactions, among other features.
- It also includes integrations with external services such as Firebase (for FCM tokens), RabbitMQ (for asynchronous messaging, noted as a future/distributed feature), Redis (for session management), and monitoring tools like Prometheus and Grafana.

##### File Structure
- Key directories include:
- src/main/java: Contains the full business logic, broken into packages for controllers, services, repositories, models (entities), configurations, and GraphQL handling.
- The configuration package houses setup for Firebase, RabbitMQ, Global CORS, Redis, security, Swagger (API documentation), and WebSocket.
- GraphQL endpoints are clearly separate under the “GraphQL” directory with further subdivided packages for queries, mutations, and resolvers.
- src/main/resources holds the application properties, Firebase service account credentials, and GraphQL schema definitions.
- Test directories mirror the main structure ensuring unit tests for controllers, services, and repositories.
- Additionally, there are Docker and Maven wrapper files for containerization and builds.

##### README for Additional Insights
- The README shows sample URLs for Swagger UI (for API documentation), Prometheus, and Grafana panels for system monitoring.
- There is a detailed listing of available REST API endpoints and GraphQL integration indicating a rich API surface meant for external clients.
- Overall, the software is deployed as a standalone Spring Boot application, with containerization supported via Docker.

a. Main Components to Include:
- Client Interfaces: Representing end-user clients (including the Swagger-based web interface and mobile/other applications) that interact with the backend.
- REST API Controllers (e.g., UserController, MachineController, etc.)
- GraphQL Endpoints (queries, mutations, resolvers)
- Business Logic / Service Layer: Handling the business operations of registration, wallet management, machine operations, etc.
- Data Access Layer / Repository: Using Spring Data JPA for repository access to entities like User, Machine, Wallet, and others.
- Domain Models: The entity classes that represent the system’s data.
- Configuration Modules: Including security, Swagger API docs, Firebase, RabbitMQ, and Redis.
- External Integrations:
- Monitoring: Prometheus and Grafana panels for system health.
- Messaging Systems: RabbitMQ for distributed messaging (even if it’s noted as a future feature).
- Cloud Services: Firebase for cloud messaging.
- WebSocket/Real-time: Components used for WebSocket communication.
- Database: Though not explicitly shown, the use of JPA implies a relational database underlying the repository layer.
- Other System Projects: Optionally, indicate the overall system linkage to the Application, Frontend, and Hardware projects mentioned in the README.

b. Relationships and Interactions:
- Data flows from external clients (via HTTP/GraphQL) entering through the controllers.
- Interactions from controllers to the service layer and subsequently to the repository layer.
- Monitoring endpoints (Prometheus, Grafana) linked to the backend—possibly through Actuator and Micrometer integration.
- If including the overall system context, show how the backend interfaces with the Frontend, Application, and Hardware components via defined APIs.

c. Architectural Patterns and Design Principles:
- The system follows a layered architecture (controllers, services, repositories).
- It utilizes GraphQL alongside REST, showing a hybrid API model.
- It integrates common enterprise-grade components (messaging, caching, security, and monitoring), highlighting best practices in modular design.

d. Relevant Technologies:
- Spring Boot (for the main application framework)
- Spring Data JPA (for database interactions)
- GraphQL (for flexible API queries/mutations)
- Maven (for project management and builds)
- Docker (for containerizing the application)
- External tools: RabbitMQ, Firebase, Redis, Swagger UI, Prometheus, and Grafana.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Interfaces
    Client["Client Interfaces"]:::external

    %% API Layer
    subgraph "API Layer"
        REST_API["REST API Controllers"]:::api
        GraphQL["GraphQL Endpoints"]:::api
    end

    %% Business Layer
    subgraph "Business Layer"
        Service["Service Layer"]:::service
        Config["Configuration Modules"]:::config
    end

    %% Data Layer
    subgraph "Data Layer"
        Repository["Repository Layer"]:::repository
        Domain["Domain Models"]:::model
    end

    %% Database
    Database["Database"]:::database

    %% External Integrations
    subgraph "External Integrations"
        Firebase["Firebase"]:::external
        RabbitMQ["RabbitMQ"]:::external
        Redis["Redis"]:::external
        Container["Container & Monitoring Setup"]:::external
        Prometheus["Prometheus"]:::external
        Grafana["Grafana"]:::external
        External_Config["External Configs"]:::external
        GraphQL_Config["GraphQL Schemas"]:::external
    end

    %% Connections
    Client -->|"HTTP/GraphQL Request"| REST_API
    Client -->|"HTTP/GraphQL Request"| GraphQL

    REST_API -->|"calls"| Service
    GraphQL -->|"calls"| Service

    Service -->|"invokes"| Repository
    Repository -->|"accesses"| Database
    Repository -->|"maps"| Domain

    Config -->|"configures"| REST_API
    Config -->|"configures"| GraphQL
    Config -->|"configures"| Service
    Config -->|"includes"| External_Config
    Config -->|"includes"| GraphQL_Config
    Config -->|"includes"| Container

    Service -->|"integrates"| Firebase
    Service -->|"sendsMsgTo"| RabbitMQ
    Service -->|"cachesVia"| Redis

    Container -->|"monitorsVia"| Prometheus
    Container -->|"dashboardsAt"| Grafana

    %% Click Events
    click REST_API "https://github.com/fan9704/garbage_classifier_backend/tree/master/src/main/java/com/bezkoder/spring/datajpa/controller"
    click GraphQL "https://github.com/fan9704/garbage_classifier_backend/tree/master/src/main/java/com/bezkoder/spring/datajpa/GraphQL"
    click Service "https://github.com/fan9704/garbage_classifier_backend/tree/master/src/main/java/com/bezkoder/spring/datajpa/service"
    click Repository "https://github.com/fan9704/garbage_classifier_backend/tree/master/src/main/java/com/bezkoder/spring/datajpa/repository"
    click Domain "https://github.com/fan9704/garbage_classifier_backend/tree/master/src/main/java/com/bezkoder/spring/datajpa/model"
    click Config "https://github.com/fan9704/garbage_classifier_backend/tree/master/src/main/java/com/bezkoder/spring/datajpa/configuration"
    click GraphQL_Config "https://github.com/fan9704/garbage_classifier_backend/tree/master/src/main/resources/graphql"
    click External_Config "https://github.com/fan9704/garbage_classifier_backend/tree/master/src/main/resources"
    click Container "https://github.com/fan9704/garbage_classifier_backend/blob/master/docker-compose.yml"

    %% Class Definitions
    classDef api fill:#bbf,stroke:#333,stroke-width:2px;
    classDef service fill:#bfb,stroke:#333,stroke-width:2px;
    classDef repository fill:#ffb,stroke:#333,stroke-width:2px;
    classDef model fill:#fdd,stroke:#333,stroke-width:2px;
    classDef config fill:#dff,stroke:#333,stroke-width:2px;
    classDef external fill:#fdc,stroke:#333,stroke-width:2px;
    classDef database fill:#cfc,stroke:#333,stroke-width:2px,stroke-dasharray: 5 5;
```
