https://github.com/rahul-0000-code/rest_api
https://github.com/rahul-0000-code/rest_api
Last synced: about 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/rahul-0000-code/rest_api
- Owner: rahul-0000-code
- Created: 2024-03-27T13:27:40.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-03-27T13:31:30.000Z (about 2 years ago)
- Last Synced: 2025-01-29T08:44:31.765Z (over 1 year ago)
- Language: JavaScript
- Size: 23.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Online Restaurant Service REST API
This is a REST API for a hypothetical online restaurant service. It allows users to perform CRUD operations on products, place orders, and view orders.
## Use Case
1. **Create, Read, Update, Delete Product**: Users can perform CRUD operations on products. Each product has a name, price, and description.
2. **Place Order for a Product**: Users can place orders for products, specifying the product and quantity.
3. **List All Orders**: Users can view all orders placed.
## Environment Variables
To run the server, create a `.env` file in the project directory with the following format:
```
MONGODB_URL=mongodb://localhost:27017/restaurant_db
PORT=3000
JWT_SECRET=your_secret_key
```
- `MONGODB_URL`: The URL of your MongoDB database.
- `PORT`: The port number for the server to listen on.
- `JWT_SECRET`: Secret key used for signing JWT tokens.
## Running the Server
1. Install dependencies:
```bash
npm install
```
2. Start MongoDB server:
```bash
mongod
```
3. Start the server:
```bash
node server.js
```
## API Endpoints
- **POST /register**: Register a new user.
- **POST /login**: Login with username and password to get JWT token.
- **POST /products**: Create a new product.
- **GET /products**: Get all products.
- **PUT /products/:id**: Update a product by ID.
- **DELETE /products/:id**: Delete a product by ID.
- **POST /orders**: Place a new order.
- **GET /orders**: Get all orders.
## Sample Requests
- **Register User**:
```bash
curl -X POST -H "Content-Type: application/json" -d '{"username":"your_username", "password":"your_password"}' http://localhost:3000/register
```
- **Login**:
```bash
curl -X POST -H "Content-Type: application/json" -d '{"username":"your_username", "password":"your_password"}' http://localhost:3000/login
```
- **Create Product**:
```bash
curl -X POST -H "Authorization: Bearer " -H "Content-Type: application/json" -d '{"name":"Product Name", "price":10, "description":"Product Description"}' http://localhost:3000/products
```
- **List Products**:
```bash
curl http://localhost:3000/products
```
- **Place Order**:
```bash
curl -X POST -H "Authorization: Bearer " -H "Content-Type: application/json" -d '{"product":"", "quantity":1}' http://localhost:3000/orders
```
- **List Orders**:
```bash
curl -H "Authorization: Bearer " http://localhost:3000/orders
```
Replace ``, ``, ``, and `` with appropriate values.
```