# malav2364/calorify architecture

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

- Interactive diagram: https://gitdiagram.com/malav2364/calorify
- Repository: https://github.com/malav2364/calorify

## Overview

##### Main Components
- Frontend Client: Represented by the pages and components in the src/app and src/components directories. This includes views for login, signup, dashboard, and overall calorie tracking.
- API Layer (Backend): Located within src/app/api, it provides endpoints for authentication (with NextAuth), dishes management, and user calorie/BMI calculations.
- Database: The Prisma folder (with migrations and schema.prisma) indicates a relational database that stores user data, meal details, and historical calorie information.
- Middleware & Shared Libraries: The presence of middleware.js files and libraries (src/lib/auth.js, prisma.js, redis.js, utils.js) points to security functions, caching, and utility routines.
- External Services: Note the integration with NextAuth (for handling authentication flows) and Redis (for caching) which should be highlighted as external (or separately managed) services.

##### Relationships and Data Flows
- The Frontend interacts with API endpoints through HTTP requests (AJAX or fetch calls) to handle user actions such as login, registration, or calorie tracking.
- API endpoints interact with the database (using Prisma) to perform CRUD operations on user profiles, dishes, and tracking data.
- Authentication flows (NextAuth routes in the API) and session management are integrated with both the frontend and the backend.
- Middleware (appearing as both src/app/middleware.js and src/middleware.js) intercepts requests for security or additional logic – show where these intercept the data flow.
- Utilities like caching (via the Redis library) should be represented as connectors that help improve performance between the API and client or DB.

##### Architectural Patterns and Technologies
- Next.js Framework: Emphasize that it is a React-based framework supporting both server-side rendering and API routes.
- API Route Handling: The RESTful or Next.js serverless function style present in the src/app/api directory.
- Database Management with Prisma: Show how database migrations and the schema are managed through Prisma.
- Modular Component Design: The separation between UI components (src/components/ui) and business logic (src/lib) which follows a clear separation of concerns.

## Diagram (Mermaid)

