-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
237 lines (189 loc) · 7.82 KB
/
api.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
from flask import Flask, request, jsonify
import mysql.connector
from config import MYSQL_CONFIG
from flask_login import current_user
from flask import Flask, session
import time
app = Flask(__name__)
# Configure MySQL database connection
db = mysql.connector.connect(**MYSQL_CONFIG)
@app.route('/insert_food', methods=['GET'])
def insert_record():
# data = request.get_json()
# title = data.get('title')
name = request.args.get('name', type=str)
description = request.args.get('description', type=str)
price = request.args.get('price', type=float)
# description = data.get('description')
mycursor = db.cursor()
sql = "INSERT INTO FOOD_TABLE (NAME,PRICE, DESCRIPTION) VALUES (%s, %s,%s)"
val = (name,price,description)
mycursor.execute(sql, val)
db.commit()
return "Record inserted."
@app.route('/retrieve_food', methods=['GET'])
def retrieve_data():
try:
mycursor = db.cursor()
sql = "SELECT * FROM FOOD_TABLE"
mycursor.execute(sql)
data = mycursor.fetchall()
result = [{'id': row[0], 'name': row[1], 'price':row[2] ,'description': row[3]} for row in data]
mycursor.close()
return jsonify(result)
except mysql.connector.Error as error:
return jsonify({'error': f"Database error: {error}"}), 500
@app.route('/delete_food/<int:id>', methods=['GET'])
def delete_record(id):
try:
mycursor = db.cursor()
sql = "DELETE FROM FOOD_TABLE WHERE ID = %s"
val = (id,)
mycursor.execute(sql, val)
db.commit()
if mycursor.rowcount > 0:
return jsonify({'message': f'Record with ID {id} deleted successfully'})
else:
return jsonify({'message': f'Record with ID {id} not found'}), 404
except mysql.connector.Error as error:
return jsonify({'error': f"Database error: {error}"}), 500
@app.route('/update_food/<int:id>', methods=['GET'])
def update_record(id):
try:
# Get the updated title and description from the request
title = request.args.get('name', type=str)
description = request.args.get('description', type=str)
price = request.args.get('price', type=float)
mycursor = db.cursor()
sql = "UPDATE FOOD_TABLE SET NAME = %s, DESCRIPTION = %s, PRICE = %s WHERE ID = %s"
val = (title, description,price,id)
mycursor.execute(sql, val)
db.commit()
if mycursor.rowcount > 0:
return jsonify({'message': f'Record with ID {id} updated successfully'})
else:
return jsonify({'message': f'Record with ID {id} not found'}), 404
except mysql.connector.Error as error:
return jsonify({'error': f"Database error: {error}"}), 500
total_price = 0.0
@app.route('/add_to_cart', methods=['GET'])
def add_to_cart():
global total_price
try:
item_price = float(request.args.get('price'))
if item_price is not None:
total_price += item_price
return jsonify({'message': 'Item added to cart successfully'})
else:
return jsonify({'error': 'Invalid item data'}), 400
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/store_user_data', methods=['GET'])
def insert_user():
# data = request.get_json()
# title = data.get('title')
username = request.args.get('username', type=str)
email = request.args.get('email', type=str)
# description = data.get('description')
mycursor = db.cursor()
sql = "INSERT INTO users (username, email) VALUES (%s, %s)"
val = (username,email)
mycursor.execute(sql, val)
db.commit()
return "User inserted."
@app.route('/retrieve_username', methods=['GET'])
def retrieve_username():
try:
email = request.args.get('email') # Get the email from the query parameters
if email is not None:
mycursor = db.cursor()
sql = "SELECT username FROM users WHERE email = %s" # Modify the SQL query to filter by email
mycursor.execute(sql, (email,))
row = mycursor.fetchone()
if row is not None:
username = row[0]
# Remove double quotation marks from the username
username = username.replace('"', '')
mycursor.close()
return jsonify(username)
else:
return jsonify({'error': 'User not found for the provided email'}), 404
else:
return jsonify({'error': 'Missing email parameter'}), 400
except mysql.connector.Error as error:
return jsonify({'error': f"Database error: {error}"}), 500
@app.route('/insert_bucket', methods=['GET'])
def insert_bucket():
Username = request.args.get('Username', type=str,default=None)
FoodName = request.args.get('FoodName', type=str)
Price = request.args.get('Price', type=float)
Quantity=request.args.get('Quantity', type=int,default=None)
SubTotal = request.args.get('SubTotal', type=float,default=None)
Status = "Pending"
mycursor = db.cursor()
sql = "INSERT INTO Bucket (Username,FoodName,Price,Quantity,SubTotal,Status) VALUES (%s, %s,%s,%s,%s,%s)"
val = (Username,FoodName,Price,Quantity,SubTotal,Status)
mycursor.execute(sql, val)
db.commit()
return "Record inserted."
@app.route('/retrieve_bucket', methods=['GET'])
def retrieve_bucket():
try:
username = request.args.get('Username')
mycursor = db.cursor()
sql = "SELECT * FROM Bucket WHERE Status = 'Pending' AND Username = %s"
mycursor.execute(sql, (username,))
data = mycursor.fetchall()
result = [{'OrderID': row[0], 'Username': row[1], 'FoodName':row[2] ,'Price': row[3],'Quantity': row[4],'SubTotal': row[5],'Status': row[6]} for row in data]
mycursor.close()
return jsonify(result)
except mysql.connector.Error as error:
return jsonify({'error': f"Database error: {error}"}), 500
@app.route('/update_bucket/<int:OrderID>', methods=['GET'])
def update_bucket(OrderID):
try:
# Get the updated title and description from the request
Quantity = request.args.get('Quantity', type=str)
SubTotal = request.args.get('SubTotal', type=float)
mycursor = db.cursor()
sql = "UPDATE Bucket SET Quantity = %s, SubTotal = %s WHERE OrderID = %s"
val = (Quantity, SubTotal,OrderID)
mycursor.execute(sql, val)
db.commit()
if mycursor.rowcount > 0:
return jsonify({'message': f'Record with ID {OrderID} updated successfully'})
else:
return jsonify({'message': f'Record with ID {OrderID} not found'}), 404
except mysql.connector.Error as error:
return jsonify({'error': f"Database error: {error}"}), 500
@app.route('/get_total_price2', methods=['GET'])
def get_total_price2():
try:
mycursor = db.cursor()
sql = "SELECT SUM(SubTotal) AS total_price FROM Bucket"
mycursor.execute(sql)
# Fetch the result
result = mycursor.fetchone()
total_price = result[0] if result else 0.0
mycursor.close()
return jsonify({"total_price": total_price})
except Exception as e:
return jsonify({"error": str(e)})
@app.route('/delete_bucket/<int:id>', methods=['GET'])
def delete_bucket(id):
try:
mycursor = db.cursor()
sql = "DELETE FROM Bucket WHERE OrderID = %s"
val = (id,)
mycursor.execute(sql, val)
db.commit()
if mycursor.rowcount > 0:
return jsonify({'message': f'Record with ID {id} deleted successfully'})
else:
return jsonify({'message': f'Record with ID {id} not found'}), 404
except mysql.connector.Error as error:
return jsonify({'error': f"Database error: {error}"}), 500
if __name__ == '__main__':
app.run(host='0.0.0.0', port=9090)
while True:
time.sleep(2)