Skip to content

Commit

Permalink
fix some error
Browse files Browse the repository at this point in the history
  • Loading branch information
Yiran Li committed Mar 13, 2024
1 parent 5b9c6d4 commit 0b7284a
Showing 1 changed file with 19 additions and 23 deletions.
42 changes: 19 additions & 23 deletions controllers/v2/unavailability/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,16 @@

class SpecificVolunteerUnavailabilityV2(Resource):

def __init__(self, event_repository):
self.event_repository = event_repository
def __init__(self):
self.event_repository = None

@requires_auth
@is_user_or_has_role(None, UserType.ROOT_ADMIN)
def put(self, user_id, event_id):
args = edit_parser.parse_args()
with session_scope() as session:
event_repository = EventRepository(session)
success = event_repository.edit_event(user_id, event_id, **args)
# success = edit_event(session, user_id, event_id, **args)
self.event_repository = EventRepository(session)
success = self.event_repository.edit_event(user_id, event_id, **args)
if success is True:
return {"message": "Updated successfully"}, 200
elif success is False:
Expand All @@ -43,9 +42,8 @@ def put(self, user_id, event_id):
def delete(self, user_id, event_id):
with session_scope() as session:
try:
event_repository = EventRepository(session)
success = event_repository.remove_event(user_id, event_id)
#success = remove_event(session, user_id, event_id)
self.event_repository = EventRepository(session)
success = self.event_repository.remove_event(user_id, event_id)
if success:
# If the event is successfully removed, return HTTP 200 OK.
return {"message": "Unavailability event removed successfully."}, 200
Expand All @@ -59,17 +57,16 @@ def delete(self, user_id, event_id):

class VolunteerUnavailabilityV2(Resource):

def __init__(self, event_repository):
self.event_repository = event_repository
def __init__(self):
self.event_repository = None

@requires_auth
@marshal_with(volunteer_unavailability_time)
@is_user_or_has_role(None, UserType.ROOT_ADMIN)
def get(self, user_id):
with session_scope() as session:
event_repository = EventRepository(session)
volunteer_unavailability_record = event_repository.get_event(user_id)
# volunteer_unavailability_record = get_event(session, user_id)
self.event_repository = EventRepository(session)
volunteer_unavailability_record = self.event_repository.get_event(user_id)
if volunteer_unavailability_record is not None:
return volunteer_unavailability_record
else:
Expand All @@ -85,8 +82,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:
event_repository = EventRepository(session)
overlapping_events = event_repository.check_overlapping_events(user_id, args['start'], args['end'], args['periodicity'])
self.event_repository = EventRepository(session)
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:
Expand All @@ -95,8 +92,7 @@ def post(self, user_id):
return {"message": "Time frames overlap with existing events",
"overlapping_events": overlapping_details}, 400 # HTTP 400 Bad Request

eventId = event_repository.create_event(
session,
eventId = self.event_repository.create_event(
user_id,
args['title'],
args['start'],
Expand All @@ -110,10 +106,10 @@ def post(self, user_id):
except Exception as e:
return {"description": "Internal server error", "error": str(e)}, 500 # HTTP 500 Internal Server Error

with session_scope() as session:
event_repository = EventRepository(session)
v2_api.add_resource(SpecificVolunteerUnavailabilityV2, '/v2/volunteers/',
'/v2/volunteers/<user_id>/unavailability/<event_id>', resource_class_args=[event_repository])

v2_api.add_resource(VolunteerUnavailabilityV2, '/v2/volunteers/',
'/v2/volunteers/<user_id>/unavailability', resource_class_args=[event_repository])

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

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

0 comments on commit 0b7284a

Please sign in to comment.