# hiddro/back_clinic architecture

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

- Interactive diagram: https://gitdiagram.com/hiddro/back_clinic
- Repository: https://github.com/hiddro/back_clinic

## Overview

##### Main Components of the System
a. Application Entry Point & Configuration:
- ClinicApplication.java – the main class to bootstrap the application.
- Configuration classes (MongoConfig, ResourceWebPropertiesConfig) that set up the database and web-related properties.
- application.yml – external configuration including database, server, and other environment-specific settings.
b. Controllers (Presentation Layer):
- LoginController, RoleController, and UserController handle incoming HTTP requests.
- They serve as the interface exposing the API endpoints.
c. Service Layer:
- Services such as CrudService, UserService, RoleService, and TokenService (with corresponding implementations) encapsulate the business logic.
- They process input from controllers and coordinate with the repositories.
d. Repository Layer (Data Access):
- GenericRepo, RoleRepositories, and UserRepositories are responsible for interacting with MongoDB (as suggested by MongoConfig).
- This layer ensures data retrieval and persistence.
e. Security Infrastructure:
- Security config (e.g., WebSecurityConfig) and classes like AuthenticationManager, SecurityContextRepository, and JWTUtil manage authentication and authorization.
- Contains builder classes for token and user request/response transformation.
f. Utilities and Builders:
- Builder classes (e.g., RoleRequestBuilder, UserResponseBuilder, etc.) help in constructing request and response objects.
- Commons utilities, constants, and exception handlers standardize operations and error responses.

##### Relationships and Interactions
- Upon receiving an HTTP request, one of the Controllers routes the request.
- The Controllers invoke corresponding Service methods which implement business logic.
- Services, in turn, interact with Repositories to perform database CRUD operations.
- The Security layer intercepts requests for authentication/authorization and uses JWTUtil and AuthenticationManager.
- The Builders all support conversion between different object layers (e.g., converting a database model to a response DTO) enhancing separation of concerns.
- Exception handling is performed via custom handlers (e.g., WebExceptionHandler and ExceptionHandler from commons).

##### Architectural Patterns and Design Principles
- Layered Architecture: Clear separation between controllers (API), services (business logic), and repositories (data access).
- Builder Pattern: Used for constructing and translating various request/response objects.
- Externalized Configuration: Use of application.yml and configuration classes for managing environment-specific settings.
- Security Best Practices: Implementing JWT and centralized authentication via Spring Security configurations.
- Dependency Injection: Likely using Spring Boot’s dependency injection to wire components together.
- Exception Handling: Dedicated classes to manage and encapsulate error responses.

## Diagram (Mermaid)

