From c9c868d91484ecc6fe4124f0b571417f31dce86a Mon Sep 17 00:00:00 2001 From: Melissa Autumn Date: Fri, 1 Nov 2024 11:08:15 -0700 Subject: [PATCH] Reduce redundant queries with update_by_calendar function --- .../src/appointment/database/repo/calendar.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/backend/src/appointment/database/repo/calendar.py b/backend/src/appointment/database/repo/calendar.py index 16bd7fc18..558fe57e5 100644 --- a/backend/src/appointment/database/repo/calendar.py +++ b/backend/src/appointment/database/repo/calendar.py @@ -26,7 +26,7 @@ def is_owned(db: Session, calendar_id: int, subscriber_id: int): ) -def get(db: Session, calendar_id: int): +def get(db: Session, calendar_id: int) -> models.Calendar|None: """retrieve calendar by id""" return db.get(models.Calendar, calendar_id) @@ -36,7 +36,7 @@ def is_connected(db: Session, calendar_id: int): return get(db, calendar_id).connected -def get_by_url(db: Session, url: str): +def get_by_url(db: Session, url: str) -> models.Calendar|None: """retrieve calendar by calendar url""" return db.query(models.Calendar).filter(models.Calendar.url == url).first() @@ -66,9 +66,8 @@ def create(db: Session, calendar: schemas.CalendarConnection, subscriber_id: int return db_calendar -def update(db: Session, calendar: schemas.CalendarConnection, calendar_id: int): - """update existing calendar by id""" - db_calendar = get(db, calendar_id) +def update_by_calendar(db: Session, calendar: schemas.CalendarConnection, db_calendar: models.Calendar) -> models.Calendar|None: + """Update a calendar from the database with calendar data.""" # list of all attributes that must never be updated # # because they have dedicated update functions for security reasons @@ -88,6 +87,13 @@ def update(db: Session, calendar: schemas.CalendarConnection, calendar_id: int): return db_calendar +def update(db: Session, calendar: schemas.CalendarConnection, calendar_id: int): + """update existing calendar by id""" + db_calendar = get(db, calendar_id) + + return update_by_calendar(db, calendar, db_calendar) + + def update_connection(db: Session, is_connected: bool, calendar_id: int): """Updates the connected status of a calendar""" db_calendar = get(db, calendar_id) @@ -117,7 +123,7 @@ def update_or_create(db: Session, calendar: schemas.CalendarConnection, calendar if subscriber_calendar is None: return create(db, calendar, subscriber_id) - return update(db, calendar, subscriber_calendar.id) + return update_by_calendar(db, calendar, subscriber_calendar) def delete(db: Session, calendar_id: int):