Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Python backend solutions #5

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions solutions/backend/orders_dao.py
Original file line number Diff line number Diff line change
@@ -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))
25 changes: 25 additions & 0 deletions solutions/backend/products_dao.py
Original file line number Diff line number Diff line change
@@ -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
}))
50 changes: 50 additions & 0 deletions solutions/backend/server.py
Original file line number Diff line number Diff line change
@@ -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)
14 changes: 14 additions & 0 deletions solutions/backend/sql_connection.py
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions solutions/backend/uom_dao.py
Original file line number Diff line number Diff line change
@@ -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'
}))