# joserljdev/socialbooks-api architecture

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

## Overview

##### Project Type and Purpose
- The project is a backend RESTful API focused on managing book-related information and user interactions.
- JWT authentication underlines the security requirement, which must be clearly depicted.
- The API, built with Spring, follows typical layered architecture principles common to Java/Spring applications.

##### File Structure
- Key directories include “src/main/java/br/com/joserljdev/socialbooks” – which contains domain classes, resource (controller) classes, repository classes, services, exception handling, and a complete security package.
- The presence of packages like domain, repository, resources, services, and security indicates an MVC-like layered architecture along with distinct security handling.
- Configuration files (like application.properties in src/main/resources and pom.xml) support that this is a Maven-based Spring Boot application.

##### Main Components of the System
a. API Entry Point:
- SocialbooksApplication.java acts as the starting point.
b. Controller (Resource) Layer:
- Components from the “resources” package (e.g., AutoresResource.java, LivrosResources.java) handle HTTP requests and responses.
c. Service Layer:
- Service classes (like AutoresService.java and LivrosService.java) encapsulate business logic and coordinate between controllers and repositories.
d. Repository Layer:
- Data access is managed by repository interfaces (e.g., AutoresRepository.java, LivrosRepository.java, etc.).
e. Domain Model:
- Domain classes under “domain” (Autor, Livro, Comentario, etc.) represent data entities.
f. Security Components:
- All files under the “security” package and nested “jwt” packages illustrate the JWT authentication flow.
- This includes filters (JwtAuthenticationFilter, JwtAuthorizationFilter), token utilities (JwtUtil), and exception/error handling for access control.
g. Exception Handling:
- The “handler” package (e.g., ResourceExceptionHandler) and services/exceptions provide a systematic way to handle errors.
h. Persistence and Initial Data:
- “import.sql” demonstrates an initial data seeding mechanism.

##### Relationships and Interactions
- HTTP requests arrive at the resource layer (controllers) and pass to the service layer for processing.
- The service layer coordinates with the repository layer for database interactions.
- The domain model is used throughout to represent data.
- JWT security filters intercept requests to validate credentials, protecting API endpoints.
- Error handling is centralized through exception handlers to manage API faults.

