-
-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from samar12-rad/reservation
Added Backend Route for Café Reservation Form and .env Configuration
- Loading branch information
Showing
11 changed files
with
178 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
MONGO_URI=mongodb+srv://vsamarth1212:[email protected]/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
MONGO_URI=enter_your_mongo_uri |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
node_modules | ||
dist | ||
.env | ||
|
||
package-lock.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
const { z } = require("zod"); | ||
const Reservation = require("../models/reservation.model"); | ||
|
||
// Define the Zod schema for reservation validation | ||
const reservationSchema = z.object({ | ||
guests: z.string(), | ||
date: z.string(), | ||
time: z.string(), | ||
}); | ||
|
||
async function createReservation(req, res) { | ||
try { | ||
const validationResult = reservationSchema.safeParse(req.body); | ||
|
||
if (!validationResult.success) { | ||
console.error("Validation error:", validationResult.error.errors); | ||
return res.status(400).json({ | ||
success: false, | ||
message: "Validation failed", | ||
errors: validationResult.error.errors, | ||
}); | ||
} | ||
|
||
const reservation = await Reservation.create(validationResult.data); | ||
|
||
res.status(201).json({ | ||
success: true, | ||
message: "Reservation created successfully", | ||
data: reservation, | ||
}); | ||
} catch (error) { | ||
console.error("Error creating reservation:", error); | ||
res.status(500).json({ | ||
success: false, | ||
message: "An error occurred while creating the reservation", | ||
}); | ||
} | ||
} | ||
|
||
module.exports = { | ||
createReservation, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,32 @@ | ||
const express = require("express"); | ||
const cors = require("cors"); | ||
const app = express(); | ||
const port = 3000; | ||
require("dotenv").config(); | ||
const mongoose = require("mongoose"); | ||
|
||
app.get("/", (req, res) => res.send("Hello World!")); | ||
app.listen(port, () => console.log(`Example app listening on port ${port}!`)); | ||
app.use( | ||
cors({ | ||
origin: ["http://localhost:5173", "https://play-cafe.vercel.app"], | ||
}) | ||
); | ||
|
||
app.use(express.json()); | ||
|
||
mongoose | ||
.connect(process.env.MONGO_URI, { | ||
useNewUrlParser: true, | ||
useUnifiedTopology: true, | ||
}) | ||
.then(() => { | ||
console.log("Connected to MongoDB"); | ||
}) | ||
.catch((error) => { | ||
console.error("Database connection failed:", error.message); | ||
console.error(error.stack); | ||
process.exit(1); | ||
}); | ||
|
||
app.use("/api", require("./routes/index")); | ||
|
||
app.listen(port, () => console.log(`Server is running on port ${port}!`)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}); | ||
|
||
const Reservation = mongoose.model("Reservation", reservationSchema); | ||
|
||
module.exports = Reservation; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const express = require("express"); | ||
const Reservation = require("../models/reservation.model"); | ||
|
||
const router = express.Router(); | ||
|
||
router.use("/reservation", require("./reservationRouter")); | ||
router.get("/", (req, res) => { | ||
res.json({ | ||
message: "Welcome to the restaurant API!", | ||
version: "1.0.0", | ||
endpoints: { | ||
Reservation: "/reservation", | ||
}, | ||
documentation: "https://api-docs-url.com", | ||
}); | ||
}); | ||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
const express = require("express"); | ||
const { createReservation } = require("../controller/reservation.controller"); | ||
const router = express.Router(); | ||
|
||
router.post("/create", createReservation); | ||
router.get("/", (req, res) => { | ||
res.json({ | ||
message: "Welcome to the restaurant reservation API!", | ||
version: "1.0.0", | ||
endpoints: { | ||
createReservation: "/create [POST]", | ||
}, | ||
documentation: "https://api-docs-url.com", | ||
}); | ||
}); | ||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters