# nouman-wp/cropify architecture

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

- Interactive diagram: https://gitdiagram.com/nouman-wp/cropify
- Repository: https://github.com/nouman-wp/cropify

## Overview

##### Main Components
- Client & Presentation Layer:
- Represented by the EJS templates within the views folder and static assets (CSS, images, videos) in the public directory.
- The browser accesses different pages such as Home, Buy, Dashboard, Profile, etc.
- Server/Backend Layer:
- The entry point is app.js along with Express route files under the routes directory (e.g., index.js, buy.js, dashboard.js, listing.js).
- Middleware (middleware.js and utils such as ExpressError.js, wrapAsync.js) is used for error handling, session management, and route protection.
- Data and Business Logic Layer:
- Models (in the models folder for listing, order, and user) encapsulate your business logic, and Mongoose connects these models with MongoDB.
- The ini directory appears to contain initialization routines or data-setting scripts which might be seeding initial data.
- Database:
- MongoDB is the database backend, accessed via Mongoose to persist data like user profiles, crop listings, orders, etc.
- Authentication & Uploading:
- Passport.js is integrated for user registration, login, and session management.
- Multer (as noted in the tech stack) handles image uploads, storing files in the uploads directory.

##### Relationships and Interactions
- Client Requests:
- Client devices (browsers) interact with the server via HTTP requests. User-triggered actions (like logging in, viewing or purchasing a crop, etc.) make calls to various Express routes.
- Presentation and API Layer:
- Once a request is received by the Express server, the appropriate route module handles the request, calls business logic via models, and finally renders a view (using EJS) or sends a JSON response.
- Data Flow and Persistence:
- Database operations (CRUD with MongoDB through Mongoose) are initiated by the models.
- Uploaded images are stored on the local filesystem (uploads directory), and their details might be stored in the database.
- Authentication Workflow:
- Passport.js integrates as middleware in the routing layer to manage authentication state for secure user sessions.

##### Architectural Patterns and Design Principles
- MVC (Model-View-Controller) Pattern:
- Models: Data schemas for users, listings, orders.
- Views: EJS templates and static assets.
- Controllers: Express route files that act as controllers and use middleware.
- Separation of Concerns:
- Different directories contain logically separate components (models for DB operations, routes for endpoints, middleware, utilities, and views for presentation).
- Modular Design:
- The project is broken down into modules like ini (initialization), utils, and clearly separated configuration via package.json, allowing for easier maintenance and scaling.

##### Technologies, Frameworks, and Libraries to Emphasize
- Node.js and Express.js as the core server technology.
- MongoDB with Mongoose as the ODM for data persistence.
- Passport.js for user authentication.
- Multer for file uploads (image handling).
- EJS paired with Bootstrap for a responsive UI.
- Chart.js for visualizing market price data.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Client & Presentation Layer"
        Client["Client (Browser)"]:::clientStyle
        Views["EJS Templates"]:::viewStyle
        Static["Static Assets"]:::assetStyle
        Client -->|"Loads Views & Assets"| Views
        Client -->|"Loads Views & Assets"| Static
    end

    subgraph "Server/Backend Layer"
        App["Main Application"]:::appStyle
        Routes["Express Routes"]:::routeStyle
        subgraph "Middleware & Utilities"
            Middleware["middleware.js"]:::middlewareStyle
            ErrorUtil["ExpressError.js (utils)"]:::middlewareStyle
            AsyncUtil["wrapAsync.js (utils)"]:::middlewareStyle
        end
        App -->|"Handles Routing"| Routes
        App -->|"Uses"| Middleware
        App -->|"Uses"| ErrorUtil
        App -->|"Uses"| AsyncUtil
    end

    subgraph "Data and Business Logic Layer"
        Models["Data Models"]:::modelStyle
        Init["Initialization/Seeding"]:::initStyle
        Routes -->|"Executes Logic"| Models
        App -->|"Seeds Data"| Init
    end

    subgraph "Database & File Storage"
        DB["Database (MongoDB)"]:::dbStyle
        Uploads["File Uploads"]:::uploadStyle
        Models -->|"CRUD Operations"| DB
        Routes -->|"Saves Images"| Uploads
    end

    Auth["Authentication (Passport.js)"]:::authStyle
    App -->|"Integrates Passport.js"| Auth
    Middleware -->|"Handles File Uploads (Multer)"| Uploads

    click Views "https://github.com/nouman-wp/cropify/tree/main/views/"
    click Static "https://github.com/nouman-wp/cropify/tree/main/public/"
    click App "https://github.com/nouman-wp/cropify/blob/main/app.js"
    click Routes "https://github.com/nouman-wp/cropify/tree/main/routes/"
    click Middleware "https://github.com/nouman-wp/cropify/blob/main/middleware.js"
    click ErrorUtil "https://github.com/nouman-wp/cropify/blob/main/utils/ExpressError.js"
    click AsyncUtil "https://github.com/nouman-wp/cropify/blob/main/utils/wrapAsync.js"
    click Models "https://github.com/nouman-wp/cropify/tree/main/models/"
    click Init "https://github.com/nouman-wp/cropify/tree/main/ini/"
    click Uploads "https://github.com/nouman-wp/cropify/tree/main/uploads/"

    classDef clientStyle fill:#b3e5fc,stroke:#0277bd,stroke-width:2px;
    classDef viewStyle fill:#e1f5fe,stroke:#0288d1,stroke-width:2px;
    classDef assetStyle fill:#e1f5fe,stroke:#0288d1,stroke-width:2px;
    classDef appStyle fill:#c8e6c9,stroke:#2e7d32,stroke-width:2px;
    classDef routeStyle fill:#dcedc8,stroke:#558b2f,stroke-width:2px;
    classDef middlewareStyle fill:#ffe0b2,stroke:#ef6c00,stroke-width:2px;
    classDef modelStyle fill:#f8bbd0,stroke:#ad1457,stroke-width:2px;
    classDef initStyle fill:#fce4ec,stroke:#c2185b,stroke-width:2px;
    classDef dbStyle fill:#d1c4e9,stroke:#5e35b1,stroke-width:2px;
    classDef uploadStyle fill:#ffccbc,stroke:#d84315,stroke-width:2px;
    classDef authStyle fill:#fff9c4,stroke:#f9a825,stroke-width:2px;
```
