# rajdeep564/practicaltest architecture

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

- Interactive diagram: https://gitdiagram.com/rajdeep564/practicaltest
- Repository: https://github.com/rajdeep564/practicaltest

## Overview

##### Project Type and Purpose
- This is a full-stack dashboard application.
- Frontend: React + TypeScript SPA with Redux, React Router, Tailwind CSS, Axios.
- Backend: Node.js + Express + TypeScript API, MySQL persistence, JWT auth, pagination, role-based access.
- Purpose: allow Admins/Users to manage and view products and categories through a responsive UI.

##### File Structure
- Top-level:
- backend/ – Express server (controllers, routes, models, middleware, utils, config)
- frontend/ – React SPA (components, hooks, routes, store, utils, types)
- database/ – raw SQL schema
- Backend patterns: MVC-style controllers → services → models → DB; middleware for auth/validation; scripts for DB init.
- Frontend patterns: feature-based component folders (auth, categories, products, layout, common), Redux slices for state, API utilities, protected routes.
- Config/build: tsconfig.json, package.json, dotenv.

##### README Insights
- JWT authentication, automatic pagination thresholds, role-based UI, RESTful API docs.
- Clear separation of concerns mentioned (modular architecture, TypeScript safety).

##### Main Components
a. Frontend SPA
- React Components (Login, Dashboard, CategoryList, ProductList, AddProductModal, Pagination, ProtectedRoute, Layout)
- Redux Store (authSlice, categorySlice, productSlice)
- React Router createBrowserRouter
- Axios API layer
b. Backend API Server
- Express app (app.ts)
- Middleware (auth.ts, validation.ts)
- Controllers (authController, categoryController, productController)
- Models and database layer (TypeORM-style or mysql2 queries in models folder)
- JWT utils, API response utils
c. Database
- MySQL instance (users, categories, products tables)
- DB init script
d. External/Shared
- CORS
- dotenv for configuration
- LocalStorage (JWT token storage)

##### Component Relationships & Data Flows
- User ↔ Browser (UI)
- Login form calls Frontend Auth service → Axios POST /api/users/login → receives JWT
- Token stored in localStorage → Axios interceptor attaches Authorization header
- Frontend ↔ Backend
- GET/POST/PUT/DELETE on /api/categories and /api/products
- ProtectedRoute guards client routes based on token
- Controllers validate via express-validator → call model layer → query MySQL → return JSON
- Backend ↔ Database
- Connection pooling via mysql2
- Queries use prepared statements, LIMIT/OFFSET for pagination
- Optional: Show refresh token or error handling flows.

##### Architectural Patterns & Principles
- Separation of Concerns (UI vs. DI vs. DB)
- Layered Architecture (routes → controllers → models → DB)
- Feature-based modularization (frontend folders by feature)
- Stateless JWT authentication
- Automatic threshold-based pagination
- Role-based access control