```mermaid
graph TD
    %% Application Entry & Configuration
    subgraph "Application Entry & Configuration"
        AP("ClinicApplication.java"):::config
        MC("MongoConfig"):::config
        RWPC("ResourceWebPropertiesConfig"):::config
        AY("application.yml"):::config
    end

    %% Controllers (Presentation Layer)
    subgraph "Controllers (Presentation Layer)"
        LC("LoginController"):::controller
        RC("RoleController"):::controller
        UC("UserController"):::controller
    end

    %% Service Layer
    subgraph "Service Layer"
        CSI("CrudService (I)"):::service
        CSImpl("CrudService (Impl)"):::service
        USI("UserService (I)"):::service
        USImpl("UserService (Impl)"):::service
        RSI("RoleService (I)"):::service
        RSImpl("RoleService (Impl)"):::service
        TSI("TokenService (I)"):::service
        TSImpl("TokenService (Impl)"):::service
    end

    %% Repository Layer
    subgraph "Repository Layer"
        GR("GenericRepo"):::repository
        RR("RoleRepositories"):::repository
        UR("UserRepositories"):::repository
    end

    %% Security Infrastructure
    subgraph "Security"
        WSC("WebSecurityConfig"):::security
        AM("AuthenticationManager"):::security
        SCR("SecurityContextRepository"):::security
        JWT("JWTUtil"):::security
    end

    %% Utilities & Builders
    subgraph "Utilities & Builders"
        RB("RoleRequestBuilder"):::builder
        RRB("RoleResponseBuilder"):::builder
        TRB("TokenResponseBuilder"):::builder
        UB("UserRequestBuilder"):::builder
        URB("UserResponseBuilder"):::builder
        CU("Commons Utility"):::builder
        EH("ExceptionHandler"):::builder
        WEH("WebExceptionHandler"):::builder
    end

    %% External Client initiating HTTP requests
    HC("HTTP Client"):::external

    %% Data Flow Connections
    HC --> WSC
    WSC --> LC
    WSC --> RC
    WSC --> UC

    %% Controller to Service interactions
    LC -->|"calls"| TSI
    TSI --> TSImpl

    RC -->|"calls"| RSI
    RSI --> RSImpl

    UC -->|"calls"| USI
    USI --> USImpl

    %% Service to Repository interactions
    CSImpl -->|"uses"| GR
    USImpl -->|"queries"| UR
    RSImpl -->|"queries"| RR
    TSImpl -->|"queries"| GR

    %% Service to Builders interactions
    USImpl -->|"builds"| UB
    USImpl -->|"builds"| URB
    RSImpl -->|"builds"| RB
    RSImpl -->|"builds"| RRB
    TSImpl -->|"builds"| TRB

    %% Services using Commons Utility
    USImpl --> CU
    RSImpl --> CU
    TSImpl --> CU

%% Click Events
click AP "https://github.com/hiddro/back_clinic/blob/master/src/main/java/com/hanpeq/chavez/clinic/ClinicApplication.java"
click MC "https://github.com/hiddro/back_clinic/blob/master/src/main/java/com/hanpeq/chavez/clinic/config/MongoConfig.java"
click RWPC "https://github.com/hiddro/back_clinic/blob/master/src/main/java/com/hanpeq/chavez/clinic/config/ResourceWebPropertiesConfig.java"
click AY "https://github.com/hiddro/back_clinic/blob/master/src/main/resources/application.yml"

click LC "https://github.com/hiddro/back_clinic/blob/master/src/main/java/com/hanpeq/chavez/clinic/controller/LoginController.java"
click RC "https://github.com/hiddro/back_clinic/blob/master/src/main/java/com/hanpeq/chavez/clinic/controller/RoleController.java"
click UC "https://github.com/hiddro/back_clinic/blob/master/src/main/java/com/hanpeq/chavez/clinic/controller/UserController.java"

click CSI "https://github.com/hiddro/back_clinic/blob/master/src/main/java/com/hanpeq/chavez/clinic/service/CrudService.java"
click CSImpl "https://github.com/hiddro/back_clinic/blob/master/src/main/java/com/hanpeq/chavez/clinic/service/impl/CrudServiceImpl.java"
click USI "https://github.com/hiddro/back_clinic/blob/master/src/main/java/com/hanpeq/chavez/clinic/service/UserService.java"
click USImpl "https://github.com/hiddro/back_clinic/blob/master/src/main/java/com/hanpeq/chavez/clinic/service/impl/UserServiceImpl.java"
click RSI "https://github.com/hiddro/back_clinic/blob/master/src/main/java/com/hanpeq/chavez/clinic/service/RoleService.java"
click RSImpl "https://github.com/hiddro/back_clinic/blob/master/src/main/java/com/hanpeq/chavez/clinic/service/impl/RoleServiceImpl.java"
click TSI "https://github.com/hiddro/back_clinic/blob/master/src/main/java/com/hanpeq/chavez/clinic/service/TokenService.java"
click TSImpl "https://github.com/hiddro/back_clinic/blob/master/src/main/java/com/hanpeq/chavez/clinic/service/impl/TokenServiceImpl.java"

click GR "https://github.com/hiddro/back_clinic/blob/master/src/main/java/com/hanpeq/chavez/clinic/repository/GenericRepo.java"
click RR "https://github.com/hiddro/back_clinic/blob/master/src/main/java/com/hanpeq/chavez/clinic/repository/RoleRepositories.java"
click UR "https://github.com/hiddro/back_clinic/blob/master/src/main/java/com/hanpeq/chavez/clinic/repository/UserRepositories.java"

click WSC "https://github.com/hiddro/back_clinic/blob/master/src/main/java/com/hanpeq/chavez/clinic/security/config/WebSecurityConfig.java"
click AM "https://github.com/hiddro/back_clinic/blob/master/src/main/java/com/hanpeq/chavez/clinic/security/AuthenticationManager.java"
click SCR "https://github.com/hiddro/back_clinic/blob/master/src/main/java/com/hanpeq/chavez/clinic/security/SecurityContextRepository.java"
click JWT "https://github.com/hiddro/back_clinic/blob/master/src/main/java/com/hanpeq/chavez/clinic/security/utils/JWTUtil.java"

click RB "https://github.com/hiddro/back_clinic/blob/master/src/main/java/com/hanpeq/chavez/clinic/builder/RoleRequestBuilder.java"
click RRB "https://github.com/hiddro/back_clinic/blob/master/src/main/java/com/hanpeq/chavez/clinic/builder/RoleResponseBuilder.java"
click TRB "https://github.com/hiddro/back_clinic/blob/master/src/main/java/com/hanpeq/chavez/clinic/builder/TokenResponseBuilder.java"
click UB "https://github.com/hiddro/back_clinic/blob/master/src/main/java/com/hanpeq/chavez/clinic/builder/UserRequestBuilder.java"
click URB "https://github.com/hiddro/back_clinic/blob/master/src/main/java/com/hanpeq/chavez/clinic/builder/UserResponseBuilder.java"
click CU "https://github.com/hiddro/back_clinic/blob/master/src/main/java/com/hanpeq/chavez/clinic/utils/commons/Commons.java"
click EH "https://github.com/hiddro/back_clinic/blob/master/src/main/java/com/hanpeq/chavez/clinic/utils/commons/ExceptionHandler.java"
click WEH "https://github.com/hiddro/back_clinic/blob/master/src/main/java/com/hanpeq/chavez/clinic/exceptions/WebExceptionHandler.java"

%% Styling
classDef config fill:#fbf,stroke:#333,stroke-width:2px;
classDef controller 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 security fill:#fbb,stroke:#333,stroke-width:2px;
classDef builder fill:#aff,stroke:#333,stroke-width:2px;
classDef external fill:#eef,stroke:#333,stroke-width:2px;
```
