-
-
Notifications
You must be signed in to change notification settings - Fork 103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added Backend Route for Café Reservation Form and .env Configuration #42
Changes from 2 commits
db7237b
8b7f3b4
1834450
13ca5e7
9c4027e
1fd66bf
39dc5ab
4af9a60
5bf789d
269fcc0
51f78ac
367a8f4
ab8d55e
35d21d2
4a71b38
3a45128
4d880aa
888aa5c
1271b72
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
const Reservation = require("../models/reservation.model"); | ||
|
||
async function createReservation(req, res) { | ||
try { | ||
const reservation = await Reservation.create(req.body); | ||
res.status(201).json(reservation); | ||
} catch (error) { | ||
res.status(400).json({ message: error.message }); | ||
} | ||
} | ||
|
||
module.exports = { | ||
createReservation, | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Codebase verification Missing The project utilizes
🔗 Analysis chainOverall implementation aligns with PR objectives, but verify .env configuration. The However, there are two points to address:
Please ensure that the .env configuration has been implemented correctly. You can verify this by running the following command: This script will check for the presence of a .env file, display its structure (without revealing sensitive values), and search for any usage of environment variables in the codebase. 🏁 Scripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Check for the presence of a .env file and its contents
if [ -f .env ]; then
echo ".env file exists"
# Print the .env file contents without showing actual values
sed 's/=.*/=****/' .env
else
echo ".env file not found"
fi
# Check if any environment variables are used in the codebase
rg -i '(process\.env\.|dotenv)'
Length of output: 242 |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,6 +1,24 @@ | ||||||||||||||||||||||||||||||||||||||||||
const express = require("express"); | ||||||||||||||||||||||||||||||||||||||||||
const cors = require("cors"); | ||||||||||||||||||||||||||||||||||||||||||
const app = express(); | ||||||||||||||||||||||||||||||||||||||||||
const port = 3000; | ||||||||||||||||||||||||||||||||||||||||||
require("dotenv").config(); | ||||||||||||||||||||||||||||||||||||||||||
const mongoose = require("mongoose"); | ||||||||||||||||||||||||||||||||||||||||||
const { createReservation } = require("./controller/reservation.controller"); | ||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Codebase verification
The
🔗 Analysis chainVerify that To ensure that the Run the following script to verify the export: 🏁 Scripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Description: Check if 'createReservation' is exported from 'reservation.controller.js'
# Test: Search for 'exports.createReservation' or 'module.exports' containing 'createReservation'
grep -E 'exports\.createReservation|module\.exports.*createReservation' ./controller/reservation.controller.js
# Also check for ES6 exports
grep -E 'export\s+(function|const|let|var)\s+createReservation' ./controller/reservation.controller.js
Length of output: 361 Script: #!/bin/bash
# Description: Locate 'reservation.controller.js' within the codebase
fd 'reservation.controller.js' .
Length of output: 79 Script: #!/bin/bash
# Description: Verify if 'createReservation' is exported from the correct 'reservation.controller.js' file
# Check for CommonJS exports
grep -E 'exports\.createReservation|module\.exports.*createReservation' ./backend/controller/reservation.controller.js
# Check for ES6 exports
grep -E 'export\s+(function|const|let|var)\s+createReservation' ./backend/controller/reservation.controller.js
Length of output: 233 |
||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
app.get("/", (req, res) => res.send("Hello World!")); | ||||||||||||||||||||||||||||||||||||||||||
app.listen(port, () => console.log(`Example app listening on port ${port}!`)); | ||||||||||||||||||||||||||||||||||||||||||
app.use(cors({})); | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
app.use(express.json()); | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
mongoose | ||||||||||||||||||||||||||||||||||||||||||
.connect(process.env.MONGO_URI) | ||||||||||||||||||||||||||||||||||||||||||
.then(() => { | ||||||||||||||||||||||||||||||||||||||||||
console.log("Connected to MongoDB"); | ||||||||||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||||||||||
.catch((error) => { | ||||||||||||||||||||||||||||||||||||||||||
console.error("Database connection failed:", error); | ||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure If Apply this diff to add a check: +if (!process.env.MONGO_URI) {
+ console.error("Error: MONGO_URI is not defined in environment variables.");
+ process.exit(1);
+}
mongoose
.connect(process.env.MONGO_URI) 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
app.post("/create-reservation", createReservation); | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
app.listen(port, () => console.log(`Server is running on port ${port}!`)); | ||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Make the server port configurable via environment variables Currently, the server port is hardcoded to Apply this diff to make the port configurable: -const port = 3000;
+const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`Server is running on port ${port}!`));
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
const mongoose = require("mongoose"); | ||
const Schema = mongoose.Schema; | ||
|
||
const reservationSchema = new Schema({ | ||
Guests: { | ||
type: String, | ||
required: true, | ||
}, | ||
Date: { | ||
type: String, | ||
required: true, | ||
}, | ||
Time: { | ||
type: String, | ||
required: true, | ||
}, | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Enhance schema definition for better data integrity and consistency While the basic structure is correct, consider the following improvements:
Here's a suggested refactor: const reservationSchema = new Schema({
guests: {
type: Number,
required: true,
min: [1, 'At least one guest is required'],
max: [20, 'Maximum 20 guests allowed']
},
date: {
type: Date,
required: true,
min: [new Date(), 'Date must be in the future']
},
time: {
type: String,
required: true,
match: [/^([0-1]?[0-9]|2[0-3]):[0-5][0-9]$/, 'Time must be in HH:MM format']
}
}); This refactor improves type safety, adds validation, and follows naming conventions. |
||
|
||
const Reservation = mongoose.model("Reservation", reservationSchema); | ||
|
||
module.exports = Reservation; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,45 @@ | ||
import { useEffect, useState } from "react"; | ||
import pic from "../../assets/img/abt1.jpg"; | ||
import pic2 from "../../assets/img/abt1.png"; | ||
import pic3 from "../../assets/img/abt2.png"; | ||
import pic4 from "../../assets/img/abt3.png"; | ||
import pic5 from "../../assets/img/abt4.png"; | ||
|
||
export default function Register() { | ||
const [Guests, setGuests] = useState(); | ||
const [Date, setDate] = useState(""); | ||
const [Time, setTime] = useState(""); | ||
RamakrushnaBiswal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
useEffect(() => { | ||
console.log(Guests); | ||
}, [Guests]); | ||
|
||
useEffect(() => { | ||
console.log(Date); | ||
}, [Date]); | ||
|
||
useEffect(() => { | ||
console.log(Time); | ||
}, [Time]); | ||
|
||
const handleSubmit = (e) => { | ||
e.preventDefault(); | ||
fetch("http://localhost:3000/create-reservation", { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ | ||
Guests, | ||
Date, | ||
Time, | ||
}), | ||
}) | ||
.then((res) => res.json()) | ||
.then((data) => console.log(data)) | ||
.catch((error) => console.log(error)); | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use environment variables for API endpoint and provide user feedback The API endpoint URL Also, currently, the response from the server is logged to the console, and there's no feedback to the user upon successful submission or error. Providing user feedback improves user experience. Apply this diff to use an environment variable for the API endpoint: - fetch("http://localhost:3000/create-reservation", {
+ fetch(`${process.env.REACT_APP_API_BASE_URL}/create-reservation`, { Ensure you have Additionally, handle user feedback by updating the UI based on the response: - .then((data) => console.log(data))
+ .then((data) => {
+ // Handle success (e.g., show a success message or redirect)
+ console.log("Reservation successful:", data);
+ }) And handle errors appropriately: - .catch((error) => console.log(error));
+ .catch((error) => {
+ // Handle error (e.g., show an error message to the user)
+ console.error("Reservation failed:", error);
+ });
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @samar12-rad
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
return ( | ||
<> | ||
<div className="w-full mx-auto mt-10 lg:mt-0 md:mt-0"> | ||
|
@@ -46,6 +81,7 @@ export default function Register() { | |
</label> | ||
<select | ||
id="guests" | ||
onChange={(e) => setGuests(e.target.value)} | ||
RamakrushnaBiswal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
className="flex h-10 w-full items-center rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background placeholder:text-muted-foreground focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50" | ||
> | ||
<option value="">Select number of guests</option> | ||
|
@@ -67,7 +103,8 @@ export default function Register() { | |
<input | ||
type="date" | ||
id="date" | ||
className="flex h-10 w-full items-center rounded-md border border-input bg-white px-3 py-2 text-sm ring-offset-background placeholder:text-muted-foreground focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50" | ||
onChange={(e) => setDate(e.target.value)} | ||
className="flex h-10 w-full items-center rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background placeholder:text-muted-foreground focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50" | ||
/> | ||
</div> | ||
</div> | ||
|
@@ -80,6 +117,7 @@ export default function Register() { | |
</label> | ||
<select | ||
id="time" | ||
onChange={(e) => setTime(e.target.value)} | ||
className="flex h-10 w-full items-center rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background placeholder:text-muted-foreground focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50" | ||
> | ||
<option value="">Select time</option> | ||
|
@@ -93,6 +131,7 @@ export default function Register() { | |
<button | ||
className="inline-flex items-center justify-center p-4 bg-[#D9D9D9] hover:bg-[#C9C9C9]" | ||
type="submit" | ||
onClick={handleSubmit} | ||
> | ||
Reserve Now | ||
</button> | ||
|
@@ -101,8 +140,8 @@ export default function Register() { | |
</div> | ||
</section> | ||
<h1 className="text-3xl sm:text-4xl md:text-5xl lg:text-6xl font-bold tracking-tighter text-amber-100 bg-green-900 p-5 text-center"> | ||
Popular Board Games | ||
</h1> | ||
Popular Board Games | ||
</h1> | ||
<div className="mt-8 w-full flex justify-center bg-white "> | ||
<div className="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 gap-4 mt-8 mb-10"> | ||
<div | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider adding input validation and enhancing error handling.
While the overall structure of the
createReservation
function is correct, there are a few areas that could be improved:Data Validation: Currently,
req.body
is passed directly toReservation.create()
. This could lead to security vulnerabilities if the input is not properly validated.Error Handling: The current error handling is basic. Consider adding more specific error handling for different types of errors (e.g., validation errors, database errors).
Response Security: Sending the entire reservation object in the response might expose sensitive data. Consider returning only necessary information.
Here's a suggested improvement:
This suggestion includes input validation, more specific error handling, and a more secure response. Make sure to implement the
validateReservation
function in a separate utility file.