# 100xdevs-cohort-3/week-8-course-selling-project architecture

> Architecture diagram of the GitHub repository 100xdevs-cohort-3/week-8-course-selling-project, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/100xdevs-cohort-3/week-8-course-selling-project
- Repository: https://github.com/100xdevs-cohort-3/week-8-course-selling-project

## Overview

##### Project Type & Purpose
- The project is a full-stack application (primarily backend-focused in the current file tree) developed in Node.js.
- Its purpose is to support a course selling platform where both users and admins have dedicated functionalities (e.g., user login, signup, course purchase; admin login, course management).
- It uses Express for the HTTP server, Mongoose for MongoDB interactions, and jsonwebtoken (JWT) for authentication, with the README noting additional suggestions like switching to cookies for auth.

##### Analyzing the File Structure
- Top-Level Files:
- index.js: The main entry point, likely where the Express server is configured and started.
- config.js: Contains configurations, which could include environment-specific settings.
- db.js: Manages the MongoDB connection, crucial for data persistence.
- .env.example: Holds environment variables, particularly for the DB connection string.
- package.json/package-lock.json: Define project dependencies.
- Directories:
- middleware/: Contains authentication middleware files: admin.js and user.js, responsible for managing access control for the respective roles.
- routes/: Divided into admin.js, course.js, and user.js. This clearly shows a separation of concerns where different routes are managed per user role or functionality (i.e., course management).

##### Key Architecture Components
a. Express Server Layer:
- Entry Point: index.js where the Express application is initialized.
- Routing: Separated in the routes directory with distinct files for admin, course, and user functionalities.
b. Middleware Layer:
- admin.js and user.js under middleware handle role-specific authentication and perhaps authorization.
c. Database Layer:
- db.js connects to MongoDB.
- The system includes schemas (as described in the README) for the models: User, Admin, Course, and Purchase.
d. Configuration & Environment:
- config.js for general configuration.
- .env.example for environment variable management.
e. External Dependencies:
- Libraries like Express (for HTTP server), jsonwebtoken (for authentication), and mongoose (for database interactions).
f. (Optional) Frontend:
- Although the file tree does not yet contain a dedicated frontend directory, the README indicates that a frontend will be added later (using EJS or React).

- Client / Frontend:
- API Gateway / Express Server:
- A central block starting from index.js that routes incoming requests.
- Middleware:
- Attach middleware blocks (user.js and admin.js) that intercept the request flow for authentication.
- Database (MongoDB):
- Configuration/Environment Variables:
- Configuration settings from config.js and .env file, which influence how different components interact (for example, DB connection strings and JWT secret keys).

##### Architectural Patterns and Design Principles
- Separation of Concerns/MVC-like Structure:
- The project separates routing, middleware, configuration, and database connections clearly.
- Layered Architecture:
- Different layers (routing/controllers, middleware, database) are clearly distinct.
- Dependency Management:
- External libraries are cleanly integrated via npm packages.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Layers
    Client["Client / Frontend"]:::client
    Server["Express Server: index.js"]:::api
    Config["Configuration: config.js"]:::config
    Env["Environment: .env.example"]:::config
    AdminMW["Admin Auth Middleware: middleware/admin.js"]:::middleware
    UserMW["User Auth Middleware: middleware/user.js"]:::middleware
    AdminRoute["Admin Routes: routes/admin.js"]:::route
    UserRoute["User Routes: routes/user.js"]:::route
    CourseRoute["Course Routes: routes/course.js"]:::route
    Database["Database: MongoDB (db.js)"]:::database
    ExpressLib["Express"]:::external
    MongooseLib["Mongoose"]:::external
    JWTLib["jsonwebtoken"]:::external

    %% Connections - Main Flow
    Client -->|"HTTP Request"| Server
    Server -->|"Loads config"| Config
    Server -->|"Loads env"| Env
    
    %% Admin functionality flow
    Server -->|"Admin Request"| AdminMW
    AdminMW -->|"Authenticated"| AdminRoute

    %% User functionality flow
    Server -->|"User Request"| UserMW
    UserMW -->|"Authenticated"| UserRoute

    %% Course route (for both customers and admins)
    Server -->|"Course Request"| CourseRoute

    %% Routes interacting with database
    AdminRoute -->|"DB Operation"| Database
    UserRoute -->|"DB Operation"| Database
    CourseRoute -->|"DB Operation"| Database

    %% External Dependencies
    ExpressLib -->|"Used by"| Server
    MongooseLib -->|"Used by"| Database
    JWTLib -->|"Utilized by"| AdminMW
    JWTLib -->|"Utilized by"| UserMW

    %% Styles and vertical adjustments
    classDef client fill:#f9d5e5,stroke:#333,stroke-width:2px;
    classDef api fill:#bfd7ea,stroke:#333,stroke-width:2px;
    classDef route fill:#c6efce,stroke:#333,stroke-width:2px;
    classDef middleware fill:#fff2cc,stroke:#333,stroke-width:2px;
    classDef database fill:#d9e4dd,stroke:#333,stroke-width:2px;
    classDef config fill:#e4d1f9,stroke:#333,stroke-width:2px;
    classDef external fill:#ffd966,stroke:#333,stroke-width:2px;

    %% Click Events
    click Server "https://github.com/100xdevs-cohort-3/week-8-course-selling-project/blob/main/index.js"
    click Config "https://github.com/100xdevs-cohort-3/week-8-course-selling-project/blob/main/config.js"
    click Env "https://github.com/100xdevs-cohort-3/week-8-course-selling-project/blob/main/.env.example"
    click Database "https://github.com/100xdevs-cohort-3/week-8-course-selling-project/blob/main/db.js"
    click AdminRoute "https://github.com/100xdevs-cohort-3/week-8-course-selling-project/blob/main/routes/admin.js"
    click CourseRoute "https://github.com/100xdevs-cohort-3/week-8-course-selling-project/blob/main/routes/course.js"
    click UserRoute "https://github.com/100xdevs-cohort-3/week-8-course-selling-project/blob/main/routes/user.js"
    click AdminMW "https://github.com/100xdevs-cohort-3/week-8-course-selling-project/blob/main/middleware/admin.js"
    click UserMW "https://github.com/100xdevs-cohort-3/week-8-course-selling-project/blob/main/middleware/user.js"
```
