# natanius18/university-api architecture

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

- Interactive diagram: https://gitdiagram.com/natanius18/university-api
- Repository: https://github.com/natanius18/university-api

## Overview

This project is a Spring Boot-based REST API designed as a university platform where teachers can create tests, and students can take these tests and view their results. The platform uses a clear layered architecture with controllers, services, repositories, entities, security configurations, and scheduled tasks. The system also interacts with external microservices (for notifications and logging) and uses both a relational database (PostgreSQL, managed by Flyway) and MongoDB (for statistics).

##### Main Components
- Application Entry Point: Mark the UniversityApplication class as the starting point.
- Controllers: Represent all the API endpoints (e.g., ApproveController, AuthenticationController, ConfirmationController, ResponseController, StudentController, TeacherController, TestController, TestStatisticsController). Display them as the “API Layer” handling inbound HTTP requests.
- Service Layer: Include service interfaces and their implementations (e.g., PersonService, TestService, TeacherService, StudentService, ResponseService, TestStatisticsService, and the testing strategies in the test package). Show that the controllers delegate business logic to these services.
- Data Access Layer: Illustrate the repositories (AnswerRepository, PersonRepository, etc.) that abstract database operations.
- Domain Model: Show the entity classes (Person, Teacher, Student, Test, Question, Answer, Response, TestStatistics) and the DTOs which help in data transportation between layers.
- Security Components: Highlight the Spring Security configuration including SecurityConfig, JwtTokenProvider, JwtTokenFilter, JwtConfigurer, and UserDetailsServiceImpl. Indicate that these modules intercept requests for authentication and authorization through JWT.
- MongoDB for Statistics: Since the project stores test statistics in MongoDB, include a separate component indicating its role to capture test attempts, results, and filtering (via mongo filters).
- Scheduled Tasks: Include ScheduledTasks that may trigger periodic jobs (e.g., for cleanup, reminders, etc.).
- External Microservices:
- Notification Microservice: Represented as an external system that handles email notifications (for account confirmation and daily test statistics).
- Logs Microservice: Represented as a separate component managing logging functionality and user activity logs.
- Messaging and Integration: Illustrate the use of Kafka and ZooKeeper for inter-service communication between this API and other microservices.

##### Relationships and Interactions
- From Controllers, show data flowing to the Service layer, which encapsulates business logic and calls repository methods.
- That the Repository layer interacts with the PostgreSQL database for CRUD operations.
- That security filters (JWT filters) intercept API calls before reaching the controllers.
- Flow where Flyway migrations initialize or update the database schema at application start-up.

##### Architectural Patterns and Design Principles
- Adhere to a layered architecture (presentation, business, persistence) which clearly demarcates your controllers, services, and repositories.
- Use Dependency Injection and Inversion of Control principles as provided by Spring Boot.
- How the security configuration “wraps” the system to enforce authentication/authorization via JWT.
- That the testing modules (unit and Cucumber tests) validate functionality across different layers.

##### Relevant Technologies and Frameworks
- Java 11 along with Spring Boot 2.7.0, Spring Security, and Spring Data (Hibernate-Core).
- Flyway for database migration and PostgreSQL as the primary relational database.
- MongoDB for storing test statistics.
- Swagger UI is used for API documentation.
- Kafka (with ZooKeeper) is used for integration with external services.
- Logging and Notification microservices are integrated via external REST API calls.

- Entry point and main application container (UniversityApplication).
- API Controllers, depicted in a grouped “Web/API layer”.
- Service components that manage business logic and orchestrate flow.
- Repository classes that manage data persistence to PostgreSQL.
- Entity and DTO models.
- Security module: SecurityConfig, JwtTokenFilter, etc., intercepting requests.
- Database components:
- PostgreSQL (with Flyway migrations) for core test data.
- MongoDB for handling statistics.
- External integrations:
- Notification Microservice.
- Logs Microservice.
- Kafka (message queues).
- Scheduled Tasks for periodic operations.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Global Entities
    Client["Client"]:::external

    %% Application Core
    subgraph "Application Core"
        UniversityApplication["UniversityApplication"]:::entry
        SecurityComponents["Security Components:\nSecurityConfig, JwtTokenFilter,\nJwtTokenProvider, UserDetailsServiceImpl"]:::security
    end

    %% Security Filter
    SecurityFilter["JWT Security Filter"]:::security

    %% Web/API Layer
    subgraph "Web/API Layer"
        Controllers["API Controllers"]:::api
    end

    %% Service Layer
    subgraph "Service Layer"
        ServiceInterfaces["Service Interfaces"]:::service
        ServiceImplementations["Service Implementations"]:::service
        Scheduled["Scheduled Tasks"]:::schedule
    end

    %% Data Access Layer
    subgraph "Data Access Layer"
        Repositories["Repositories"]:::repo
        Entities["Entities"]:::domain
        DTOs["DTOs"]:::domain
    end

    %% Persistence
    subgraph Persistence
        PostgreSQL["PostgreSQL Database\n(Flyway migrations)"]:::database
        MongoDB["MongoDB for Statistics"]:::database
    end

    %% External Microservices and Messaging
    ExternalNotifications["Notification Microservice"]:::external
    ExternalLogs["Logs Microservice"]:::external
    Kafka["Kafka"]:::external
    ZooKeeper["ZooKeeper"]:::external

    %% Connections
    Client -->|"HTTP_Request"| SecurityFilter
    SecurityFilter -->|"ValidJWT"| UniversityApplication
    UniversityApplication -->|"Initialize"| SecurityComponents
    UniversityApplication -->|"Delegates"| Controllers
    Controllers -->|"Calls"| ServiceInterfaces
    Controllers -->|"Calls"| ServiceImplementations
    ServiceInterfaces -->|"Executes|BusinessLogic"| ServiceImplementations
    ServiceImplementations -->|"Uses"| Repositories
    Repositories -->|"CRUD"| PostgreSQL
    ServiceImplementations -->|"Updates/Queries"| MongoDB
    ServiceImplementations -->|"REST_Call"| ExternalNotifications
    ServiceImplementations -->|"REST_Call"| ExternalLogs
    ServiceImplementations -->|"Publishes"| Kafka
    Kafka -->|"ManagedBy"| ZooKeeper
    Scheduled -->|"Triggers"| ServiceImplementations
    UniversityApplication -->|"InitSchema"| PostgreSQL

    %% Click Events
    click UniversityApplication "https://github.com/natanius18/university-api/blob/main/src/main/java/software/sigma/internship/UniversityApplication.java"
    click Controllers "https://github.com/natanius18/university-api/tree/main/src/main/java/software/sigma/internship/controller"
    click ServiceInterfaces "https://github.com/natanius18/university-api/tree/main/src/main/java/software/sigma/internship/service"
    click ServiceImplementations "https://github.com/natanius18/university-api/tree/main/src/main/java/software/sigma/internship/service/impl"
    click Repositories "https://github.com/natanius18/university-api/tree/main/src/main/java/software/sigma/internship/repo"
    click Entities "https://github.com/natanius18/university-api/tree/main/src/main/java/software/sigma/internship/entity"
    click DTOs "https://github.com/natanius18/university-api/tree/main/src/main/java/software/sigma/internship/dto"
    click SecurityComponents "https://github.com/natanius18/university-api/blob/main/src/main/java/software/sigma/internship/config/SecurityConfig.java"
    click SecurityComponents "https://github.com/natanius18/university-api/tree/main/src/main/java/software/sigma/internship/security"
    click PostgreSQL "https://github.com/natanius18/university-api/tree/main/src/main/resources/db/migration"
    click MongoDB "https://github.com/natanius18/university-api/tree/main/src/main/java/software/sigma/internship/mongo"
    click Scheduled "https://github.com/natanius18/university-api/blob/main/src/main/java/software/sigma/internship/scheduler/ScheduledTasks.java"

    %% Styles
    classDef entry fill:#ffcccc,stroke:#cc0000,stroke-width:2px;
    classDef api fill:#ccffcc,stroke:#009900,stroke-width:2px;
    classDef service fill:#ccccff,stroke:#0000cc,stroke-width:2px;
    classDef repo fill:#ffffcc,stroke:#999900,stroke-width:2px;
    classDef domain fill:#ffebcc,stroke:#cc6600,stroke-width:2px;
    classDef security fill:#e6ccff,stroke:#6600cc,stroke-width:2px;
    classDef database fill:#cce5ff,stroke:#003366,stroke-width:2px;
    classDef external fill:#e6ffe6,stroke:#009900,stroke-width:2px;
    classDef schedule fill:#f0f5c9,stroke:#808000,stroke-width:2px;
```
