-
Notifications
You must be signed in to change notification settings - Fork 0
created API for restaurants #2
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,202 @@ | ||
| import { Request, Response } from "express"; | ||
| import Restaurant from "../models/Restaurants.js"; | ||
| import Booking from "../models/Booking.js"; | ||
|
|
||
| // ====================================================== | ||
| // Get all restaurants | ||
| // GET /api/restaurants | ||
| // ====================================================== | ||
| export const getRestaurants = async ( | ||
| req: Request, | ||
| res: Response | ||
| ): Promise<void> => { | ||
| try { | ||
| const { | ||
| search, | ||
| cuisine, | ||
| priceRange, | ||
| featured, | ||
| page = "1", | ||
| limit = "10", | ||
| } = req.query; | ||
|
|
||
| const query: any = { | ||
| status: "approved", | ||
| }; | ||
|
|
||
| if (search) { | ||
| if (typeof search !== "string" || search.length > 100) { | ||
| res.status(400).json({ success: false, message: "Invalid search query" }); | ||
| return; | ||
| } | ||
| const escapedSearch = search.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); | ||
| query.name = { | ||
| $regex: escapedSearch, | ||
| $options: "i", | ||
| }; | ||
| } | ||
|
|
||
| if (cuisine) { | ||
| query.cuisine = cuisine; | ||
| } | ||
|
|
||
| if (priceRange) { | ||
| query.priceRange = priceRange; | ||
| } | ||
|
|
||
| if (featured !== undefined) { | ||
| query.featured = featured === "true"; | ||
| } | ||
|
|
||
| const pageNumber = Number(page); | ||
| const limitNumber = Number(limit); | ||
|
|
||
| const restaurants = await Restaurant.find(query) | ||
| .populate("owner", "name email") | ||
|
prepwave marked this conversation as resolved.
|
||
| .sort({ createdAt: -1 }) | ||
| .skip((pageNumber - 1) * limitNumber) | ||
| .limit(limitNumber); | ||
|
prepwave marked this conversation as resolved.
|
||
|
|
||
| const total = await Restaurant.countDocuments(query); | ||
|
|
||
| res.status(200).json({ | ||
| success: true, | ||
| total, | ||
| page: pageNumber, | ||
| totalPages: Math.ceil(total / limitNumber), | ||
| restaurants, | ||
| }); | ||
| } catch (error: any) { | ||
| console.error(error); | ||
| res.status(500).json({ | ||
| success: false, | ||
| message: error.message, | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
| // ====================================================== | ||
| // Get Featured Restaurants | ||
| // GET /api/restaurants/featured | ||
| // ====================================================== | ||
| export const getFeaturedRestaurants = async ( | ||
| req: Request, | ||
| res: Response | ||
| ): Promise<void> => { | ||
| try { | ||
| const restaurants = await Restaurant.find({ | ||
| status: "approved", | ||
| featured: true, | ||
| exclusive: true, | ||
| }) | ||
| .sort({ rating: -1 }) | ||
| .limit(8); | ||
|
|
||
| res.status(200).json({ | ||
| success: true, | ||
| count: restaurants.length, | ||
| restaurants, | ||
| }); | ||
| } catch (error: any) { | ||
| console.error(error); | ||
|
|
||
| res.status(500).json({ | ||
| success: false, | ||
| message: error.message, | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
| // ====================================================== | ||
| // Get Restaurant By Slug | ||
| // GET /api/restaurants/:slug | ||
| // ====================================================== | ||
| export const getSlugsRestaurants = async ( | ||
| req: Request, | ||
| res: Response | ||
| ): Promise<void> => { | ||
| try { | ||
| const { slug } = req.params; | ||
|
|
||
| const restaurant = await Restaurant.findOne({ | ||
| slug, | ||
| status: "approved", | ||
| }).populate("owner", "name email"); | ||
|
|
||
| if (!restaurant) { | ||
| res.status(404).json({ | ||
| success: false, | ||
| message: "Restaurant not found", | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| res.status(200).json({ | ||
| success: true, | ||
| restaurant, | ||
| }); | ||
| } catch (error: any) { | ||
| console.error(error); | ||
|
|
||
| res.status(500).json({ | ||
| success: false, | ||
| message: error.message, | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
| // ====================================================== | ||
| // Get Active Bookings for Restaurant on Selected Date | ||
| // GET /api/restaurants/:restaurantId/bookings?date=2026-07-18 | ||
| // ====================================================== | ||
| export const getRestaurantBookings = async ( | ||
| req: Request, | ||
| res: Response | ||
| ): Promise<void> => { | ||
| try { | ||
| const { restaurantId } = req.params; | ||
| const { date } = req.query; | ||
|
|
||
| if (!date) { | ||
| res.status(400).json({ | ||
| success: false, | ||
| message: "Date is required.", | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| const selectedDate = new Date(date as string); | ||
|
|
||
| const startOfDay = new Date(selectedDate); | ||
| startOfDay.setHours(0, 0, 0, 0); | ||
|
|
||
| const endOfDay = new Date(selectedDate); | ||
| endOfDay.setHours(23, 59, 59, 999); | ||
|
|
||
| const bookings = await Booking.find({ | ||
| restaurant: restaurantId, | ||
| bookingDate: { | ||
| $gte: startOfDay, | ||
| $lte: endOfDay, | ||
| }, | ||
| status: { | ||
| $in: ["pending", "confirmed"], | ||
| }, | ||
| }) | ||
|
prepwave marked this conversation as resolved.
|
||
| .populate("user", "name email") | ||
| .sort({ timeSlot: 1 }); | ||
|
|
||
| res.status(200).json({ | ||
| success: true, | ||
| count: bookings.length, | ||
| bookings, | ||
| }); | ||
| } catch (error: any) { | ||
| console.error(error); | ||
|
|
||
| res.status(500).json({ | ||
| success: false, | ||
| message: error.message, | ||
| }); | ||
| } | ||
| }; | ||
This file contains hidden or 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,75 @@ | ||
| import { Document, model, Schema, Types } from "mongoose"; | ||
|
|
||
| export interface IBooking extends Document { | ||
| user: Types.ObjectId; | ||
| restaurant: Types.ObjectId; | ||
|
|
||
| bookingDate: Date; | ||
| timeSlot: string; | ||
|
|
||
| guests: number; | ||
| cccasion?: String; | ||
| specialRequest?: string; | ||
|
prepwave marked this conversation as resolved.
|
||
|
|
||
| status: "pending" | "confirmed" | "cancelled" | "completed"; | ||
|
|
||
| createdAt: Date; | ||
| updatedAt: Date; | ||
| } | ||
|
|
||
| const BookingSchema = new Schema<IBooking>( | ||
| { | ||
| user: { | ||
| type: Schema.Types.ObjectId, | ||
| ref: "User", | ||
| required: true, | ||
| }, | ||
|
|
||
| restaurant: { | ||
| type: Schema.Types.ObjectId, | ||
| ref: "Restaurant", | ||
| required: true, | ||
| }, | ||
|
|
||
| bookingDate: { | ||
| type: Date, | ||
| required: true, | ||
| }, | ||
|
|
||
| timeSlot: { | ||
| type: String, | ||
| required: true, | ||
| }, | ||
|
|
||
| guests: { | ||
| type: Number, | ||
| required: true, | ||
| min: 1, | ||
| }, | ||
|
|
||
| specialRequest: { | ||
| type: String, | ||
| trim: true, | ||
| default: "", | ||
| }, | ||
|
|
||
| status: { | ||
| type: String, | ||
| enum: ["pending", "confirmed", "cancelled", "completed"], | ||
| default: "pending", | ||
| }, | ||
| }, | ||
| { | ||
| timestamps: true, | ||
| versionKey: false, | ||
| } | ||
| ); | ||
|
|
||
| // Indexes | ||
| BookingSchema.index({ user: 1 }); | ||
| BookingSchema.index({ restaurant: 1 }); | ||
| BookingSchema.index({ bookingDate: 1 }); | ||
|
|
||
| const Booking = model<IBooking>("Booking", BookingSchema); | ||
|
|
||
| export default Booking; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.