| import psycopg2 | |
| import os | |
| def init_connection(): | |
| return psycopg2.connect(os.environ["DB_CONNECT"]) | |
| def save_history(conn, user_id, timestamp, symptoms, result): | |
| with conn.cursor() as cur: | |
| cur.execute(""" | |
| INSERT INTO mb_history (user_id, timestamp, symptoms, result) | |
| VALUES (%s, %s, %s, %s) | |
| """, (user_id, timestamp, symptoms, result)) | |
| conn.commit() | |
| def get_history_for_user(conn, user_id): | |
| with conn.cursor() as cur: | |
| cur.execute(""" | |
| SELECT timestamp, symptoms, result FROM mb_history | |
| WHERE user_id = %s ORDER BY timestamp DESC | |
| """, (user_id,)) | |
| rows = cur.fetchall() | |
| return [{"timestamp": r[0], "symptoms": r[1], "result": r[2]} for r in rows] | |