-
Notifications
You must be signed in to change notification settings - Fork 200
Expand file tree
/
Copy pathserver.py
More file actions
83 lines (72 loc) · 2.53 KB
/
server.py
File metadata and controls
83 lines (72 loc) · 2.53 KB
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
from flask import Flask, request, jsonify , render_template
from flask_cors import CORS
from sql_connection import get_sql_connection
import json
import products_dao
import orders_dao
import uom_dao
app = Flask(__name__)
CORS(app)
connection = get_sql_connection()
@app.route('/getUOM', methods=['GET'])
def get_uom():
try:
response = uom_dao.get_uoms(connection)
return jsonify(response)
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/getProducts', methods=['GET'])
def get_products():
try:
response = products_dao.get_all_products(connection)
return jsonify(response)
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/insertProduct', methods=['POST'])
def insert_product():
try:
request_payload = json.loads(request.form['data'])
product_id = products_dao.insert_new_product(connection, request_payload)
return jsonify({'product_id': product_id})
except Exception as e:
return jsonify({'error': str(e)}), 500
# @app.route('/getAllOrders', methods=['GET'])
# def get_all_orders():
# try:
# response = orders_dao.get_all_orders(connection)
# return jsonify(response)
# except Exception as e:
# return jsonify({'error': str(e)}), 500
@app.route('/getAllOrders', methods=['GET'])
def get_all_orders():
try:
cursor = connection.cursor()
cursor.execute("SELECT * FROM orders") # Example query
orders = cursor.fetchall()
return jsonify(orders)
except Exception as e:
print(f"Error: {e}") # Log the error for debugging
return jsonify({'error': str(e)}), 500
finally:
cursor.close()
@app.route('/insertOrder', methods=['POST'])
def insert_order():
try:
request_payload = json.loads(request.form['data'])
order_id = orders_dao.insert_order(connection, request_payload)
return jsonify({'order_id': order_id})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/deleteProduct', methods=['POST'])
def delete_product():
try:
return_id = products_dao.delete_product(connection, request.form['product_id'])
return jsonify({'product_id': return_id})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/ui/manage-product')
def manage_product():
return render_template('manage-product.html')
if __name__ == "__main__":
print("Starting Python Flask Server For Grocery Store Management System")
app.run(port=5000)