From d57da6c6046892baf71ddb9ad74f8aa708f53172 Mon Sep 17 00:00:00 2001 From: Wylie Campbell Date: Wed, 11 Nov 2026 21:21:04 +1100 Subject: [PATCH] Additional work to Add function. It is currently in a broken state and needs work aligning INSERT sqlite instructions with schema of events db --- events.db | 0 src/cs50_final_project/main.py | 35 +++++++++++++++++++++++++++------- 2 files changed, 28 insertions(+), 7 deletions(-) create mode 100644 events.db diff --git a/events.db b/events.db new file mode 100644 index 0000000..e69de29 diff --git a/src/cs50_final_project/main.py b/src/cs50_final_project/main.py index 28a9c71..6f7d40d 100644 --- a/src/cs50_final_project/main.py +++ b/src/cs50_final_project/main.py @@ -1,8 +1,10 @@ +import sqlite3 # Make sure sqlite3 is imported from datetime import datetime, timedelta import typer from cs50_final_project.db import db_init +from cs50_final_project.helpers import create_cursor app = typer.Typer(help="Calendar Clash") @@ -42,14 +44,33 @@ def add(): error_datetime = "Incompatible Syntax. Please use DD-MM-YY and HH:MM for date and time, respectively, as well as a whole number (in minutes) for length." typer.echo(typer.style(error_datetime, fg=typer.colors.RED), err=True) raise typer.Exit(code=1) -#Query events.db for existing events matching iso_date -conn = sqlite3. - print(name) - print(date) - print(time) - print(length) - print(description) + conn = sqlite3.connect("events.db") + cursor = create_cursor(conn) + + cursor.execute("SELECT name, time, length FROM events WHERE date = ?", (iso_date,)) + existing_events = cursor.fetchall() + + for existing_name, existing_time, existing_length in existing_events: + ex_start = datetime.strptime(f"{iso_date} {existing_time}", "%Y-%m-%d %H:%M") + ex_end = ex_start + timedelta(minutes=int(existing_length)) + + if start_datetime < ex_end and ex_start < end_datetime: + print(f"\n Clash detected! Conflicts with existing event: '{existing_name}' ({existing_time} - {ex_end.strftime('%H:%M')})") + conn.close() + return + + # Close connection if no clash found + conn.close() + + cursor.execute( + "INSERT INTO events (name, start_time, length, description) VALUES (?, ?, ?, ?, ?)", + (name, iso_date, time, duration, description) + ) + conn.commit() + conn.close() + + typer.secho("\nEvent successfully added without clashes!", fg=typer.colors.GREEN) if __name__ == "__main__": app()