# zit-software/clean-code architecture

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

- Interactive diagram: https://gitdiagram.com/zit-software/clean-code
- Repository: https://github.com/zit-software/clean-code

## Overview

##### Project type and purpose
- This is a full-stack web application (Todo App) built with Node.js and Express, using Pug for server-side rendering.
- Purpose: allow users to sign up/in, create, complete, and delete tasks.

##### Core architectural components
a. Client (Browser)
- Renders HTML/CSS/JS returned by server.
b. Web Server (Express Application)
- Entry point: `index.js`/`app.js` set up Express, middlewares, view engine (Pug), global error handler.
c. Routing Layer (`routes/*.js`)
- Defines RESTful endpoints for auth and task operations.
d. Controllers (`controllers/*.js`)
- Handle HTTP requests, invoke services, render views or redirect.
e. Services (`services/*.js`)
- Encapsulate business logic (e.g., `auth.service.js`, `task.service.js`).
f. Models (`models/*.js`)
- Data schema and DB operations (e.g., `User`, `Task`).
g. Data Layer (`configs/db.js` + actual DB)
- Connection config (e.g., to MySQL/PostgreSQL/SQLite).
h. Views (`views/*.pug`)
- Pug templates for sign-in/up, task list, error pages.
i. Middlewares (`middlewares/*.js`)
- `auth` (protect routes), `errorHandler` (centralized error responses).

##### Relationships & interactions
1. Client sends HTTP request →
2. Express routes match path →
3. Route invokes corresponding controller →
4. Controller calls service layer →
5. Service uses models to query/update DB →
6. Model executes SQL via DB connection →
7. DB returns results →
8. Service returns data to controller →
9. Controller renders Pug view or sends JSON/redirect →
10. Client receives HTML/response.

##### Architectural patterns & design principles
- MVC (Model-View-Controller) separation: clear roles for data, UI, and request handling.
- Service layer abstracts business logic from controllers.
- Single responsibility: each file handles one concern (routing vs. controller vs. service vs. model).
- Middleware chain for cross-cutting concerns (authentication, error handling).

##### Elements
- Clear labels: e.g., “AuthController”, “TaskService”, “DB (MySQL)”.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Client Layer
    Browser["Browser\n(Client)"]:::client

    %% Server Layer
    subgraph "Express Application" 
        direction TB
        app["Express App\n(app.js / index.js)"]:::http
        subgraph Middlewares
            direction TB
            AuthMiddleware["Auth Middleware"]:::http
            ErrorHandler["Error Handler"]:::http
        end
        subgraph Routes
            direction TB
            AuthRoute["Auth Route"]:::http
            TaskRoute["Task Route"]:::http
            IndexRoute["Index Route"]:::http
        end
        subgraph Controllers
            direction TB
            AuthController["AuthController"]:::http
            TaskController["TaskController"]:::http
        end
        app -->|use| AuthMiddleware
        app -->|use| ErrorHandler
        app -->|use| AuthRoute
        app -->|use| TaskRoute
        app -->|use| IndexRoute
        AuthRoute -->|routes to| AuthController
        TaskRoute -->|routes to| TaskController
        IndexRoute -->|routes to| AuthController
    end

    %% Service Layer
    subgraph "Services" 
        direction TB
        AuthService["AuthService"]:::business
        TaskService["TaskService"]:::business
    end

    %% Model Layer
    subgraph "Models"
        direction TB
        UserModel["User Model"]:::data
        TaskModel["Task Model"]:::data
    end

    %% Data Layer
    subgraph "Database Layer"
        direction TB
        DBConfig["DB Config\n(configs/db.js)"]:::data
        Database["Database\n(MySQL/PostgreSQL)"]:::data
    end

    %% Views
    subgraph "Pug Views"
        direction TB
        SignInView["sign-in.pug"]:::views
        SignUpView["sign-up.pug"]:::views
        TaskIndexView["index.pug"]:::views
        LayoutView["layout.pug"]:::views
        HeaderView["header.pug"]:::views
        FooterView["footer.pug"]:::views
        ErrorView["error.pug"]:::views
    end

    %% Flow Connections
    Browser -->|HTTP Request| app
    AuthController -->|calls| AuthService
    TaskController -->|calls| TaskService
    AuthService -->|uses| UserModel
    TaskService -->|uses| TaskModel
    UserModel -->|queries via| DBConfig
    TaskModel -->|queries via| DBConfig
    DBConfig -->|connects to| Database
    AuthController -->|renders| SignInView
    AuthController -->|renders| SignUpView
    TaskController -->|renders| TaskIndexView
    AuthController -->|renders| ErrorView

    %% Click Events
    click app "https://github.com/zit-software/clean-code/blob/main/app.js"
    click app "https://github.com/zit-software/clean-code/blob/main/index.js"
    click DBConfig "https://github.com/zit-software/clean-code/blob/main/configs/db.js"
    click AuthRoute "https://github.com/zit-software/clean-code/blob/main/routes/auth.js"
    click TaskRoute "https://github.com/zit-software/clean-code/blob/main/routes/task.js"
    click IndexRoute "https://github.com/zit-software/clean-code/blob/main/routes/index.js"
    click AuthController "https://github.com/zit-software/clean-code/blob/main/controllers/auth.controller.js"
    click TaskController "https://github.com/zit-software/clean-code/blob/main/controllers/task.controller.js"
    click AuthService "https://github.com/zit-software/clean-code/blob/main/services/auth.service.js"
    click TaskService "https://github.com/zit-software/clean-code/blob/main/services/task.service.js"
    click UserModel "https://github.com/zit-software/clean-code/blob/main/models/user.js"
    click TaskModel "https://github.com/zit-software/clean-code/blob/main/models/task.js"
    click SignInView "https://github.com/zit-software/clean-code/blob/main/views/auth/sign-in.pug"
    click SignUpView "https://github.com/zit-software/clean-code/blob/main/views/auth/sign-up.pug"
    click TaskIndexView "https://github.com/zit-software/clean-code/blob/main/views/task/index.pug"
    click LayoutView "https://github.com/zit-software/clean-code/blob/main/views/layout.pug"
    click HeaderView "https://github.com/zit-software/clean-code/blob/main/views/header.pug"
    click FooterView "https://github.com/zit-software/clean-code/blob/main/views/footer.pug"
    click ErrorView "https://github.com/zit-software/clean-code/blob/main/views/error.pug"

    %% Styles
    classDef client fill:#ADD8E6,stroke:#333,stroke-width:1px;
    classDef http fill:#90EE90,stroke:#333,stroke-width:1px;
    classDef business fill:#FFA500,stroke:#333,stroke-width:1px;
    classDef data fill:#DDA0DD,stroke:#333,stroke-width:1px;
    classDef views fill:#87CEFA,stroke:#333,stroke-width:1px;
```
