Open darylnoumbo opened 5 months ago
f name == 'main': connection = get_sql_connection() print(get_all_orders(connection)) # print(get_order_details(connection,4)) # print(insert_order(connection, { # 'customer_name': 'dhaval', # 'total': '500', # 'datetime': datetime.now(), # 'order_details': [ # { # 'product_id': 1, # 'quantity': 2, # 'total_price': 50 # }, # { # 'product_id': 3, # 'quantity': 1, # 'total_price': 30 # } # ] # })) User Stories..docx
from datetime import datetimefrom sql_connection import get_sql_connectiondef insert_order(connection, order): cursor = connection.cursor() order_query = ("INSERT INTO orders " "(customer_name, total, datetime)" "VALUES (%s, %s, %s)") order_data = (order['customer_name'], order['grand_total'], datetime.now()) cursor.execute(order_query, order_data) order_id = cursor.lastrowid order_details_query = ("INSERT INTO order_details " "(order_id, product_id, quantity, total_price)" "VALUES (%s, %s, %s, %s)") order_details_data = [] for order_detail_record in order['order_details']: order_details_data.append([ order_id, int(order_detail_record['product_id']), float(order_detail_record['quantity']), float(order_detail_record['total_price']) ]) cursor.executemany(order_details_query, order_details_data) connection.commit() return order_iddef get_order_details(connection, order_id): cursor = connection.cursor() query = "SELECT * from order_details where order_id = %s" query = "SELECT order_details.order_id, order_details.quantity, order_details.total_price, "\ "products.name, products.price_per_unit FROM order_details LEFT JOIN products on " \