How tuhin114/prompt-sharing-application works
Main Components
• Frontend UI Layer: Represented by the Next.js “app” folder with pages such as “create-prompt”, “dashboard”, “profile”, “leaderboard”, “prompt”, etc. Also include reusable components and UI elements from the “components” and “src/components/ui” directories.
• API Routes/Backend Logic: This is defined in the “app/api” folder. It contains endpoints for authentication (e.g., /auth/[...nextauth]), prompts (/api/prompt and its variants for new, draft, sort, etc.), and various functionalities (bookmarks, categories, notifications, scheduling, etc.).
• Database Layer: A MongoDB backend that is integrated via Mongoose. This includes connection management (handled in utils/database.js and further configured via a “connectToDB” function) and models (found in the “models” directory for User, Prompt, Category, Notification, etc.).
• Authentication Module: Managed via NextAuth and GoogleProvider (with detailed custom callbacks for session and sign-in), ensuring that each session includes the user’s MongoDB ID and that new users are automatically created if they don’t exist.
• External Integrations and CI/CD: Indicate integration with external services (Google for authentication and MongoDB as the primary data store) and also note automated workflows (GitHub Workflows found in .github/workflows) such as the cron job setup.
Relationships and Data Flow
• The Frontend communicates with the Backend API routes using HTTP requests (GET, POST, etc.). For instance, forms (e.g., in CreatePrompt and EditPrompt components) submit data to endpoints like /api/prompt/new.
• The API layer receives data and interacts with the Database. For example, the POST /api/prompt route extracts input data, connects to MongoDB via the “connectToDB” function, creates a new prompt document using a corresponding Mongoose model, and returns a response.
• The Authentication module intercepts sign-in and session checks. It manages user creation, session enrichment (adding MongoDB _id), and redirects based on authentication status.
• Custom Hooks (in the “hooks” directory) are used across the UI to fetch data, handle search/filter operations, manage states like “submitting” status, and interact with API endpoints.
Architectural Patterns and Design Principles
• Separation of Concerns: The system is divided into distinct layers – UI components/pages (views), API routes (controllers/business logic), and database models (data layer).
• RESTful API Design: The various API endpoints follow HTTP methods (GET, POST) to perform specific operations.
• Component Reusability: There is a clear separation between UI components (e.g., PromptCard, Feed, Form) for a consistent user interface.
• Micro-interactions: Custom hooks and modular components allow for a smooth user experience and state management.
• Authentication and Authorization: NextAuth provides robust user authentication while integrating database validations using Mongoose schemas.
• CI/CD & Cron Jobs: GitHub workflows indicate automated deployment/build and scheduled tasks.
Specific Elements
– Component labels such as “Frontend UI”, “API Routes”, “Database (MongoDB)”, “Authentication Module (NextAuth + Google)”, “Models (User, Prompt, etc.)”, and “Custom Hooks/Utilities”.
• User interaction on UI → API Request (e.g. creating a prompt, signing in).
• API Route → Database Connection (e.g., via connectToDB and Mongoose models).
• Authentication flow → Session callback during sign-in.
• Data flow from API → Frontend (responses feeding into components like Feed, PromptCard).
– Include annotations on key architectural patterns (e.g., “Separation of Lookup and CRUD operations in API”, “Reusable UI Components”, “Authentication Middleware via NextAuth”).