From db7237b47a90a2a7bbb23713ca62cf1e4f303158 Mon Sep 17 00:00:00 2001 From: Samarth Vaidya Date: Thu, 3 Oct 2024 00:26:48 +0530 Subject: [PATCH 01/14] Added backend form submission for Reservation --- backend/controller/reservation.controller.js | 14 ++++++ backend/index.js | 22 +++++++++- backend/models/reservation.model.js | 21 +++++++++ backend/package.json | 6 ++- frontend/src/components/Pages/Register.jsx | 45 ++++++++++++++++++-- 5 files changed, 102 insertions(+), 6 deletions(-) create mode 100644 backend/controller/reservation.controller.js create mode 100644 backend/models/reservation.model.js diff --git a/backend/controller/reservation.controller.js b/backend/controller/reservation.controller.js new file mode 100644 index 00000000..d2b57078 --- /dev/null +++ b/backend/controller/reservation.controller.js @@ -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, +}; diff --git a/backend/index.js b/backend/index.js index c7f400fc..ee787758 100644 --- a/backend/index.js +++ b/backend/index.js @@ -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"); -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); + }); + +app.post("/create-reservation", createReservation); + +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..d57cb1de --- /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..b06bd3b6 100644 --- a/backend/package.json +++ b/backend/package.json @@ -10,6 +10,10 @@ "license": "ISC", "description": "", "dependencies": { - "express": "^4.21.0" + "cors": "^2.8.5", + "dotenv": "^16.4.5", + "express": "^4.21.0", + "mongoose": "^8.7.0", + "nodemon": "^3.1.7" } } diff --git a/frontend/src/components/Pages/Register.jsx b/frontend/src/components/Pages/Register.jsx index fbe2244c..c79a6646 100644 --- a/frontend/src/components/Pages/Register.jsx +++ b/frontend/src/components/Pages/Register.jsx @@ -1,3 +1,4 @@ +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"; @@ -5,6 +6,40 @@ 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(""); + + 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)); + }; + return ( <>
@@ -46,6 +81,7 @@ 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 +117,7 @@ export default function Register() { setGuests(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" > @@ -103,7 +67,6 @@ 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" /> @@ -117,7 +80,6 @@ export default function Register() { { + setGuests(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" > @@ -67,6 +96,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() {