Skip to content

0xZeyad11/Restaurant---Backend-API

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Restaurant Backend API

A comprehensive Node.js backend API for a restaurant food delivery application built with Express.js, MongoDB, and JWT authentication.

πŸš€ Features

  • User Management: Registration, login, profile management with role-based access (client, admin, vendor, driver)
  • Restaurant Management: CRUD operations for restaurants with location coordinates
  • Food Management: Complete food item management with categories and ratings
  • Category Management: Food category organization
  • Order Management: Place orders and track order status
  • Authentication & Authorization: JWT-based authentication with middleware protection
  • Password Management: Update and reset password functionality

πŸ› οΈ Tech Stack

  • Backend: Node.js, Express.js
  • Database: MongoDB with Mongoose ODM
  • Authentication: JSON Web Tokens (JWT)
  • Password Hashing: bcryptjs
  • Environment Variables: dotenv
  • CORS: Cross-Origin Resource Sharing enabled
  • Development: Nodemon for auto-restart

πŸ“ Project Structure

Restaurant-Backend-API/
β”œβ”€β”€ config/
β”‚   └── db.js                 # Database connection configuration
β”œβ”€β”€ controllers/
β”‚   β”œβ”€β”€ authController.js     # Authentication logic
β”‚   β”œβ”€β”€ categoryController.js # Category management
β”‚   β”œβ”€β”€ foodController.js     # Food item management
β”‚   β”œβ”€β”€ restaurantController.js # Restaurant management
β”‚   └── userController.js     # User profile management
β”œβ”€β”€ middleware/
β”‚   └── authMiddleware.js     # JWT authentication middleware
β”œβ”€β”€ models/
β”‚   β”œβ”€β”€ categoryModel.js      # Category schema
β”‚   β”œβ”€β”€ foodModel.js          # Food item schema
β”‚   β”œβ”€β”€ orderModel.js         # Order schema
β”‚   β”œβ”€β”€ restaurantModel.js    # Restaurant schema
β”‚   └── userModel.js          # User schema
β”œβ”€β”€ routes/
β”‚   β”œβ”€β”€ authRoutes.js         # Authentication routes
β”‚   β”œβ”€β”€ categoryRoutes.js     # Category routes
β”‚   β”œβ”€β”€ foodRoutes.js         # Food routes
β”‚   β”œβ”€β”€ restaurantRoutes.js   # Restaurant routes
β”‚   β”œβ”€β”€ testRoutes.js         # Test routes
β”‚   └── userRoutes.js         # User routes
β”œβ”€β”€ .env                      # Environment variables
β”œβ”€β”€ .gitignore               # Git ignore file
β”œβ”€β”€ package.json             # Dependencies and scripts
└── server.js                # Main server file

βš™οΈ Installation & Setup

Prerequisites

  • Node.js (v14 or higher)
  • MongoDB database
  • npm or yarn package manager

Installation Steps

  1. Clone the repository

    git clone <repository-url>
    cd Restaurant-Backend-API
  2. Install dependencies

    npm install
  3. Environment Configuration

    Create a .env file in the root directory and configure the following variables:

    PORT=8080
    MONGO_URL=your_mongodb_connection_string
    JWT_SECRET=your_jwt_secret_key
  4. Start the server

    # Development mode with auto-restart
    npm start
    
    # Or directly with node
    node server.js

The server will start on http://localhost:8080

πŸ“š API Documentation

Base URL

http://localhost:8080/api/v1

Authentication

Most endpoints require JWT authentication. Include the token in the Authorization header:

Authorization: Bearer <your_jwt_token>

API Endpoints

πŸ” Authentication Routes (/auth)

  • POST /auth/register - Register a new user
  • POST /auth/login - User login

πŸ‘€ User Routes (/user) - Requires Authentication

  • GET /user/getUser - Get current user profile
  • PUT /user/updateUser - Update user profile
  • POST /user/updateUserPass - Update password
  • POST /user/resetUserPass - Reset password
  • DELETE /user/deleteUser/:id - Delete user account

πŸͺ Restaurant Routes (/restaurant)

  • POST /restaurant/create - Create restaurant (Auth required)
  • GET /restaurant/getAll - Get all restaurants
  • GET /restaurant/get/:id - Get restaurant by ID
  • DELETE /restaurant/delete/:id - Delete restaurant (Auth required)

πŸ• Food Routes (/food)

  • POST /food/create - Create food item (Auth required)
  • GET /food/getAll - Get all food items
  • GET /food/get/:id - Get food item by ID
  • GET /food/getByResturant/:id - Get foods by restaurant
  • PUT /food/update/:id - Update food item (Auth required)
  • DELETE /food/delete/:id - Delete food item (Auth required)
  • POST /food/placeorder - Place an order (Auth required)
  • POST /food/orderstatus/:id - Update order status (Auth required)

πŸ“‚ Category Routes (/category)

  • POST /category/create - Create category (Auth required)
  • GET /category/getall - Get all categories
  • GET /category/get/:id - Get category by ID
  • PUT /category/update/:id - Update category (Auth required)
  • DELETE /category/delete/:id - Delete category (Auth required)

πŸ§ͺ Test Routes (/test)

  • Test endpoints for API health checks

πŸ“Š Data Models

User Model

{
  username: String (required),
  email: String (required, unique),
  password: String (required),
  address: String (required),
  phone: String (required),
  usertype: String (enum: ['client', 'admin', 'vendor', 'driver']),
  profile: String (default profile image URL)
}

Restaurant Model

{
  title: String (required),
  imageUrl: String,
  foods: Array,
  pickup: Boolean (default: true),
  delivery: Boolean (default: true),
  isOpen: Boolean (default: true),
  logoUrl: String,
  rating: Number (1-5, default: 1),
  ratingCount: String,
  code: String,
  coords: {
    id: String,
    latitude: Number,
    longitude: Number,
    address: String,
    title: String
  }
}

Food Model

{
  title: String (required),
  description: String (required),
  price: Number (required),
  imageUrl: String (default image),
  foodTags: String,
  category: String,
  code: String,
  isAvailable: Boolean (default: true),
  restaurant: ObjectId (ref: Restaurant),
  rating: Number (1-5, default: 5),
  ratingCount: String
}

Category Model

{
  title: String (required),
  imageUrl: String (default image)
}

πŸ”’ User Roles

  • Client: Regular customers who can browse and order food
  • Admin: Full system access and management capabilities
  • Vendor: Restaurant owners who can manage their restaurants and food items
  • Driver: Delivery personnel for order fulfillment

🚦 Response Format

All API responses follow a consistent format:

Success Response:

{
  "success": true,
  "message": "Operation successful",
  "data": { ... }
}

Error Response:

{
  "success": false,
  "message": "Error description",
  "error": "Detailed error message"
}

πŸ§ͺ Testing

The API includes test routes for health checks. Access them at:

GET /api/v1/test

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

πŸš€ Deployment

For production deployment:

  1. Set up environment variables on your hosting platform
  2. Ensure MongoDB connection is properly configured
  3. Set NODE_ENV=production
  4. Use a process manager like PM2 for production

About

A backend for serving restaurants

Resources

Stars

Watchers

Forks