Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, jsonify, request
|
| 2 |
+
from flask_cors import CORS
|
| 3 |
+
import sqlite3
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
app = Flask(__name__)
|
| 7 |
+
CORS(app)
|
| 8 |
+
|
| 9 |
+
# Database setup
|
| 10 |
+
DATABASE = 'restaurant.db'
|
| 11 |
+
|
| 12 |
+
def init_db():
|
| 13 |
+
with sqlite3.connect(DATABASE) as conn:
|
| 14 |
+
cursor = conn.cursor()
|
| 15 |
+
# Create tables
|
| 16 |
+
cursor.execute('''
|
| 17 |
+
CREATE TABLE IF NOT EXISTS Categories (
|
| 18 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 19 |
+
name TEXT NOT NULL,
|
| 20 |
+
image TEXT NOT NULL
|
| 21 |
+
)
|
| 22 |
+
''')
|
| 23 |
+
cursor.execute('''
|
| 24 |
+
CREATE TABLE IF NOT EXISTS Items (
|
| 25 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 26 |
+
name TEXT NOT NULL,
|
| 27 |
+
description TEXT,
|
| 28 |
+
price REAL NOT NULL,
|
| 29 |
+
category_id INTEGER NOT NULL,
|
| 30 |
+
image TEXT NOT NULL,
|
| 31 |
+
FOREIGN KEY (category_id) REFERENCES Categories(id)
|
| 32 |
+
)
|
| 33 |
+
''')
|
| 34 |
+
# Insert sample data
|
| 35 |
+
cursor.execute("INSERT OR IGNORE INTO Categories (id, name, image) VALUES (1, 'Breakfast', 'breakfast.png')")
|
| 36 |
+
cursor.execute("INSERT OR IGNORE INTO Categories (id, name, image) VALUES (2, 'Ramen', 'ramen.png')")
|
| 37 |
+
cursor.execute("INSERT OR IGNORE INTO Items (id, name, description, price, category_id, image) VALUES "
|
| 38 |
+
"(1, 'Pancakes', 'Fluffy pancakes with syrup.', 5.99, 1, 'pancakes.png')")
|
| 39 |
+
cursor.execute("INSERT OR IGNORE INTO Items (id, name, description, price, category_id, image) VALUES "
|
| 40 |
+
"(2, 'Classic Ramen', 'Delicious ramen with pork and egg.', 12.99, 2, 'ramen.png')")
|
| 41 |
+
conn.commit()
|
| 42 |
+
|
| 43 |
+
# Initialize database
|
| 44 |
+
if not os.path.exists(DATABASE):
|
| 45 |
+
init_db()
|
| 46 |
+
|
| 47 |
+
@app.route('/categories', methods=['GET'])
|
| 48 |
+
def get_categories():
|
| 49 |
+
with sqlite3.connect(DATABASE) as conn:
|
| 50 |
+
cursor = conn.cursor()
|
| 51 |
+
cursor.execute("SELECT * FROM Categories")
|
| 52 |
+
categories = [{"id": row[0], "name": row[1], "image": row[2]} for row in cursor.fetchall()]
|
| 53 |
+
return jsonify(categories)
|
| 54 |
+
|
| 55 |
+
@app.route('/items', methods=['GET'])
|
| 56 |
+
def get_items():
|
| 57 |
+
category_id = request.args.get('category_id')
|
| 58 |
+
with sqlite3.connect(DATABASE) as conn:
|
| 59 |
+
cursor = conn.cursor()
|
| 60 |
+
cursor.execute("SELECT * FROM Items WHERE category_id = ?", (category_id,))
|
| 61 |
+
items = [{"id": row[0], "name": row[1], "description": row[2], "price": row[3], "image": row[5]} for row in cursor.fetchall()]
|
| 62 |
+
return jsonify(items)
|
| 63 |
+
|
| 64 |
+
@app.route('/update-item', methods=['POST'])
|
| 65 |
+
def update_item():
|
| 66 |
+
data = request.json
|
| 67 |
+
item_id = data.get('id')
|
| 68 |
+
name = data.get('name')
|
| 69 |
+
description = data.get('description')
|
| 70 |
+
price = data.get('price')
|
| 71 |
+
category_id = data.get('category_id')
|
| 72 |
+
image = data.get('image')
|
| 73 |
+
|
| 74 |
+
with sqlite3.connect(DATABASE) as conn:
|
| 75 |
+
cursor = conn.cursor()
|
| 76 |
+
cursor.execute("UPDATE Items SET name = ?, description = ?, price = ?, category_id = ?, image = ? WHERE id = ?",
|
| 77 |
+
(name, description, price, category_id, image, item_id))
|
| 78 |
+
conn.commit()
|
| 79 |
+
return jsonify({"message": "Item updated successfully!"})
|
| 80 |
+
|
| 81 |
+
@app.route('/deals', methods=['GET'])
|
| 82 |
+
def get_deals():
|
| 83 |
+
with sqlite3.connect(DATABASE) as conn:
|
| 84 |
+
cursor = conn.cursor()
|
| 85 |
+
cursor.execute("SELECT * FROM Items LIMIT 3") # Example: Fetch 3 items as best deals
|
| 86 |
+
deals = [{"id": row[0], "name": row[1], "description": row[2], "price": row[3], "image": row[5]} for row in cursor.fetchall()]
|
| 87 |
+
return jsonify(deals)
|
| 88 |
+
|
| 89 |
+
if __name__ == '__main__':
|
| 90 |
+
app.run(debug=True)
|