# mscopa/cse340 architecture

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

- Interactive diagram: https://gitdiagram.com/mscopa/cse340
- Repository: https://github.com/mscopa/cse340

## Overview

##### Main Components
- Server/Entry Point: The application starts from server.js, which sets up the Express server. It defines the port and uses values from a .env file (mentioned in the README).
- Controllers: These include baseController.js and invController.js located in the controllers directory. They process incoming requests, control business logic, and coordinate data flow between models and views.
- Routes: In the routes folder, files like inventoryRoute.js and static.js map HTTP request URLs to specific controller methods.
- Models: The models directory (with inventory-model.js) is in charge of interacting with the database—encapsulating the business data and handling validation or transformation as needed.
- Database: The database folder contains SQL scripts, a database index (likely configuring connection details), and sample data (inventory-data.txt). This represents a PostgreSQL database backend that the application uses.
- Views: Using EJS templates, the views folder contains the presentation layer, with layouts, partials (header, footer, etc.), and view files for errors and inventory pages.
- Public Assets: The public directory holds static files like CSS, JavaScript, and images which are directly served to clients.
- Utilities: A set of helper functions or miscellaneous modules meant to assist various components of the application.
- Package Management & Configuration: The package.json and pnpm-lock.yaml files manage dependencies, with instructions in the README for using PNPM as the package manager.

##### Relationships and Data Flow
- The client (browser) sends HTTP requests to the server.
- The Express server (in server.js) listens for these client requests.
- The request URL is handled by a route (defined in routes folder), which determines the controller to invoke.
- The controller performs application logic, potentially interacting with the model for data retrieval or updates against the PostgreSQL database.
- The controller then loads an appropriate EJS view (from the views folder) to render the dynamic response, often embedding data from the models.
- Static content (CSS, JS, images) is delivered from the public directory independently.
- Utilities may be called by different parts of the system to manage tasks such as formatting data or handling error logging.

##### Architectural Patterns and Important Design Principles
- Separation of Concerns: The application’s structure separates the frontend presentation layer (EJS views & public assets) from the backend logic (controllers, routes, and models) and data persistence (database).
- Dependency Management: Use of Node, Express, and PNPM defines how the third-party packages are managed and integrated.

## Diagram (Mermaid)

```mermaid
flowchart TD
    Client["Client (Browser)"]:::client
    ExpressServer["Express Server (server.js)"]:::backend
    PackageConfig["Package Config (package.json & pnpm-lock.yaml)"]:::config

    subgraph "Routes"
        InventoryRoute["Inventory Route (inventoryRoute.js)"]:::backend
        StaticRoute["Static Route (static.js)"]:::backend
    end

    subgraph "Controllers"
        BaseController["Base Controller (baseController.js)"]:::backend
        InvController["Inventory Controller (invController.js)"]:::backend
    end

    InventoryModel["Inventory Model (inventory-model.js)"]:::data

    subgraph "Database"
        DBIndex["DB Index (index.js)"]:::data
        DBAssignment["DB Script (assignment2.sql)"]:::data
        DBSqlCode["DB SQL Code (db-sql-code.sql)"]:::data
        DBInventory["Inventory Data (inventory-data.txt)"]:::data
    end

    subgraph "Presentation"
        Views["Views (EJS Templates)"]:::presentation
        PublicAssets["Public Assets"]:::presentation
    end

    Utilities["Utilities (index.js)"]:::backend

    %% Data Flow
    Client -->|"HTTP_Request"| ExpressServer
    ExpressServer -->|"Load_Config"| PackageConfig
    ExpressServer -->|"Route_Request"| InventoryRoute
    ExpressServer -->|"Route_Request"| StaticRoute
    ExpressServer -->|"Serve_Static"| PublicAssets

    InventoryRoute -->|"Invoke_Controller"| BaseController
    InventoryRoute -->|"Invoke_Controller"| InvController
    StaticRoute -->|"Serve_Asset"| PublicAssets

    BaseController -->|"Call_Model"| InventoryModel
    InvController -->|"Call_Model"| InventoryModel

    BaseController -->|"Use_Utility"| Utilities
    InvController -->|"Use_Utility"| Utilities

    InventoryModel -->|"Query_DB"| DBIndex
    DBIndex -->|"Contains"| DBAssignment
    DBIndex -->|"Contains"| DBSqlCode
    DBIndex -->|"Contains"| DBInventory

    BaseController -->|"Render_View"| Views
    InvController -->|"Render_View"| Views

    %% Click Events
    click ExpressServer "https://github.com/mscopa/cse340/blob/main/server.js"
    click BaseController "https://github.com/mscopa/cse340/blob/main/controllers/baseController.js"
    click InvController "https://github.com/mscopa/cse340/blob/main/controllers/invController.js"
    click InventoryRoute "https://github.com/mscopa/cse340/blob/main/routes/inventoryRoute.js"
    click StaticRoute "https://github.com/mscopa/cse340/blob/main/routes/static.js"
    click InventoryModel "https://github.com/mscopa/cse340/blob/main/models/inventory-model.js"
    click DBIndex "https://github.com/mscopa/cse340/blob/main/database/index.js"
    click DBAssignment "https://github.com/mscopa/cse340/blob/main/database/assignment2.sql"
    click DBSqlCode "https://github.com/mscopa/cse340/blob/main/database/db-sql-code.sql"
    click DBInventory "https://github.com/mscopa/cse340/blob/main/database/inventory-data.txt"
    click Views "https://github.com/mscopa/cse340/tree/main/views/"
    click PublicAssets "https://github.com/mscopa/cse340/tree/main/public/"
    click Utilities "https://github.com/mscopa/cse340/blob/main/utilities/index.js"
    click PackageConfig "https://github.com/mscopa/cse340/blob/main/package.json"
    click PackageConfig "https://github.com/mscopa/cse340/blob/main/pnpm-lock.yaml"

    %% Styles
    classDef client fill:#fceabb,stroke:#f2a900,stroke-width:2px;
    classDef backend fill:#add8e6,stroke:#0000FF,stroke-width:2px;
    classDef data fill:#90ee90,stroke:#008000,stroke-width:2px;
    classDef presentation fill:#ffcc99,stroke:#FF8C00,stroke-width:2px;
    classDef config fill:#d3d3d3,stroke:#808080,stroke-width:2px;
```
