This is a simple RESTful API for managing a food ordering system for Milestone1 under TCE. It supports operations to manage the menu, place orders, and track order statuses. The API is built with Express and Node.js, and it uses a cron job to update the order statuses (e.g., "Preparing", "Out for Delivery", "Delivered").
- Menu Management: Add or update food items in the menu.
- Order Management: Place orders, view order details, and track order statuses.
- Order Status Updates: The system automatically updates the order status based on the prepared and delivery times using a cron job.
- Backend: Node.js, Express.js
- Database: In-memory storage (Menu and Orders)
- Cron Jobs:
node-cron
- Postman: API testing with the provided Postman collection.
- Node.js: Version 14.x or higher
- npm: Comes bundled with Node.js
Clone this repository to your local machine:
https://github.com/msranjana/SL_FoodDeliverySystem.git
Navigate to the project directory and install the required dependencies:
cd SL_FoodDeliverySystem-main
npm install
Start the server:
node index.js
The API will be available at http://localhost:3000.
- Method:
POST
- Endpoint:
/menu
- Description: Adds a new menu item to the system.
- Request Body:
{ "name": "Pizza", "price": 150, "category": "Main Course" }
{ "name": "Cheesecake", "price": 70, "category": "Dessert" }
{ "name": "Sushi", "price": -50, "category": "Main Course" }
- Response:
{ "message": "Menu item added successfully" }
- Error Response:
{ "error": "Invalid menu item data" }
- Method:
GET
- Endpoint:
/menu
- Description: Retrieves all menu items.
- Response:
[ { "id": 1, "name": "Pizza", "price": 15, "category": "Main Course" }, { "id": 2, "name": "Cheesecake", "price": 7, "category": "Dessert" } ]
- Method:
POST
- Endpoint:
/orders
- Description: Places a new order with selected menu items.
- Request Body:
{ "items": [1, 2] }
- Response:
{ "message": "Order placed successfully", "orderId": 1 }
- Error Response:
{ "error": "Invalid order data" }
- Method:
GET
- Endpoint:
/orders/:id
- Description: Retrieves details of a specific order by its ID.
- Path Parameter: id: Order ID (integer).
- Response:
{ "id": 1, "items": [1, 2], "status": "Preparing", "createdAt": "2024-12-04T09:00:00.000Z" }
- Error Response:
{ "error": "Order not found" }
The system automatically updates the status of orders every minute via a CRON job:
- Preparing → Out for Delivery
- Out for Delivery → Delivered
- Place an order using the Place Order endpoint.
- Fetch the order details periodically using the Get Order Details endpoint to observe the status updates.
-
Description: Provide a section explaining common error responses and validation for better debugging.
- 400 Bad Request: When the input data is invalid, for example, a negative price for a menu item or missing required fields in the order.
- 404 Not Found: If the menu item or order ID does not exist.
- 500 Internal Server Error: For unexpected server errors or issues during the operation.
{ "error": "Invalid price. Price must be a positive number." }
After starting the server with node index.js
, you can use Postman to make the following requests:
- Add a Menu Item:
POST /menu
- Get All Menu Items:
GET /menu
- Place an Order:
POST /orders
- Get Order Details:
GET /orders/:id
Make sure to use valid data for menu items and orders. The API will return appropriate status codes:
- 200 OK: The request was successful.
- 400 Bad Request: Invalid data or missing fields.
- 404 Not Found: Resource (order/menu item) not found.
- 500 Internal Server Error: Something went wrong with the server.