Skip to content

Commit 083bcb4

Browse files
committed
backend setup, added global.css and initialized recipes.js
1 parent 6140810 commit 083bcb4

File tree

5 files changed

+1496
-79
lines changed

5 files changed

+1496
-79
lines changed

app/api/recipes.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import clientPromise from "../../lib/mongodb";
2+
3+
/**
4+
* API route handler for fetching all recipes from the 'recipes' collection in MongoDB.
5+
*
6+
* @async
7+
* @function
8+
* @param {Object} _req - The request object (not used in this case).
9+
* @param {Object} res - The response object used to send back the result.
10+
* @returns {Promise<void>} Sends a JSON response containing the fetched recipes or an error message.
11+
*/
12+
export default async (_req, res) => {
13+
try {
14+
// Await the MongoDB client connection
15+
const client = await clientPromise;
16+
const db = client.db('devdb'); // Connect to the 'devdb' database
17+
18+
// Fetch all documents from the 'recipes' collection and convert them to an array
19+
const recipes = await db.collection('recipes').find({}).toArray();
20+
21+
// Send a 200 (OK) response with the fetched recipes in JSON format
22+
res.status(200).json(recipes);
23+
} catch (e) {
24+
// Log the error to the console for debugging
25+
console.error(e);
26+
27+
// Send a 500 (Internal Server Error) response with an error message
28+
res.status(500).json({ error: 'Failed to fetch data' });
29+
}
30+
};

app/globals.css

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
@tailwind base;
2+
@tailwind components;
3+
@tailwind utilities;
4+
:root {
5+
--background: #ffffff;
6+
--foreground: #171717;
7+
}
8+
@media (prefers-color-scheme: dark) {
9+
:root {
10+
--background: #0a0a0a;
11+
--foreground: #ededed;
12+
}
13+
}
14+
body {
15+
color: var(--foreground);
16+
background: var(--background);
17+
font-family: Arial, Helvetica, sans-serif;
18+
}
19+
@layer utilities {
20+
.text-balance {
21+
text-wrap: balance;
22+
}
23+
}

lib/mongodb.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { MongoClient } from 'mongodb';
2+
3+
const uri = "mongodb+srv://group-b:[email protected]/devdb?retryWrites=true&w=majority";
4+
let client;
5+
let clientPromise;
6+
7+
/**
8+
* Throws an error if the MongoDB URI is not present in environment variables.
9+
* @throws Will throw an error if MONGODB_URI is not defined in the environment variables.
10+
*/
11+
if (!process.env.MONGODB_URI) {
12+
throw new Error("Please add your MongoDB URI to the environment variables");
13+
}
14+
15+
const options = {};
16+
17+
/**
18+
* In development mode, this ensures that the MongoClient instance is reused across hot reloads.
19+
* This prevents creating new instances of MongoClient each time a change occurs.
20+
*/
21+
if (process.env.NODE_ENV === "development") {
22+
if (!global._mongoClientPromise) {
23+
client = new MongoClient(uri, options);
24+
global._mongoClientPromise = client.connect();
25+
}
26+
clientPromise = global._mongoClientPromise;
27+
} else {
28+
// In production mode, create a new MongoClient and connect.
29+
client = new MongoClient(uri, options);
30+
clientPromise = client.connect();
31+
}
32+
33+
/**
34+
* Exports a promise that resolves to the MongoClient instance.
35+
* @type {Promise<MongoClient>}
36+
*/
37+
export default clientPromise;

0 commit comments

Comments
 (0)