-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
54 lines (40 loc) · 1.53 KB
/
app.py
File metadata and controls
54 lines (40 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from flask import Flask, jsonify, render_template, request
import sqlite3
app = Flask(__name__)
def get_db_connection():
"""Create a new database connection"""
conn = sqlite3.connect('transactions.db')
conn.row_factory = sqlite3.Row # Allows fetching columns by name
return conn
def fetch_transactions(limit=50, offset=0, sort_by='transaction_date', order='DESC'):
"""Fetch transactions with sorting and pagination"""
conn = get_db_connection()
cursor = conn.cursor()
query = f"""
SELECT transaction_date, merchant, category, amount, transaction_type
FROM transactions
ORDER BY {sort_by} {order}
LIMIT ? OFFSET ?
"""
cursor.execute(query, (limit, offset))
transactions = cursor.fetchall()
conn.close()
return [dict(t) for t in transactions]
@app.route("/")
def index():
return render_template("index.html")
@app.route("/get_transactions", methods=["GET"])
def get_transactions():
try:
limit = int(request.args.get("limit", 50)) # Default 50 records
offset = int(request.args.get("offset", 0)) # Pagination offset
sort_by = request.args.get("sort_by", "transaction_date")
order = request.args.get("order", "DESC").upper()
if order not in ["ASC", "DESC"]:
order = "DESC"
transactions = fetch_transactions(limit, offset, sort_by, order)
return jsonify(transactions)
except Exception as e:
return jsonify({"error": str(e)}), 500
if __name__ == "__main__":
app.run(debug=True)