Skip to content
Snippets Groups Projects
Commit a5b92579 authored by Steffen Martinsen's avatar Steffen Martinsen
Browse files

configured routing to seperate files

parent 423f90e4
No related branches found
No related tags found
No related merge requests found
from flask import Flask, jsonify
from flask_mysqldb import MySQL
from routes.category import get_category
from routes.order import get_order
from routes.home import get_home
from routes.cart import get_cart
from routes.login import post_login
# Initialize APP
app = Flask(__name__)
......@@ -13,43 +18,24 @@ mysql = MySQL(app)
# Routing
@app.route('/', methods=['GET'])
def home():
return "Welcome to home"
return get_home()
@app.route('/order/<int:order_id>', methods=['GET'])
def order(order_id):
return f"Order ID: {order_id}"
return get_order(order_id)
@app.route('/category', methods=['GET'])
@app.route('/category/<string:category_name>', methods=['GET'])
def category(category_name=None):
if category_name is None:
cur = mysql.connection.cursor()
cur.execute('''SELECT category.name FROM category''')
data = cur.fetchall()
cur.close()
return jsonify(data)
else:
cur = mysql.connection.cursor()
cur.execute('''SELECT category.name FROM category WHERE name = %s''', (category_name,))
data = cur.fetchall()
cur.close()
return jsonify(data)
def category(category_name):
return get_category(category_name)
@app.route('/cart', methods=['GET'])
def cart():
return "Cart and your items"
return get_cart()
@app.route('/data', methods=['GET'])
def get_data():
cur = mysql.connection.cursor()
cur.execute('''SELECT * FROM brand''')
data = cur.fetchall()
cur.close()
return jsonify(data)
@app.route('/login', methods=['POST'])
def login():
return post_login()
if __name__ == '__main__':
app.run(debug=True, port=8080)
\ No newline at end of file
from main import mysql, jsonify
def get_cart():
cur = mysql.connection.cursor()
cur.execute('') # TODO Add SQL query here
data = cur.fetchall()
cur.close()
return jsonify(data)
\ No newline at end of file
from main import mysql, jsonify
def category(category_name=None):
if category_name is None:
cur = mysql.connection.cursor()
cur.execute('''SELECT category.name FROM category''')
data = cur.fetchall()
cur.close()
return jsonify(data)
else:
cur = mysql.connection.cursor()
cur.execute('''SELECT category.name FROM category WHERE name = %s''', (category_name,))
data = cur.fetchall()
cur.close()
return jsonify(data)
\ No newline at end of file
from main import mysql, jsonify
def get_home():
return "Welcome to home"
\ No newline at end of file
from main import mysql, jsonify
def post_login():
# TODO Add authentication logic here
return "Login successful"
from main import mysql, jsonify
def get_order(order_id):
if order_id is None:
return jsonify({"message": "Order not found"}), 404
else:
cur = mysql.connection.cursor()
cur.execute('') # TODO Add SQL query here
data = cur.fetchall()
cur.close()
return jsonify(data) # TODO Return the data as JSON
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment