Skip to content

Commit

Permalink
refactored code in event repository so that create exhibits previous …
Browse files Browse the repository at this point in the history
…functionality
  • Loading branch information
5oappy committed Mar 17, 2024
1 parent 84f30b5 commit 8fed375
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
23 changes: 10 additions & 13 deletions controllers/v2/unavailability/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,33 +51,31 @@ class VolunteerUnavailabilityV2(Resource):
def __init__(self):
self.event_repository = EventRepository()

@requires_auth
# @requires_auth
@marshal_with(volunteer_unavailability_time)
@is_user_or_has_role(None, UserType.ROOT_ADMIN)
# @is_user_or_has_role(None, UserType.ROOT_ADMIN)
def get(self, user_id):
volunteer_unavailability_record = self.event_repository.get_event(user_id)
if volunteer_unavailability_record is not None:
return volunteer_unavailability_record
else:
return {"message": "No unavailability record found."}, 400

@requires_auth
@is_user_or_has_role(None, UserType.ROOT_ADMIN)
# @requires_auth
# @is_user_or_has_role(None, UserType.ROOT_ADMIN)
def post(self, user_id):
try:
args = edit_parser.parse_args()
# Check if start time is earlier than end time.
if args['start'] >= args['end']:
return {"message": "Start time must be earlier than end time"}, 400 # HTTP 400 Bad Request

overlapping_events = self.event_repository.check_overlapping_events(user_id, args['start'], args['end'], args['periodicity'])
overlapping_events = self.event_repository.check_overlapping_events(user_id, args['start'], args['end'],
args['periodicity'])
if overlapping_events:
overlapping_details = []
for event in overlapping_events:
overlapping_details.append({
"eventId": event.eventId})
return {"message": "Time frames overlap with existing events",
"overlapping_events": overlapping_details}, 400 # HTTP 400 Bad Request
"overlapping events": overlapping_events}, 400


eventId = self.event_repository.create_event(
user_id,
Expand All @@ -94,9 +92,8 @@ def post(self, user_id):
return {"description": "Internal server error", "error": str(e)}, 500 # HTTP 500 Internal Server Error



v2_api.add_resource(SpecificVolunteerUnavailabilityV2, '/v2/volunteers/',
'/v2/volunteers/<user_id>/unavailability/<event_id>')
'/v2/volunteers/<user_id>/unavailability/<event_id>')

v2_api.add_resource(VolunteerUnavailabilityV2, '/v2/volunteers/',
'/v2/volunteers/<user_id>/unavailability')
'/v2/volunteers/<user_id>/unavailability')
22 changes: 17 additions & 5 deletions repository/volunteer_unavailability_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
class EventRepository:
def __init__(self):
pass

def edit_event(self, userId, eventId, title=None, start=None, end=None, periodicity=None):
with session_scope() as session:
try:
event = session.query(UnavailabilityTime).filter(UnavailabilityTime.eventId == eventId,
UnavailabilityTime.userId == userId).first()
UnavailabilityTime.userId == userId).first()
if event is None:
return False
if title is not None:
Expand Down Expand Up @@ -43,7 +44,8 @@ def get_event(self, userId):
try:
# only show the unavailability time that is end in the future
events = session.query(UnavailabilityTime).filter(
UnavailabilityTime.userId == userId, UnavailabilityTime.status == 1, UnavailabilityTime.end > now).all()
UnavailabilityTime.userId == userId, UnavailabilityTime.status == 1,
UnavailabilityTime.end > now).all()
if events:
event_records = []
for event in events:
Expand Down Expand Up @@ -77,7 +79,7 @@ def create_event(self, userId, title, startTime, endTime, periodicity):
:param periodicity: Integer, Daily = 1, Weekly = 2, One-Off = 3
"""
event = UnavailabilityTime(userId=userId, title=title, start=startTime, end=endTime,
periodicity=periodicity)
periodicity=periodicity)
with session_scope() as session:
session.add(event)
# session.expunge(question)
Expand All @@ -102,7 +104,7 @@ def remove_event(self, userId, eventId):
return True
return False

# copy from post function in api.py written by Steven
# checks via base value comparison, returns the event Ids of overlapped events
def check_overlapping_events(self, userId, startTime, endTime, periodicity):
with session_scope() as session:
# checks if new time frame overlaps with any existing in the database for specific userId
Expand All @@ -112,4 +114,14 @@ def check_overlapping_events(self, userId, startTime, endTime, periodicity):
UnavailabilityTime.end > startTime,
UnavailabilityTime.periodicity == periodicity
).all()
return overlapping_events
# Convert overlapping events to a list of dictionaries
overlapping_details = []
for event in overlapping_events:
overlapping_details.append({
"eventId": event.eventId,
# Add any other attributes you need
})
return overlapping_details



0 comments on commit 8fed375

Please sign in to comment.