diff --git a/server/controllers/bookingController.ts b/server/controllers/bookingController.ts new file mode 100644 index 0000000..4f63316 --- /dev/null +++ b/server/controllers/bookingController.ts @@ -0,0 +1,103 @@ + +import { Request, Response } from "express"; +import Booking from "../models/Booking.js"; +import Restaurant from "../models/Restaurants.js"; + +// ====================================================== +// Create a new booking +// POST /api/bookings +// @access Private +// ====================================================== +export const createBooking = async ( + req: Request, + res: Response +): Promise => { + try { + const { + restaurant, + bookingDate, + timeSlot, + guests, + specialRequest, + } = req.body; + + // User comes from auth middleware + const user = (req as any).user?.id; + + if (!user) { + res.status(401).json({ + success: false, + message: "Unauthorized", + }); + return; + } + + // Check restaurant exists + const restaurantExists = await Restaurant.findById(restaurant); + + if (!restaurantExists) { + res.status(404).json({ + success: false, + message: "Restaurant not found", + }); + return; + } + + // Check duplicate booking + const existingBooking = await Booking.findOne({ + user, + restaurant, + bookingDate: new Date(bookingDate), + timeSlot, + status: { + $in: ["pending", "confirmed"], + }, + }); + + if (existingBooking) { + res.status(400).json({ + success: false, + message: "You already have a booking for this time slot.", + }); + return; + } + + const booking = await Booking.create({ + user, + restaurant, + bookingDate, + timeSlot, + guests, + specialRequest, + status: "pending", + }); + + await booking.populate("restaurant", "name image location"); + + res.status(201).json({ + success: true, + message: "Booking created successfully.", + booking, + }); + } catch (error: any) { + console.error(error); + + res.status(500).json({ + success: false, + message: error.message, + }); + } +}; +export const getMyBooking = async ( + req: Request, + res: Response +): Promise => { + // code +}; + +export const cancelBooking = async ( + req: Request, + res: Response +): Promise => { + // code +}; \ No newline at end of file diff --git a/server/routes/booking.routes.ts b/server/routes/booking.routes.ts new file mode 100644 index 0000000..6d7a573 --- /dev/null +++ b/server/routes/booking.routes.ts @@ -0,0 +1,24 @@ +import { Router } from "express"; +import { + createBooking, + getMyBooking, + cancelBooking, +} from "../controllers/bookingController.js"; + +import { protect } from "../middlewares/auth.js"; + +const bookingRouter = Router(); + +// Create a new booking +// POST /api/bookings +bookingRouter.post("/", protect, createBooking); + +// Get logged-in user's bookings +// GET /api/bookings/my +bookingRouter.get("/my", protect, getMyBooking); + +// Cancel a booking +// PUT /api/bookings/:id/cancel +bookingRouter.put("/:id/cancel", protect, cancelBooking); + +export default bookingRouter; \ No newline at end of file diff --git a/server/server.ts b/server/server.ts index de36e31..8289d2e 100644 --- a/server/server.ts +++ b/server/server.ts @@ -8,6 +8,7 @@ import cors from "cors"; import connectDB from "./config/db.js"; import authRouter from "./routes/authRoutes.js"; import restaurantRouter from "./routes/restaurant.routes.js"; +import bookingRouter from "./routes/booking.routes.js"; const app = express(); @@ -26,7 +27,7 @@ app.get("/", (req: Request, res: Response) => { app.use("/api/auth", authRouter); app.use("/api/restaurants", restaurantRouter); - +app.use("/api/bookings", bookingRouter); // Global Error Handler app.use(( err: Error,