|
| 1 | +# Pizza Ordering API & CLI |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This project is a Pizza Ordering System with both server-side and client-side components, providing a RESTful API and a CLI interface for users to interact with the system. It includes various customer and admin features, including menu management, order creation, user registration, and comprehensive error handling. |
| 6 | + |
| 7 | +## Prerequisites |
| 8 | + |
| 9 | +1. Python 3.x installed on your machine |
| 10 | +2. Environment Variable Setup: |
| 11 | +Set the ``ADMIN_TOKEN`` environment variable to secure the admin API. |
| 12 | + |
| 13 | + export ADMIN_TOKEN=your_secure_token |
| 14 | + |
| 15 | +## Setup |
| 16 | +1. Run the Server: |
| 17 | + |
| 18 | + python3 server.py |
| 19 | +You should see the message ``Server running on port 8080``. |
| 20 | + |
| 21 | +2. Run the CLI: |
| 22 | + |
| 23 | + python3 cli.py |
| 24 | + |
| 25 | +## Server API Endpoints |
| 26 | +### Customer Endpoints |
| 27 | +**1. List Menu** |
| 28 | +- Endpoint: ``GET /menu`` |
| 29 | +- Description: Retrieves the list of available pizzas. |
| 30 | +- Response: JSON array of pizza names. |
| 31 | + |
| 32 | +**2. Create Order** |
| 33 | +- Endpoint: ``POST /order`` |
| 34 | +- Description: Creates an order with a pizza and either a username or address. |
| 35 | +Body: |
| 36 | +``` |
| 37 | +{ |
| 38 | + "pizza": "Margherita", |
| 39 | + "username": "alex_smith", // Optional if address is provided |
| 40 | + "address": "12 Main St" // Required if username is not provided |
| 41 | +} |
| 42 | +``` |
| 43 | +- Response: JSON object with order_id. |
| 44 | + |
| 45 | +**3. Check Order Status** |
| 46 | +- Endpoint: ``GET /order/{order_id}`` |
| 47 | +- Description: Checks the status of a specific order by ``order_id``. |
| 48 | +- Response: JSON object with order details. |
| 49 | + |
| 50 | +**4. Cancel Order** |
| 51 | +- Endpoint: ``DELETE /order/{order_id}`` |
| 52 | +- Description: Cancels an order if its status is not ``ready_to_be_delivered``. |
| 53 | +- Response: Success or error message. |
| 54 | + |
| 55 | +## Admin Endpoints |
| 56 | +**1. Add Pizza to Menu** |
| 57 | +- Endpoint: ``POST /menu`` |
| 58 | +- Authorization: Requires Authorization header with the ``ADMIN_TOKEN``. |
| 59 | +- Body: |
| 60 | +``` |
| 61 | +{ |
| 62 | + "pizza": "New Pizza Name" |
| 63 | +} |
| 64 | +``` |
| 65 | +- Response: Success or error message. |
| 66 | + |
| 67 | +**2. Delete Pizza from Menu** |
| 68 | +- Endpoint: ``DELETE /menu/{pizza_id}`` |
| 69 | +- Authorization: Requires Authorization header with the ``ADMIN_TOKEN``. |
| 70 | +- Response: Success or error message. |
| 71 | + |
| 72 | +**3. Cancel Any Order (Admin Only)** |
| 73 | +- Endpoint: ``DELETE /admin/order/{order_id}`` |
| 74 | +- Authorization: Requires Authorization header with the ``ADMIN_TOKEN``. |
| 75 | +- Response: Success or error message. |
| 76 | + |
| 77 | +**4. Register User** |
| 78 | +- Endpoint: ``POST /register`` |
| 79 | +- Description: Registers a new user with a ``username`` and ``address``. |
| 80 | +- Body: |
| 81 | +``` |
| 82 | +{ |
| 83 | + "username": "alex_smith", |
| 84 | + "address": "12 Main St" |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | + |
| 89 | +## CLI Commands |
| 90 | +### Customer Commands |
| 91 | +**1. List Menu** |
| 92 | +- Command: ``1`` |
| 93 | +- Description: Lists available pizzas on the menu. |
| 94 | + |
| 95 | +**2. Register User** |
| 96 | +- Command: ``2`` |
| 97 | +- Description: Registers a new user. |
| 98 | +- Prompts for ``username`` and ``address``. |
| 99 | + |
| 100 | +**3. Place Order** |
| 101 | +- Command: ``3`` |
| 102 | +- Description: Places a pizza order. |
| 103 | +- Prompts for ``pizza``, ``username``, and optional ``address``. |
| 104 | + |
| 105 | +**4. Check Order Status** |
| 106 | +- Command: ``4`` |
| 107 | +- Description: Checks the status of an order by ``order_id``. |
| 108 | + |
| 109 | +**5. Cancel Order** |
| 110 | +- Command: ``5`` |
| 111 | +- Description: Cancels an order if not ``ready_to_be_delivered``. |
| 112 | + |
| 113 | +### Admin Commands |
| 114 | +**1. Add Pizza to Menu** |
| 115 | +- Command: ``6`` |
| 116 | +- Description: Adds a new pizza to the menu. |
| 117 | +- Prompts for ``admin token`` and ``pizza name``. |
| 118 | + |
| 119 | +**2. Delete Pizza from Menu** |
| 120 | +- Command: ``7`` |
| 121 | +- Description: Deletes a pizza from the menu by ``pizza_id``. |
| 122 | +- Prompts for ``admin token`` and ``pizza_id``. |
| 123 | + |
| 124 | +**3. Cancel Any Order (Admin Only)** |
| 125 | +- Command: ``8`` |
| 126 | +- Description: Cancels any order regardless of status. |
| 127 | +- Prompts for ``admin token`` and ``order_id``. |
| 128 | + |
| 129 | +**4. Exit** |
| 130 | +- Command: ``0`` |
| 131 | +- Description: Exits the CLI application. |
| 132 | + |
| 133 | +## Error Handling |
| 134 | +The application includes extensive error handling for various scenarios, ensuring meaningful feedback for users and admins. |
| 135 | + |
| 136 | +### Common Error Responses |
| 137 | +- **Missing Fields:** Required fields like username, address, or pizza are missing. |
| 138 | +- **Invalid IDs:** IDs must be positive integers. |
| 139 | +- **Unauthorized Access:** Missing or incorrect admin token when accessing admin endpoints. |
| 140 | +- **Duplicate Entry:** Usernames and pizza names cannot be duplicated. |
| 141 | +- **Exceeded Character Limits:** Limits on username, address, and pizza name lengths. |
| 142 | +- **Unregistered User Without Address:** Unregistered users must provide an address when placing an order. |
| 143 | + |
| 144 | +### Error Messages (Examples) |
| 145 | +- ``{"error": "Username 'john_doe' already exists."}:`` Triggered when trying to register a duplicate username. |
| 146 | +- ``{"error": "Pizza 'Hawaiian' already exists in the menu."}:`` Triggered when adding a pizza that already exists. |
| 147 | +- ``{"error": "Order ID must be a positive integer."}:`` Triggered when an invalid ``order_id is`` provided. |
| 148 | +- ``{"error": "Unauthorized. Admin token required."}:`` Triggered when the ``Authorization`` header is missing or incorrect. |
| 149 | +- ``{"error": "Cannot cancel an order that is 'ready_to_be_delivered'."}:`` Triggered when a non-admin user attempts to cancel an order that is already out for delivery. |
| 150 | + |
| 151 | + |
| 152 | +## Example Usage |
| 153 | +**1. List Menu:** |
| 154 | +``` |
| 155 | +python3 cli.py |
| 156 | +[CLI]: 1 |
| 157 | +Expected Output: ["Margherita", "Pepperoni"] |
| 158 | +``` |
| 159 | + |
| 160 | +**2. Register User:** |
| 161 | +``` |
| 162 | +[CLI]: 2 |
| 163 | +Enter username: alex_smith |
| 164 | +Enter address: 12 Main St |
| 165 | +Expected Output: User registered successfully. |
| 166 | +``` |
| 167 | +**3. Admin Add Pizza:** |
| 168 | +``` |
| 169 | +[CLI]: 6 |
| 170 | +Enter admin token: secureAdmin12345 |
| 171 | +Enter pizza name: Hawaiian |
| 172 | +Expected Output: Pizza added. |
| 173 | +``` |
| 174 | + |
| 175 | +**4. Place Order:** |
| 176 | +``` |
| 177 | +[CLI]: 3 |
| 178 | +Enter pizza: Margherita |
| 179 | +Enter username: alex_smith |
| 180 | +Expected Output: Order placed successfully! Order ID: 1 |
| 181 | +``` |
| 182 | + |
| 183 | +## Conclusion |
| 184 | +This project is a fully functional Pizza Ordering System API and CLI, featuring comprehensive customer and admin functionality, secure token management, and error handling for a smooth user experience. The detailed CLI and RESTful API allow for flexible user interactions, and the system is designed with scalability and security in mind. |
| 185 | + |
| 186 | + |
| 187 | + |
| 188 | + |
| 189 | + |
| 190 | + |
| 191 | + |
| 192 | + |
| 193 | + |
| 194 | + |
| 195 | + |
| 196 | + |
| 197 | + |
| 198 | + |
| 199 | + |
| 200 | + |
| 201 | + |
| 202 | + |
| 203 | + |
| 204 | + |
| 205 | + |
| 206 | + |
| 207 | + |
| 208 | + |
| 209 | + |
| 210 | + |
| 211 | + |
| 212 | + |
| 213 | + |
| 214 | + |
| 215 | + |
| 216 | + |
| 217 | + |
| 218 | + |
| 219 | + |
| 220 | + |
| 221 | + |
| 222 | + |
0 commit comments