# suaracost/petly architecture

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

- Interactive diagram: https://gitdiagram.com/suaracost/petly
- Repository: https://github.com/suaracost/petly

## Overview

##### Main Components
- Web Layer (Presentation):
- Controllers: Includes various controllers like ClienteController, LoginController, VeterinarioController, etc., that handle incoming HTTP requests.
- View/Template Layer: HTML templates and static resources (CSS, JS, images) used to serve web pages.
- Business Logic Layer:
- Service Interfaces and Implementations: Classes in the “servicio” package (e.g., ClienteServiceImpl, MascotaServiceImpl) implement business rules and orchestrate operations.
- Data Access Layer:
- Repositories: Components in the “repositorio” package interact with the database using Spring Data JPA (e.g., ClienteRepository, MascotaRepository).
- Domain Model:
- Entities: Domain objects located in “entidad” (like Cliente, Mascota, Veterinario) that define the data structure used throughout the application.
- DTOs and Mappers: DTO classes and mapping helpers such as VeterinarioDTO and VeterinarioMapper.
- Security Components:
- Security configuration, JWT filters, authentication/authorization services (e.g., SecurityConfig, JWTAuthenticationFilter, JWTGenerator).
- Error Handling:
- Custom error handling classes in “errorHandling” to manage exceptions such as IdClienteNotFoundException, NotFoundException, etc.
- Configuration & Utilities:
- Spring Boot configuration files (application.properties) and database initialization classes (Databaseinit).
- Testing Components:
- Unit and E2E tests structured in the “src/test” hierarchy ensuring each layer performs as expected.

##### Relationships and Interactions
- The controllers receive HTTP requests and pass them to the corresponding service classes.
- The service layer implements business logic and interacts with repositories to retrieve or update entity information.
- Repositories directly communicate with the H2 database (access via the provided H2 console link in the README).
- Security components, integrated with controllers, filter and validate incoming requests via JWT, enforcing proper authentication and CORS policies.
- DTOs are used to transfer data between the controllers and service layers, ensuring that only the necessary information is exposed.
- Error Handling components catch exceptions thrown from any layer and return appropriate responses.
- Finally, static resources and templates facilitate the generation of dynamic web pages, integrating with the backend logic.

##### Notable Architectural Patterns & Design Principles
- Layered Architecture (MVC Pattern): Separation of concerns between the presentation (controllers/views), business logic (services), and data access layers.
- Dependency Injection: Leveraged by Spring Boot for managing component dependencies.
- RESTful API Practices: Exposing endpoints for client consumption (with Swagger API documentation referenced in the README).
- Security Best Practices: JWT-based authentication and carefully configured security filters.

##### Relevant Technologies and Frameworks
- Spring Boot Framework
- Spring Data JPA for database interactions
- Spring Security for authentication and authorization
- H2 Database for a lightweight, in-memory or file-based database
- Maven for project build and dependency management
- Swagger for API documentation
- Thymeleaf (implied by the “templates” folder) for server-side HTML rendering, if used
- File-based static resources for CSS and JavaScript assets

##### Specific Elements
- Clear Labels:
- Each layer (Controllers, Services, Repositories, Domain, Security, Error Handling) should be clearly labeled.
- External systems: Angular frontend, H2 Database, and API documentation (Swagger).
- Visual Distinction:
- Annotations that point out:
- The use of JWT-based authentication.
- The RESTful API design and the integration of Swagger documentation.
- The static resource/template management that serves the web pages.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "External Interfaces"
        AF["Angular Frontend"]:::external
        SWAG["Swagger API Docs"]:::external
    end

    subgraph "Backend System"
        subgraph "Web Layer (Presentation)"
            CTR["Controllers"]:::controller
            VIEW["HTML Templates"]:::view
            STATIC["Static Resources"]:::view
        end
        subgraph "Business Logic Layer"
            SRV["Services"]:::service
        end
        subgraph "Data Access Layer"
            REP["Repositories"]:::repository
        end
        subgraph "Domain Model"
            ENT["Entities"]:::domain
            DTO["DTOs & Mappers"]:::domain
        end
        SEC["Security (JWT)"]:::security
        EH["Error Handling"]:::error
        subgraph "Configuration & Utilities"
            CFG["application.properties"]:::config
            DBI["Database Initialization"]:::config
        end
        TEST["Testing Components"]:::testing
        H2["H2 Database"]:::database
    end

    AF -->|"REST_API_call"| SEC
    SEC -->|"Validates"| CTR
    SWAG -->|"Documents_API"| CTR
    CTR -->|"Invokes"| SRV
    SRV -->|"Calls"| REP
    REP -->|"Data_access"| H2

    CTR -->|"Renders_HTML"| VIEW
    CTR -->|"Renders_static"| STATIC

    SRV -->|"Uses_DTO"| DTO
    DTO -->|"Maps_to"| ENT

    CTR -.->|"Catches"| EH
    SRV -.->|"Catches"| EH
    REP -.->|"Catches"| EH

    CFG -->|"Configures"| REP
    DBI -->|"Initializes"| H2

    TEST -.->|"Tests"| CTR
    TEST -.->|"Tests"| SRV
    TEST -.->|"Tests"| REP

    classDef external fill:#F0E68C,stroke:#000,stroke-width:2px;
    classDef controller fill:#ADD8E6,stroke:#000,stroke-width:2px;
    classDef view fill:#87CEFA,stroke:#000,stroke-width:2px;
    classDef service fill:#90EE90,stroke:#000,stroke-width:2px;
    classDef repository fill:#FFFF99,stroke:#000,stroke-width:2px;
    classDef domain fill:#D8BFD8,stroke:#000,stroke-width:2px;
    classDef security fill:#FFDAB9,stroke:#000,stroke-width:2px;
    classDef error fill:#FF7F7F,stroke:#000,stroke-width:2px;
    classDef config fill:#D3D3D3,stroke:#000,stroke-width:2px;
    classDef testing fill:#E0E0E0,stroke:#000,stroke-width:2px;
    classDef database fill:#C0C0C0,stroke:#000,stroke-width:2px;

    click CTR "https://github.com/suaracost/petly/tree/main/demo/src/main/java/com/example/demo/controlador/"
    click VIEW "https://github.com/suaracost/petly/tree/main/demo/src/main/resources/templates/"
    click STATIC "https://github.com/suaracost/petly/tree/main/demo/src/main/resources/static/"
    click SRV "https://github.com/suaracost/petly/tree/main/demo/src/main/java/com/example/demo/servicio/"
    click REP "https://github.com/suaracost/petly/tree/main/demo/src/main/java/com/example/demo/repositorio/"
    click ENT "https://github.com/suaracost/petly/tree/main/demo/src/main/java/com/example/demo/entidad/"
    click DTO "https://github.com/suaracost/petly/tree/main/demo/src/main/java/com/example/demo/DTO/"
    click SEC "https://github.com/suaracost/petly/tree/main/demo/src/main/java/com/example/demo/security/"
    click EH "https://github.com/suaracost/petly/tree/main/demo/src/main/java/com/example/demo/errorHandling/"
    click CFG "https://github.com/suaracost/petly/blob/main/demo/src/main/resources/application.properties"
    click DBI "https://github.com/suaracost/petly/blob/main/demo/src/main/java/com/example/demo/entidad/Databaseinit.java"
    click TEST "https://github.com/suaracost/petly/tree/main/demo/src/test/"
```
