From 5b9c6d40581cc3bc3b72e7c1c9a76c040b19d11c Mon Sep 17 00:00:00 2001 From: Yiran Li Date: Wed, 13 Mar 2024 06:54:20 +1100 Subject: [PATCH] add delete and create function in EventRepository class --- controllers/v2/unavailability/api.py | 16 +++----- repository/volunteer_unavailability_v2.py | 48 ++++++++++++++++++++++- 2 files changed, 53 insertions(+), 11 deletions(-) diff --git a/controllers/v2/unavailability/api.py b/controllers/v2/unavailability/api.py index 470f9cf4..7d4070db 100644 --- a/controllers/v2/unavailability/api.py +++ b/controllers/v2/unavailability/api.py @@ -8,7 +8,6 @@ from .response_models import volunteer_unavailability_time from domain import session_scope, UserType from repository.volunteer_unavailability_v2 import EventRepository -from repository.unavailability_repository import * from services.jwk import requires_auth, is_user_or_has_role from controllers.v2.v2_blueprint import v2_api @@ -44,7 +43,9 @@ def put(self, user_id, event_id): def delete(self, user_id, event_id): with session_scope() as session: try: - success = remove_event(session, user_id, event_id) + event_repository = EventRepository(session) + success = event_repository.remove_event(user_id, event_id) + #success = remove_event(session, user_id, event_id) if success: # If the event is successfully removed, return HTTP 200 OK. return {"message": "Unavailability event removed successfully."}, 200 @@ -84,13 +85,8 @@ def post(self, user_id): return {"message": "Start time must be earlier than end time"}, 400 # HTTP 400 Bad Request with session_scope() as session: - # checks if new time frame overlaps with any existing in the database for specific userId - overlapping_events = session.query(UnavailabilityTime).filter( - UnavailabilityTime.userId == user_id, - UnavailabilityTime.start < args['end'], - UnavailabilityTime.end > args['start'], - UnavailabilityTime.periodicity == args['periodicity'] - ).all() + event_repository = EventRepository(session) + overlapping_events = event_repository.check_overlapping_events(user_id, args['start'], args['end'], args['periodicity']) if overlapping_events: overlapping_details = [] for event in overlapping_events: @@ -99,7 +95,7 @@ def post(self, user_id): return {"message": "Time frames overlap with existing events", "overlapping_events": overlapping_details}, 400 # HTTP 400 Bad Request - eventId = create_event( + eventId = event_repository.create_event( session, user_id, args['title'], diff --git a/repository/volunteer_unavailability_v2.py b/repository/volunteer_unavailability_v2.py index 7fa9368c..d84a1422 100644 --- a/repository/volunteer_unavailability_v2.py +++ b/repository/volunteer_unavailability_v2.py @@ -62,4 +62,50 @@ def get_event(self, userId): return None except Exception as e: logging.error(e) - return None \ No newline at end of file + return None + + # copy from repository.unavailability_repository.py + def create_event(self, userId, title, startTime, endTime, periodicity): + """ + Function to create an event + :param session: session + :param userId: Integer, user id + :param title: String, reason why unavailable + :param startTime: DateTime, from what time is unavailable + :param endTime: DateTime, to what time is unavailable + :param periodicity: Integer, Daily = 1, Weekly = 2, One-Off = 3 + """ + event = UnavailabilityTime(userId=userId, title=title, start=startTime, end=endTime, + periodicity=periodicity) + self.session.add(event) + # session.expunge(question) + self.session.flush() + return event.eventId + + # copy from repository.unavailability_repository.py + def remove_event(self, userId, eventId): + """ + Function to remove an event + :param session: session + :param userId: Integer, user id, who want to remove an event + :param eventId: Integer, event id want to remove + :return: True: remove successful + False: remove failed + """ + existing = self.session.query(UnavailabilityTime).filter(UnavailabilityTime.userId == userId, + UnavailabilityTime.eventId == eventId).first() + if existing is not None and existing.status is True: + existing.status = False + return True + return False + + # copy from post function in api.py written by Steven + def check_overlapping_events(self, userId, startTime, endTime, periodicity): + # checks if new time frame overlaps with any existing in the database for specific userId + overlapping_events = self.session.query(UnavailabilityTime).filter( + UnavailabilityTime.userId == userId, + UnavailabilityTime.start < endTime, + UnavailabilityTime.end > startTime, + UnavailabilityTime.periodicity == periodicity + ).all() + return overlapping_events \ No newline at end of file