Additional work to Add function.

It is currently in a broken state and needs work aligning INSERT sqlite instructions with schema of events db
This commit is contained in:
2026-11-11 21:21:04 +11:00
parent dc6d7f9503
commit d57da6c604
2 changed files with 28 additions and 7 deletions
View File
+28 -7
View File
@@ -1,8 +1,10 @@
import sqlite3 # Make sure sqlite3 is imported
from datetime import datetime, timedelta from datetime import datetime, timedelta
import typer import typer
from cs50_final_project.db import db_init from cs50_final_project.db import db_init
from cs50_final_project.helpers import create_cursor
app = typer.Typer(help="Calendar Clash") 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." 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) typer.echo(typer.style(error_datetime, fg=typer.colors.RED), err=True)
raise typer.Exit(code=1) raise typer.Exit(code=1)
#Query events.db for existing events matching iso_date
conn = sqlite3.
print(name) conn = sqlite3.connect("events.db")
print(date) cursor = create_cursor(conn)
print(time)
print(length) cursor.execute("SELECT name, time, length FROM events WHERE date = ?", (iso_date,))
print(description) 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__": if __name__ == "__main__":
app() app()