From 8ed79935733b10479d27312b1edce4d36e6add4b Mon Sep 17 00:00:00 2001 From: ben Date: Sun, 2 Aug 2026 20:35:09 +0200 Subject: [PATCH] update database.py and main.py --- backend/database.py | 77 +++++++++++++++++++++++++++++++++++++++++++++ backend/main.py | 32 +++++++++++++++++-- 2 files changed, 106 insertions(+), 3 deletions(-) diff --git a/backend/database.py b/backend/database.py index e69de29..c47173a 100644 --- a/backend/database.py +++ b/backend/database.py @@ -0,0 +1,77 @@ +import sqlite3 +from pathlib import Path + +DB_PATH = Path(__file__).parent.parent / "database" / "calendar.db" + + +def get_connection(): + """ + Maakt een verbinding met de SQLite database. + """ + conn = sqlite3.connect(DB_PATH) + + # Zorgt dat foreign keys werken in SQLite + conn.execute("PRAGMA foreign_keys = ON") + + # Resultaten kunnen als dictionary worden gebruikt + conn.row_factory = sqlite3.Row + + return conn + + +def execute(query, params=()): + """ + Voor INSERT, UPDATE, DELETE. + """ + with get_connection() as conn: + cursor = conn.cursor() + cursor.execute(query, params) + conn.commit() + + return cursor.lastrowid + + +def fetch_one(query, params=()): + """ + Haalt één resultaat op. + """ + with get_connection() as conn: + cursor = conn.cursor() + cursor.execute(query, params) + + return cursor.fetchone() + + +def fetch_all(query, params=()): + """ + Haalt meerdere resultaten op. + """ + with get_connection() as conn: + cursor = conn.cursor() + cursor.execute(query, params) + + return cursor.fetchall() + + +def test_connection(): + """ + Test of de database bereikbaar is. + """ + with get_connection() as conn: + cursor = conn.cursor() + cursor.execute("SELECT name FROM sqlite_master WHERE type='table';") + + return cursor.fetchall() + + +if __name__ == "__main__": + print("Database:", DB_PATH) + + tables = test_connection() + + if tables: + print("Gevonden tabellen:") + for table in tables: + print("-", table["name"]) + else: + print("Geen tabellen gevonden.") \ No newline at end of file diff --git a/backend/main.py b/backend/main.py index 756281b..43cc70f 100644 --- a/backend/main.py +++ b/backend/main.py @@ -1,6 +1,32 @@ # Python backend for a calender for my friend group. # We had the problem of never agreeing when or where -# we should meet up, and I hope this fixes the problem. +# we should meet up, and I hope this fixes the problem. + +from fastapi import FastAPI +from database import fetch_all + +app = FastAPI( + title="Calendar API", + description="Room based agenda system", + version="0.1" +) + + +@app.get("/") +def home(): + return { + "status": "online", + "service": "Calendar API" + } + + +@app.get("/database") +def database_status(): + tables = fetch_all( + "SELECT name FROM sqlite_master WHERE type='table';" + ) + + return { + "tables": [table["name"] for table in tables] + } -import fastapi -import time