diff --git a/Backend/main.py b/Backend/main.py index 6aa47a8c9fa61c849bd4f1eac520d0fd565dc8b5..931959c0378f11feeb78bfe8bb578567ec437160 100644 --- a/Backend/main.py +++ b/Backend/main.py @@ -1,7 +1,7 @@ from utils.application import app from routes.category import get_category from routes.home import get_home -from routes.order import post_create_order, pay_order +from routes.order import post_create_order, pay_order, get_all_orders from routes.login import post_login, post_logout, post_register from routes.product import get_product_by_id, get_product_all, get_products_by_search @@ -23,6 +23,10 @@ def category(category_name=None): def create_order(): return post_create_order() +@app.route('/order/', methods=['GET']) +def get_order(): + return get_all_orders() + # Route for paying an order @app.route('/order/payment/', methods=['POST']) def pay(): diff --git a/Backend/routes/order.py b/Backend/routes/order.py index d23eaf612aed5b1fcac8162696ded713f6c0eda9..1a17f4c507062eedfd7d21dec74e597adbfdd481 100644 --- a/Backend/routes/order.py +++ b/Backend/routes/order.py @@ -67,7 +67,7 @@ def post_create_order(): cur.execute('''SELECT order_id FROM user_order WHERE user_id = %s ORDER BY order_id DESC LIMIT 1''', (userId,)) order_id = cur.fetchall()[0][0] cur.close() - + # Create order_item for each product for i in range(len(products)): cur = mysql.connection.cursor() @@ -111,7 +111,7 @@ def pay_order(): # Get the order status to make sure the order is not already paid cur = mysql.connection.cursor() - cur.execute('''SELECT user_order.status, user_order.total_amount, user_order.user_id + cur.execute('''SELECT user_order.status, user_order.total_amount, user_order.user_id FROM user_order WHERE order_id = %s''', (order_id,)) order_info = cur.fetchall() @@ -127,21 +127,21 @@ def pay_order(): # Check email is the same as logged in email if userId != str(user_id): return jsonify({"message": "This order is not for the logged in user"}), 400 - + # Check order is not already paid if status != "Pending": return jsonify({"message": "Order is already paid"}), 400 - + # Make sure correct payment method if payment_method != "Vipps": return jsonify({"message": "Invalid payment method"}), 400 - + # Pay the order payment = vipps(amount, order_id) if not payment: return jsonify({"message": "Payment failed"}), 400 - + # Update the status of the order to paid cur = mysql.connection.cursor() cur.execute('''UPDATE user_order SET status = 'Paid' WHERE user_order.order_id = %s''', (order_id,)) @@ -152,4 +152,32 @@ def pay_order(): # Dummy payment method def vipps(amount, order_id): - return True \ No newline at end of file + return True + +# Get all orders for a user +def get_all_orders(): + # Get user from logged in cookie + userId = request.cookies.get('logged_in') + + # Check if user is logged in + if userId is None: + return jsonify({"message": "You are not logged in"}), 401 + + # Get orders from database + cur = mysql.connection.cursor() + cur.execute('''SELECT user_order.order_id, user_order.order_date, user_order.total_amount, user_order.status FROM user_order WHERE user_id = %s''', (userId,)) + ordersDb = cur.fetchall() + cur.close() + + # Format orders + orders = [] + for order in ordersDb: + orders.append({ + "order_id": order[0], + "order_date": order[1].isoformat(), + "total_amount": order[2], + "status": order[3] + }) + + # Return orders + return jsonify(orders), 200 \ No newline at end of file diff --git a/README.md b/README.md index f952c7dce081008f140622d1089db87a9342864a..2d74d3aa682490a6c2f578d349e23f68436d4972 100644 --- a/README.md +++ b/README.md @@ -1,43 +1,77 @@ -# idatg2204-project +# Electromart Database Implementation -## ElectroMart Website Database Implementation +This documentation will provide details about a simple backend implementation which is fully integrated with a relational database created in MariaDB. -Welcome to the ElectroMart Website Database Documentation! -- This documentation will provide details about a simple backend implementation which is fully integrated with a relational database created in MariaDB. +## Electromart +ElectroMart is an electronics store that sells a large variety of different electronic products.Product categories include phones, tablets, laptops, cameras, home +applications + +# Deployment +The service can be hosted locally on your computer after running the code included in the `Backend` directory. The service can be accessed at the URL: `http://localhost:8080` + +## Services being provided +## Homepage +http://localhost:8080/ Method = ["GET"] + + +## Category +http://localhost:8080/category Method = ["GET"] + +http://localhost:8080/category/<string:category_name> Method = ["GET"] + + +## User +http://localhost:8080/register Method = ["POST"] + +Example JSON data +```JSON +{ + "firstname": "Ola", + "lastname": "Nordmann", + "address": "Tollbugata 2", + "email": "ola.nordmann@example.com", + "password": "Hemmelig" +} ``` -What is ElectroMart? - -- ElectroMart is an electronics store that sells a large variety of different electronic products. -- Example of some product categories that can be expected on the ElectroMart website: - - Phones - - Tablets - - laptops - - Cameras - - Home Appliances - - Various Electronic Gadgets - - etc., -- ElectroMart aims to provide an efficient website experience for its users with focus on user friendliness + +http://localhost:8080/login Method = ["POST"] + +Example JSON data +```JSON +{ + "email": "ola.nordmann@example.com", + "password": "Hemmelig" +} +``` +http://localhost:8080/logout Method = ["POST"] + +## Product +http://localhost:8080/product Method = ["GET"] + +http://localhost:8080/product/search/<string:search> Method = ["GET"] + +http://localhost:8080/product/<int:product_id> Method = ["GET"] + +## Order +http://localhost:8080/order/ Method = ["GET"] + +http://localhost:8080/order/ Method = ["POST"] + +```JSON +{ + "products": [ + {"product_id": 1, "quantity": 2}, + {"product_id": 2, "quantity": 1}, + {"product_id": 3, "quantity": 3} + ] +} ``` -## Deployment -- This service can be hosted locally on a computer after running. -- After running the service on your computer you can access it through localhost with port: 8080 -- http://localhost:8080/ - -## Services being provided -- Homepage - - http://localhost:8080/ Method = ["GET"] -- category - - http://localhost:8080/category Method = ["GET"] - - http://localhost:8080/category/<string:category_name Method = ["GET"] -- Login - - http://localhost:8080/login Method = ["POST"] -- Logout - - http://localhost:8080/logout Method = ["POST"] -- Register - - http://localhost:8080/Register Method = ["POST"] -- product - - http://localhost:8080/Product Method = ["GET"] - - http://localhost:8080/product/search/<string:search> Method = ["GET"] - - http://localhost:8080/product/<int:product_id> Method = ["GET"] +http://localhost:8080/order/payment Method = ["POST"] +```JSON +{ + "order_id": 1, + "payment_method": "Vipps" +} +``` \ No newline at end of file