##### Detail
- Significant elements: route layers, middleware, state slices, utils (api.ts, jwt.ts).
- Don’t over-abstract: show the real feature sets (Auth, Products, Categories).

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Frontend Layer
    subgraph "Frontend (React SPA)"
        direction TB
        App["App.tsx"]:::frontend
        Routes["routes/index.tsx"]:::frontend
        Login["Login.tsx"]:::frontend
        Dashboard["Dashboard.tsx"]:::frontend
        CategoryList["CategoryList.tsx"]:::frontend
        ProductList["ProductList.tsx"]:::frontend
        AddProductModal["AddProductModal.tsx"]:::frontend
        Pagination["Pagination.tsx"]:::frontend
        ProtectedRoute["ProtectedRoute.tsx"]:::frontend
        Header["Header.tsx"]:::frontend
        Sidebar["Sidebar.tsx"]:::frontend
        LayoutWrapper["Layout.tsx"]:::frontend
        authSlice["authSlice.ts"]:::store
        categorySlice["categorySlice.ts"]:::store
        productSlice["productSlice.ts"]:::store
        storeIndex["index.ts"]:::store
        APIService["api.ts"]:::util
        JWTUtil["jwt.ts"]:::util
        Formatters["formatters.ts"]:::util
        SweetAlert["sweetAlert.ts"]:::util
        useAuth["useAuth.ts"]:::util
    end

    %% Backend Layer
    subgraph "Backend (Express API)"
        direction TB
        AppServer["app.ts"]:::backend
        RoutesIndex["routes/index.ts"]:::backend
        AuthRoutes["routes/auth.ts"]:::backend
        CategoryRoutes["routes/categories.ts"]:::backend
        ProductRoutes["routes/products.ts"]:::backend
        AuthController["authController.ts"]:::backend
        CategoryController["categoryController.ts"]:::backend
        ProductController["productController.ts"]:::backend
        AuthMiddleware["auth.ts"]:::middleware
        ValidationMiddleware["validation.ts"]:::middleware
        UserModel["User.ts"]:::model
        CategoryModel["Category.ts"]:::model
        ProductModel["Product.ts"]:::model
        DBConfig["database.ts"]:::config
        InitScript["initDatabase.ts"]:::script
        LegacyInit["init-db.ts"]:::script
        JWTUtilBackend["jwt.ts"]:::util
        APIResponseUtil["apiResponse.ts"]:::util
    end

    %% Database Layer
    subgraph "Database (MySQL)"
        direction TB
        MySQL[(MySQL Database)]:::db
        Schema["schema.sql"]:::db
    end

    %% Connections Frontend
    App --> Routes
    Routes --> Login
    Routes --> Dashboard
    Dashboard --> CategoryList
    Dashboard --> ProductList
    ProductList --> AddProductModal
    CategoryList --> Pagination
    ProductList --> Pagination
    Routes --> ProtectedRoute
    ProtectedRoute --> LayoutWrapper
    LayoutWrapper --> Header
    LayoutWrapper --> Sidebar
    App --> authSlice
    App --> categorySlice
    App --> productSlice
    App --> storeIndex
    App --> APIService
    APIService --> JWTUtil
    APIService --> Formatters
    APIService --> SweetAlert
    App --> useAuth

    %% Frontend to Backend API
    APIService -->|POST /api/users/login| AuthRoutes
    APIService -->|GET /api/categories| CategoryRoutes
    APIService -->|GET /api/products| ProductRoutes
    APIService -->|protected| AuthMiddleware

    %% Connections Backend
    AppServer --> RoutesIndex
    RoutesIndex --> AuthRoutes
    RoutesIndex --> CategoryRoutes
    RoutesIndex --> ProductRoutes
    AuthRoutes --> AuthController
    CategoryRoutes --> CategoryController
    ProductRoutes --> ProductController
    AuthController --> AuthMiddleware
    CategoryController --> ValidationMiddleware
    ProductController --> ValidationMiddleware
    AuthController --> UserModel
    CategoryController --> CategoryModel
    ProductController --> ProductModel
    UserModel --> MySQL
    CategoryModel --> MySQL
    ProductModel --> MySQL
    AppServer --> DBConfig
    DBConfig --> MySQL
    InitScript --> MySQL
    LegacyInit --> MySQL
    AuthController --> JWTUtilBackend
    AuthController --> APIResponseUtil
    CategoryController --> APIResponseUtil
    ProductController --> APIResponseUtil

    %% Click Events Frontend
    click App "https://github.com/rajdeep564/practicaltest/blob/main/frontend/src/App.tsx"
    click Routes "https://github.com/rajdeep564/practicaltest/blob/main/frontend/src/routes/index.tsx"
    click Login "https://github.com/rajdeep564/practicaltest/blob/main/frontend/src/components/auth/Login.tsx"
    click Dashboard "https://github.com/rajdeep564/practicaltest/blob/main/frontend/src/components/dashboard/Dashboard.tsx"
    click CategoryList "https://github.com/rajdeep564/practicaltest/blob/main/frontend/src/components/categories/CategoryList.tsx"
    click ProductList "https://github.com/rajdeep564/practicaltest/blob/main/frontend/src/components/products/ProductList.tsx"
    click AddProductModal "https://github.com/rajdeep564/practicaltest/blob/main/frontend/src/components/products/AddProductModal.tsx"
    click Pagination "https://github.com/rajdeep564/practicaltest/blob/main/frontend/src/components/common/Pagination.tsx"
    click ProtectedRoute "https://github.com/rajdeep564/practicaltest/blob/main/frontend/src/components/common/ProtectedRoute.tsx"
    click Header "https://github.com/rajdeep564/practicaltest/blob/main/frontend/src/components/layout/Header.tsx"
    click Sidebar "https://github.com/rajdeep564/practicaltest/blob/main/frontend/src/components/layout/Sidebar.tsx"
    click LayoutWrapper "https://github.com/rajdeep564/practicaltest/blob/main/frontend/src/components/layout/Layout.tsx"
    click authSlice "https://github.com/rajdeep564/practicaltest/blob/main/frontend/src/store/authSlice.ts"
    click categorySlice "https://github.com/rajdeep564/practicaltest/blob/main/frontend/src/store/categorySlice.ts"
    click productSlice "https://github.com/rajdeep564/practicaltest/blob/main/frontend/src/store/productSlice.ts"
    click storeIndex "https://github.com/rajdeep564/practicaltest/blob/main/frontend/src/store/index.ts"
    click APIService "https://github.com/rajdeep564/practicaltest/blob/main/frontend/src/utils/api.ts"
    click JWTUtil "https://github.com/rajdeep564/practicaltest/blob/main/frontend/src/utils/jwt.ts"
    click Formatters "https://github.com/rajdeep564/practicaltest/blob/main/frontend/src/utils/formatters.ts"
    click SweetAlert "https://github.com/rajdeep564/practicaltest/blob/main/frontend/src/utils/sweetAlert.ts"
    click useAuth "https://github.com/rajdeep564/practicaltest/blob/main/frontend/src/hooks/useAuth.ts"

    %% Click Events Backend
    click AppServer "https://github.com/rajdeep564/practicaltest/blob/main/backend/src/app.ts"
    click RoutesIndex "https://github.com/rajdeep564/practicaltest/blob/main/backend/src/routes/index.ts"
    click AuthRoutes "https://github.com/rajdeep564/practicaltest/blob/main/backend/src/routes/auth.ts"
    click CategoryRoutes "https://github.com/rajdeep564/practicaltest/blob/main/backend/src/routes/categories.ts"
    click ProductRoutes "https://github.com/rajdeep564/practicaltest/blob/main/backend/src/routes/products.ts"
    click AuthController "https://github.com/rajdeep564/practicaltest/blob/main/backend/src/controllers/authController.ts"
    click CategoryController "https://github.com/rajdeep564/practicaltest/blob/main/backend/src/controllers/categoryController.ts"
    click ProductController "https://github.com/rajdeep564/practicaltest/blob/main/backend/src/controllers/productController.ts"
    click AuthMiddleware "https://github.com/rajdeep564/practicaltest/blob/main/backend/src/middleware/auth.ts"
    click ValidationMiddleware "https://github.com/rajdeep564/practicaltest/blob/main/backend/src/middleware/validation.ts"
    click UserModel "https://github.com/rajdeep564/practicaltest/blob/main/backend/src/models/User.ts"
    click CategoryModel "https://github.com/rajdeep564/practicaltest/blob/main/backend/src/models/Category.ts"
    click ProductModel "https://github.com/rajdeep564/practicaltest/blob/main/backend/src/models/Product.ts"
    click DBConfig "https://github.com/rajdeep564/practicaltest/blob/main/backend/src/config/database.ts"
    click InitScript "https://github.com/rajdeep564/practicaltest/blob/main/backend/src/scripts/initDatabase.ts"
    click LegacyInit "https://github.com/rajdeep564/practicaltest/blob/main/backend/init-db.ts"
    click JWTUtilBackend "https://github.com/rajdeep564/practicaltest/blob/main/backend/src/utils/jwt.ts"
    click APIResponseUtil "https://github.com/rajdeep564/practicaltest/blob/main/backend/src/utils/apiResponse.ts"
    click Schema "https://github.com/rajdeep564/practicaltest/blob/main/database/schema.sql"

    %% Styles
    classDef frontend fill:#D0E8FF,stroke:#0366d6,stroke-width:1px
    classDef store fill:#BEE3F8,stroke:#0366d6,stroke-width:1px
    classDef util fill:#F0F0F0,stroke:#666,stroke-width:1px
    classDef backend fill:#D6F5D6,stroke:#28a745,stroke-width:1px
    classDef middleware fill:#E2F8E2,stroke:#28a745,stroke-width:1px
    classDef config fill:#CDF0EA,stroke:#17a2b8,stroke-width:1px
    classDef script fill:#FFF1B8,stroke:#d39e00,stroke-width:1px
    classDef model fill:#E8F5E9,stroke:#28a745,stroke-width:1px
    classDef db fill:#FFF8B0,stroke:#d39e00,stroke-width:1px
```
