diff --git a/solutions/backend/orders_dao.py b/solutions/backend/orders_dao.py new file mode 100644 index 0000000..a52b4c0 --- /dev/null +++ b/solutions/backend/orders_dao.py @@ -0,0 +1,31 @@ +from datetime import datetime +from sql_connection import get_sql_connection + + +def get_order_details(connection, order_id): + cursor = connection.cursor() + query = ("select orders.customer_name as customer, products.name as product, quantity, total_price " + "from order_details " + "inner join orders on order_details.order_id = orders.order_id " + "inner join products on order_details.product_id = products.product_id " + "where order_details.order_id = " + str(order_id)) + + cursor.execute(query) + + response = [] + + for (customer, product, quantity, total_price) in cursor: + response.append({ + 'customer': customer, + 'product': product, + 'quantity': quantity, + 'total_price': total_price + } + + ) + return response + + +if __name__ == '__main__': + connection = get_sql_connection() + print(get_order_details(connection, 27)) diff --git a/solutions/backend/products_dao.py b/solutions/backend/products_dao.py new file mode 100644 index 0000000..8fcd8e7 --- /dev/null +++ b/solutions/backend/products_dao.py @@ -0,0 +1,25 @@ +from sql_connection import get_sql_connection + + +def edit_product(connection, product): + cursor = connection.cursor() + query = ("UPDATE products " + "set name=%s, uom_id=%s, price_per_unit=%s" + " WHERE product_id=%s;") + data = (product['name'], product['uom_id'], product['price_per_unit'], product['product_id']) + + cursor.execute(query, data) + connection.commit() + + return product['product_id'] + + +if __name__ == '__main__': + connection = get_sql_connection() + + print(edit_product(connection, { + 'product_id': 1, + 'name': 'potatoes', + 'uom_id': 1, + 'price_per_unit': 10 + })) diff --git a/solutions/backend/server.py b/solutions/backend/server.py new file mode 100644 index 0000000..1816cb3 --- /dev/null +++ b/solutions/backend/server.py @@ -0,0 +1,50 @@ +from flask import Flask, request, jsonify +from sql_connection import get_sql_connection +import mysql.connector +import json + +import products_dao +import orders_dao +import uom_dao + +app = Flask(__name__) + +connection = get_sql_connection() + + +# Passes ID and Edits details of a product +@app.route('/editProduct', methods=['POST', 'GET']) +def edit_product(): + request_payload = json.loads(request.form['data']) + product_id = products_dao.edit_product(connection, request_payload) + response = jsonify({ + 'product_id': product_id + }) + response.headers.add('Access-Control-Allow-Origin', '*') + return response + + +# Inserts new UOM +@app.route('/insertUOM', methods=['GET', 'POST']) +def insert_uom(): + request_payload = json.loads(request.form['data']) + uom_id = uom_dao.insert_new_uom(connection,request_payload) + response = jsonify({ + 'uom_id': uom_id + }) + response.headers.add('Access-Control-Allow-Origin', '*') + return response + + +# Order details (customer_name, product, quantity, total_price) +# of a particular order is fetched from the Database +@app.route('/getOrderDetails', methods=['GET']) +def get_order_details(): + response = orders_dao.get_order_details(connection, request.form['order_id']) + response = jsonify(response) + response.headers.add('Access-Control-Allow-Origin', '*') + return response + +if __name__ == "__main__": + print("Starting Python Flask Server For Grocery Store Management System") + app.run(port=5000) \ No newline at end of file diff --git a/solutions/backend/sql_connection.py b/solutions/backend/sql_connection.py new file mode 100644 index 0000000..dc86690 --- /dev/null +++ b/solutions/backend/sql_connection.py @@ -0,0 +1,14 @@ +import datetime +import mysql.connector + +__cnx = None + + +def get_sql_connection(): + print("Opening mysql connection") + global __cnx + + if __cnx is None: + __cnx = mysql.connector.connect(user='root', password='root', database='grocery_store') + + return __cnx diff --git a/solutions/backend/uom_dao.py b/solutions/backend/uom_dao.py new file mode 100644 index 0000000..b6f2fb2 --- /dev/null +++ b/solutions/backend/uom_dao.py @@ -0,0 +1,17 @@ +from sql_connection import get_sql_connection +connection = get_sql_connection() + + +def insert_new_uom(connection, uom): + cursor = connection.cursor() + query = f"INSERT INTO uom (uom_name) VALUES ('{uom['name']}');" + cursor.execute(query) + connection.commit() + return cursor.lastrowid + + +if __name__ == '__main__': + + print(insert_new_uom(connection, { + 'name': 'cubic meter' + }))