-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.py
executable file
·131 lines (114 loc) · 4.18 KB
/
cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import requests
import json
BASE_URL = "http://127.0.0.1:8080" # Base URL of the server
def list_menu():
response = requests.get(f"{BASE_URL}/menu")
if response.status_code == 200:
print("Menu:", response.json())
else:
print("Error:", response.json().get("error"))
def register_user():
username = input("Enter your username: ")
address = input("Enter your address: ")
payload = {"username": username, "address": address}
response = requests.post(f"{BASE_URL}/register", json=payload)
if response.status_code == 200:
print(response.json()["message"])
else:
print("Error:", response.json().get("error"))
def place_order():
username = input("Enter your username (leave blank if unregistered): ")
pizza = input("Enter the pizza name: ")
address = None
if not username: # If the user is unregistered, ask for an address
address = input("Enter your address: ")
payload = {"pizza": pizza}
if username:
payload["username"] = username
if address:
payload["address"] = address
response = requests.post(f"{BASE_URL}/order", json=payload)
if response.status_code == 200:
print("Order placed successfully! Order ID:", response.json()["order_id"])
else:
print("Error:", response.json().get("error"))
def check_order_status():
order_id = input("Enter your order ID: ")
response = requests.get(f"{BASE_URL}/order/{order_id}")
if response.status_code == 200:
print("Order Status:", response.json())
else:
print("Error:", response.json().get("error"))
def cancel_order():
order_id = input("Enter the order ID to cancel: ")
response = requests.delete(f"{BASE_URL}/order/{order_id}")
if response.status_code == 200:
print(response.json()["message"])
else:
print("Error:", response.json().get("error"))
# Admin-specific functions
def admin_add_pizza():
token = input("Enter admin token: ")
pizza = input("Enter the pizza name to add: ")
payload = {"pizza": pizza}
headers = {"Authorization": token}
response = requests.post(f"{BASE_URL}/menu", json=payload, headers=headers)
if response.status_code == 200:
print(response.json()["message"])
else:
print("Error:", response.json().get("error"))
def admin_delete_pizza():
token = input("Enter admin token: ")
pizza_id = input("Enter the pizza ID to delete: ")
headers = {"Authorization": token}
response = requests.delete(f"{BASE_URL}/menu/{pizza_id}", headers=headers)
if response.status_code == 200:
print(response.json()["message"])
else:
print("Error:", response.json().get("error"))
def admin_cancel_any_order():
token = input("Enter admin token: ")
order_id = input("Enter the order ID to cancel: ")
headers = {"Authorization": token}
response = requests.delete(f"{BASE_URL}/admin/order/{order_id}", headers=headers)
if response.status_code == 200:
print(response.json()["message"])
else:
print("Error:", response.json().get("error"))
def main():
while True:
print("\nWelcome to the Pizza Ordering App!")
print("Please select an option:")
print("1. List Menu")
print("2. Register")
print("3. Place Order")
print("4. Check Order Status")
print("5. Cancel Order")
print("6. Admin: Add Pizza to Menu")
print("7. Admin: Delete Pizza from Menu")
print("8. Admin: Cancel Any Order")
print("0. Exit")
choice = input("Enter your choice: ")
if choice == "1":
list_menu()
elif choice == "2":
register_user()
elif choice == "3":
place_order()
elif choice == "4":
check_order_status()
elif choice == "5":
cancel_order()
elif choice == "6":
admin_add_pizza()
elif choice == "7":
admin_delete_pizza()
elif choice == "8":
admin_cancel_any_order()
elif choice == "0":
print("Exiting the application.")
break
else:
print("Invalid choice. Please try again.")
if __name__ == "__main__":
main()