##### Architectural Patterns and Design Principles to Highlight
- The layered (or n-tier) architecture with distinct separation of concerns (controllers, services, repositories, domain).
- Security is implemented as a cross-cutting concern via JWT filters and Spring Security configurations.
- Use of exception handling best practices to deliver consistent error responses.
- Maven-based project structure for dependency management and build configuration.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Client
    Client["External HTTP Request"]:::client

    %% Security Layer
    subgraph "Security Layer"
        JWTAuthFilter["JwtAuthenticationFilter & JwtAuthorizationFilter"]:::security
        SecurityConfig["SecurityConfiguration"]:::security
        UserDetailsService["UserDetailsServiceImpl"]:::security
        JWTComponents["JWT Utilities & Components"]:::security
        JWTHandlers["JWT Handlers (AccessDenied, Unauthorized)"]:::security
    end

    %% Controller Layer
    subgraph "Controller Layer"
        AutoresCtrl["AutoresResource"]:::controller
        LivrosCtrl["LivrosResources"]:::controller
    end

    %% Service Layer
    subgraph "Service Layer"
        AutoresSvc["AutoresService"]:::service
        LivrosSvc["LivrosService"]:::service
    end

    %% Repository and Domain Layer
    subgraph "Repository and Domain Layer"
        Repositories["Repositories"]:::repository
        Domain["Domain Models"]:::domain
    end

    %% Exception Handling
    subgraph "Exception Handling"
        ExceptionHandler["Global Exception Handler"]:::exception
        ServiceExceptions["Service Exception Classes"]:::exception
    end

    %% API Entry Point
    App["SocialbooksApplication (Entry Point)"]:::entry

    %% Persistence and Initial Data
    Persistence["Initial Data (import.sql)"]:::persistence

    %% Connections
    Client -->|"passesThrough"| JWTAuthFilter
    JWTAuthFilter -->|"securedRequest"| AutoresCtrl
    JWTAuthFilter -->|"securedRequest"| LivrosCtrl

    AutoresCtrl -->|"callsBusinessLogic"| AutoresSvc
    LivrosCtrl -->|"callsBusinessLogic"| LivrosSvc

    AutoresSvc -->|"accessesData"| Repositories
    LivrosSvc -->|"accessesData"| Repositories

    Repositories -->|"usesModel"| Domain

    AutoresCtrl -->|"handlesError"| ExceptionHandler
    LivrosCtrl -->|"handlesError"| ExceptionHandler
    AutoresSvc -->|"serviceError"| ServiceExceptions
    LivrosSvc -->|"serviceError"| ServiceExceptions

    App -->|"bootsSystem"| Client

    Persistence -->|"populates"| Domain

    SecurityConfig -->|"configures"| JWTAuthFilter
    SecurityConfig -->|"configures"| UserDetailsService
    JWTComponents -->|"assists"| JWTAuthFilter
    JWTHandlers -->|"handlesSecError"| JWTAuthFilter

    %% Click Events
    click App "https://github.com/joserljdev/socialbooks-api/blob/master/src/main/java/br/com/joserljdev/socialbooks/SocialbooksApplication.java"
    click AutoresCtrl "https://github.com/joserljdev/socialbooks-api/blob/master/src/main/java/br/com/joserljdev/socialbooks/resources/AutoresResource.java"
    click LivrosCtrl "https://github.com/joserljdev/socialbooks-api/blob/master/src/main/java/br/com/joserljdev/socialbooks/resources/LivrosResources.java"
    click AutoresSvc "https://github.com/joserljdev/socialbooks-api/blob/master/src/main/java/br/com/joserljdev/socialbooks/services/AutoresService.java"
    click LivrosSvc "https://github.com/joserljdev/socialbooks-api/blob/master/src/main/java/br/com/joserljdev/socialbooks/services/LivrosService.java"
    click Repositories "https://github.com/joserljdev/socialbooks-api/tree/master/src/main/java/br/com/joserljdev/socialbooks/repository"
    click Domain "https://github.com/joserljdev/socialbooks-api/tree/master/src/main/java/br/com/joserljdev/socialbooks/domain"
    click SecurityConfig "https://github.com/joserljdev/socialbooks-api/blob/master/src/main/java/br/com/joserljdev/socialbooks/security/SecurityConfiguration.java"
    click UserDetailsService "https://github.com/joserljdev/socialbooks-api/blob/master/src/main/java/br/com/joserljdev/socialbooks/security/UserDetailsServiceImpl.java"
    click JWTComponents "https://github.com/joserljdev/socialbooks-api/tree/master/src/main/java/br/com/joserljdev/socialbooks/security/jwt"
    click JWTHandlers "https://github.com/joserljdev/socialbooks-api/tree/master/src/main/java/br/com/joserljdev/socialbooks/security/jwt/handler"
    click ExceptionHandler "https://github.com/joserljdev/socialbooks-api/blob/master/src/main/java/br/com/joserljdev/socialbooks/handler/ResourceExceptionHandler.java"
    click ServiceExceptions "https://github.com/joserljdev/socialbooks-api/tree/master/src/main/java/br/com/joserljdev/socialbooks/services/exceptions"
    click Persistence "https://github.com/joserljdev/socialbooks-api/blob/master/src/main/resources/import.sql"

    %% Styles
    classDef client fill:#b3e5fc,stroke:#0277bd,stroke-width:2px,color:#000;
    classDef controller fill:#81d4fa,stroke:#01579b,stroke-width:2px,color:#000;
    classDef service fill:#ffb74d,stroke:#ef6c00,stroke-width:2px,color:#000;
    classDef repository fill:#aed581,stroke:#558b2f,stroke-width:2px,color:#000;
    classDef domain fill:#ffcc80,stroke:#f57c00,stroke-width:2px,color:#000;
    classDef security fill:#e57373,stroke:#c62828,stroke-width:2px,color:#000;
    classDef exception fill:#ce93d8,stroke:#6a1b9a,stroke-width:2px,color:#000;
    classDef entry fill:#90a4ae,stroke:#37474f,stroke-width:2px,color:#000;
    classDef persistence fill:#ffe082,stroke:#ff6f00,stroke-width:2px,color:#000;
```