```mermaid
flowchart TD
    UserBrowser["User Browser"]

    %% Frontend Client Subgraph
    subgraph "Frontend Client"
        LoginPage["Login Page"]:::frontend
        SignupPage["Signup Page"]:::frontend
        DashboardPage["Dashboard Page"]:::frontend
        HomePage["Home Page"]:::frontend
        Avatar["Avatar Component"]:::frontend
        Button["Button Component"]:::frontend
        Card["Card Component"]:::frontend
        AuthProv["Auth Provider"]:::frontend
        ThemeProv["Theme Provider"]:::frontend
    end

    %% API Layer Subgraph
    subgraph "API Layer"
        NextAuthEp["NextAuth Endpoint"]:::backend
        LoginEp["Login Endpoint"]:::backend
        SignupEp["Signup Endpoint"]:::backend
        DishesEp["Dishes Endpoint"]:::backend
        DishDetailEp["Dish Detail Endpoint"]:::backend
        UserCalEp["User Calories Endpoint"]:::backend
    end

    %% Database Subgraph
    subgraph "Database"
        PrismaDB["Prisma ORM"]:::database
    end

    %% Middleware & Utilities Subgraph
    subgraph "Middleware & Utilities"
        MiddlewareApp["Global Middleware (app)"]:::middleware
        MiddlewareRoot["Global Middleware (root)"]:::middleware
        AuthUtil["Auth Utility"]:::middleware
        PrismaUtil["Prisma Utility"]:::middleware
        RedisUtil["Redis Utility"]:::middleware
        Utils["General Utils"]:::middleware
    end

    %% External Services Subgraph
    subgraph "External Services"
        NextAuthService["NextAuth Service"]:::external
        RedisService["Redis Cache Service"]:::external
    end

    %% Data Flow Connections
    %% User Browser flows through Middleware to Frontend
    UserBrowser --> MiddlewareApp
    MiddlewareApp --> HomePage
    UserBrowser --> MiddlewareRoot
    MiddlewareRoot --> HomePage

    %% Frontend to API Layer Connections
    HomePage --> NextAuthEp
    HomePage --> DishesEp
    HomePage --> UserCalEp
    LoginPage --> LoginEp
    SignupPage --> SignupEp

    %% API Endpoints to Database (CRUD operations)
    LoginEp -->|"CRUD"| PrismaDB
    SignupEp -->|"CRUD"| PrismaDB
    DishesEp -->|"CRUD"| PrismaDB
    DishDetailEp -->|"CRUD"| PrismaDB
    UserCalEp -->|"CRUD"| PrismaDB

    %% API Endpoints to External Services
    NextAuthEp --> NextAuthService

    %% API Endpoints using Utilities
    NextAuthEp --> AuthUtil
    LoginEp --> Utils
    DishesEp --> PrismaUtil
    UserCalEp --> RedisUtil

    %% Class Definitions
    classDef frontend fill:#ADD8E6,stroke:#000,stroke-width:2px;
    classDef backend fill:#90EE90,stroke:#000,stroke-width:2px;
    classDef database fill:#FFFACD,stroke:#000,stroke-width:2px;
    classDef middleware fill:#FFD700,stroke:#000,stroke-width:2px;
    classDef external fill:#FFB6C1,stroke:#000,stroke-width:2px;

    %% Click Events for Frontend Client
    click LoginPage "https://github.com/malav2364/calorify/blob/master/src/app/login/page.jsx"
    click SignupPage "https://github.com/malav2364/calorify/blob/master/src/app/signup/page.jsx"
    click DashboardPage "https://github.com/malav2364/calorify/blob/master/src/app/dashboard/page.jsx"
    click HomePage "https://github.com/malav2364/calorify/blob/master/src/app/page.js"
    click Avatar "https://github.com/malav2364/calorify/blob/master/src/components/ui/avatar.jsx"
    click Button "https://github.com/malav2364/calorify/blob/master/src/components/ui/button.jsx"
    click Card "https://github.com/malav2364/calorify/blob/master/src/components/ui/card.jsx"
    click AuthProv "https://github.com/malav2364/calorify/blob/master/src/components/auth-provider.jsx"
    click ThemeProv "https://github.com/malav2364/calorify/blob/master/src/components/theme-provider.jsx"

    %% Click Events for API Layer
    click NextAuthEp "https://github.com/malav2364/calorify/blob/master/src/app/api/auth/[...nextauth]/route.js"
    click LoginEp "https://github.com/malav2364/calorify/blob/master/src/app/api/auth/login/route.js"
    click SignupEp "https://github.com/malav2364/calorify/blob/master/src/app/api/auth/signup/route.js"
    click DishesEp "https://github.com/malav2364/calorify/tree/master/src/app/api/dishes"
    click DishDetailEp "https://github.com/malav2364/calorify/blob/master/src/app/api/dishes/[id]/route.js"
    click UserCalEp "https://github.com/malav2364/calorify/blob/master/src/app/api/user/calories/route.js"

    %% Click Event for Database
    click PrismaDB "https://github.com/malav2364/calorify/blob/master/prisma/schema.prisma"

    %% Click Events for Middleware & Utilities
    click MiddlewareApp "https://github.com/malav2364/calorify/blob/master/src/app/middleware.js"
    click MiddlewareRoot "https://github.com/malav2364/calorify/blob/master/src/middleware.js"
    click AuthUtil "https://github.com/malav2364/calorify/blob/master/src/lib/auth.js"
    click PrismaUtil "https://github.com/malav2364/calorify/blob/master/src/lib/prisma.js"
    click RedisUtil "https://github.com/malav2364/calorify/blob/master/src/lib/redis.js"
    click Utils "https://github.com/malav2364/calorify/blob/master/src/lib/utils.js"
```
