diff --git a/backend/.env b/backend/.env new file mode 100644 index 00000000..e9921c4c --- /dev/null +++ b/backend/.env @@ -0,0 +1 @@ +MONGO_URI=mongodb+srv://vsamarth1212:vinodvaidya1234@cluster0.dp1v0xa.mongodb.net/ \ No newline at end of file diff --git a/backend/.env.exapmle b/backend/.env.exapmle new file mode 100644 index 00000000..c8cf3d4e --- /dev/null +++ b/backend/.env.exapmle @@ -0,0 +1 @@ +MONGO_URI=enter_your_mongo_uri \ No newline at end of file diff --git a/backend/.gitignore b/backend/.gitignore index e5af36a7..1e0339c6 100644 --- a/backend/.gitignore +++ b/backend/.gitignore @@ -1,4 +1,4 @@ node_modules dist -.env + package-lock.json \ No newline at end of file diff --git a/backend/controller/reservation.controller.js b/backend/controller/reservation.controller.js new file mode 100644 index 00000000..f5631326 --- /dev/null +++ b/backend/controller/reservation.controller.js @@ -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, +}; diff --git a/backend/index.js b/backend/index.js index c7f400fc..cd1df4b7 100644 --- a/backend/index.js +++ b/backend/index.js @@ -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}!`)); diff --git a/backend/models/reservation.model.js b/backend/models/reservation.model.js new file mode 100644 index 00000000..07d7effb --- /dev/null +++ b/backend/models/reservation.model.js @@ -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; diff --git a/backend/package.json b/backend/package.json index 70ceb0e8..eb0cd14e 100644 --- a/backend/package.json +++ b/backend/package.json @@ -3,6 +3,7 @@ "version": "1.0.0", "main": "index.js", "scripts": { + "start": "nodemon index.js", "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], @@ -10,6 +11,13 @@ "license": "ISC", "description": "", "dependencies": { - "express": "^4.21.0" + "cors": "^2.8.5", + "dotenv": "^16.4.5", + "express": "^4.21.0", + "mongoose": "^8.7.0", + "zod": "^3.23.8" + }, + "devDependencies": { + "nodemon": "^3.1.7" } } diff --git a/backend/routes/index.js b/backend/routes/index.js new file mode 100644 index 00000000..8503b4c3 --- /dev/null +++ b/backend/routes/index.js @@ -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; diff --git a/backend/routes/reservationRouter.js b/backend/routes/reservationRouter.js new file mode 100644 index 00000000..f741c1b3 --- /dev/null +++ b/backend/routes/reservationRouter.js @@ -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; diff --git a/frontend/package.json b/frontend/package.json index 0dc66924..92fa21fd 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -13,6 +13,7 @@ "@splidejs/splide": "^4.1.4", "@splidejs/splide-extension-auto-scroll": "^0.5.3", "autoprefixer": "^10.4.19", + "axios": "^1.7.7", "clsx": "^2.1.1", "framer-motion": "^11.5.6", "react": "^18.3.1", diff --git a/frontend/src/components/Pages/Register.jsx b/frontend/src/components/Pages/Register.jsx index fbe2244c..881bf3f3 100644 --- a/frontend/src/components/Pages/Register.jsx +++ b/frontend/src/components/Pages/Register.jsx @@ -1,3 +1,4 @@ +import { useState } from "react"; import pic from "../../assets/img/abt1.jpg"; import pic2 from "../../assets/img/abt1.png"; import pic3 from "../../assets/img/abt2.png"; @@ -5,6 +6,31 @@ import pic4 from "../../assets/img/abt3.png"; import pic5 from "../../assets/img/abt4.png"; export default function Register() { + const [date, setDate] = useState(""); + const [time, setTime] = useState(""); + const [guests, setGuests] = useState(); + + const handleSubmit = (e) => { + console.log(guests); + console.log(time); + console.log(date); + e.preventDefault(); + fetch("http://localhost:3000/api/reservation/create", { + 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)); + }; + return ( <>
@@ -46,6 +72,9 @@ export default function Register() { { + 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" />
@@ -80,6 +112,9 @@ export default function Register() {