# ngnhatthanh/linkway-socialnetwork architecture

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

- Interactive diagram: https://gitdiagram.com/ngnhatthanh/linkway-socialnetwork
- Repository: https://github.com/ngnhatthanh/linkway-socialnetwork

## Overview

##### Project Type & Purpose
- This is a full-stack web application built with Spring Boot, intended to serve as a social networking platform for developers.
- Its core functions include user authentication, post creation (with Markdown to HTML rendering), social interactions (comments, votes, tag management), real‐time chat and notifications (using WebSockets), and email integration (for features like forgot password).

##### Identifying the Main Components
a. Frontend Components:
– Views: Thymeleaf templates and static assets (CSS, JavaScript, and images) in the “src/main/resources/templates” and “src/main/resources/static” directories.
– Client-side interactions: JavaScript files (e.g., chatpage.js, navbar.js) that handle dynamic behavior, like real-time chat updates and UI events.

b. Backend Components:
– Controllers: Under “.../controller”, these manage incoming HTTP requests for authentication, posts, chats, notifications, and user operations.
– Service Layer: Contained within the “.../service” directory, where business logic is implemented. Services interact with controllers and the DAO layer.
– Data Access: The “.../dao” folder contains repositories (using Spring Data JPA conventions) to interact with the MySQL database.
– Entities & DTOs: “.../entity” defines the domain models (e.g. User, Post, Comment, Notification), while “.../dto” packages manage data transfer between layers.
– Configuration & Security: The “.../config” and subdirectories (e.g., for WebSocket, security, interceptors) configure Spring Security, session handling, cloud integrations (Cloudinary), and CORS/interception.
– Exception Handling: Organized within the “.../exception” package to manage errors uniformly.
– Utility Functions: In “.../util” for helper functions like Markdown conversion and password encoding (BCrypt).

c. External Dependencies:
– MySQL Database: As noted by the “create_tables_mysql.sql” script and the ERD in the README for persistent storage.
– Cloudinary: Configured via “CloudinaryConfig.java” to support image uploads.
– Email Service: Configured in Spring mail, as per the instructions in the README.
– WebSocket: Real-time communication for chat and notifications.

##### Mapping Relationships & Data Flow
- The Browser (with UI rendered using Thymeleaf) sends requests to the Controller layer.
- Controllers use Service components to apply business logic.
- Services then interact with Data Access Objects (DAO/repositories) which perform CRUD operations on the MySQL database.
- For real-time features, a dedicated WebSocket layer (configured via “WebSocketConfig.java”) manages persistent connections for chat and notifications.
- External service integrations (Cloudinary for images, email service for password resets) are invoked from their respective service components.

- How security is enforced:
- Spring Security configuration which intercepts requests, applies authentication/authorization (with components such as “CustomUserDetailService” and “CustomSuccessHandler”), and uses a set of interceptors as shown in the “interceptor” package.

##### Architectural Patterns & Design Principles
- MVC (Model-View-Controller) pattern:
- Model: Domain entities and DTOs.
- View: Thymeleaf templates and static resources.
- Controller: HTTP handling and routing.
- Layered Architecture:
- Separation of concerns between presentation (frontend), business logic (services), and data access (repositories/DAO).
- Dependency Injection via Spring Boot, enabling better testability and loose coupling.
- Real-time messaging using WebSockets, adding an event-driven aspect to the architecture.

- Follow with the Service layer, then the DAO/Data layer, culminating in the MySQL database.
- Off to the side (or connected appropriately), include external services like Cloudinary for image storage and the email server for notifications.

## Diagram (Mermaid)

```mermaid
graph TD
    %% Presentation Layer
    subgraph "Presentation Layer"
        A["User Browser"]:::presentation
        B["Thymeleaf Templates"]:::presentation
        C["Static Assets"]:::presentation
    end

    %% Backend Layer
    subgraph "Backend Layer"
        D["Controllers (HTTP)"]:::backend
        E["Services (Business Logic)"]:::backend
        F["Entities"]:::backend
        F1["DTOs"]:::backend
        G["DAO/Repositories"]:::backend
        H["MySQL Database"]:::database
    end

    %% Real-time & External
    subgraph "Real-time & External"
        I["WebSocket Server"]:::external
        J["Cloudinary (Image Uploads)"]:::external
        K["Email Service"]:::external
    end

    %% Infrastructure
    subgraph "Infrastructure"
        L["Configuration & Security"]:::infrastructure
        M["Exception Handling"]:::infrastructure
        N["Utility Functions"]:::infrastructure
    end

    %% Main Flow
    A -->|"sends_request"| D
    D -->|"invokes"| E
    E -->|"uses"| F
    E -->|"transfers"| F1
    F -->|"persists"| G
    F1 -->|"maps_to"| G
    G -->|"CRUD_ops"| H

    %% External Integrations from Services
    E -->|"real_time_comm"| I
    E -->|"image_upload"| J
    E -->|"email_notif"| K

    %% Infrastructure connections (dashed lines)
    D --- L
    E --- L
    G --- L
    D -->|"error_handling"| M
    E -->|"utilizes"| N

    %% Styles
    classDef presentation fill:#a8d0e6,stroke:#333,stroke-width:2px;
    classDef backend fill:#f76c6c,stroke:#333,stroke-width:2px;
    classDef database fill:#dcedc1,stroke:#333,stroke-width:2px;
    classDef external fill:#ffb7b2,stroke:#333,stroke-width:2px;
    classDef infrastructure fill:#c0c0c0,stroke:#333,stroke-width:2px;

    %% Click Events
    click B "https://github.com/ngnhatthanh/linkway-socialnetwork/tree/main/social_network/src/main/resources/templates"
    click C "https://github.com/ngnhatthanh/linkway-socialnetwork/tree/main/social_network/src/main/resources/static"
    click D "https://github.com/ngnhatthanh/linkway-socialnetwork/tree/main/social_network/src/main/java/com/social_network/controller"
    click E "https://github.com/ngnhatthanh/linkway-socialnetwork/tree/main/social_network/src/main/java/com/social_network/service"
    click F "https://github.com/ngnhatthanh/linkway-socialnetwork/tree/main/social_network/src/main/java/com/social_network/entity"
    click F1 "https://github.com/ngnhatthanh/linkway-socialnetwork/tree/main/social_network/src/main/java/com/social_network/dto"
    click G "https://github.com/ngnhatthanh/linkway-socialnetwork/tree/main/social_network/src/main/java/com/social_network/dao"
    click L "https://github.com/ngnhatthanh/linkway-socialnetwork/tree/main/social_network/src/main/java/com/social_network/config"
    click I "https://github.com/ngnhatthanh/linkway-socialnetwork/blob/main/social_network/src/main/java/com/social_network/config/WebSocketConfig.java"
    click J "https://github.com/ngnhatthanh/linkway-socialnetwork/blob/main/social_network/src/main/java/com/social_network/config/CloudinaryConfig.java"
    click M "https://github.com/ngnhatthanh/linkway-socialnetwork/tree/main/social_network/src/main/java/com/social_network/exception"
    click N "https://github.com/ngnhatthanh/linkway-socialnetwork/tree/main/social_network/src/main/java/com/social_network/util"
```
