1
- import json
2
- import uuid
3
-
4
- from flask import jsonify
5
1
from flask_restful import reqparse , Resource , marshal_with , inputs
6
2
7
- from domain .entity import unavailability_time
8
3
from .response_models import volunteer_unavailability_time
9
- from domain import session_scope , UserType
10
- from repository .volunteer_unavailability_v2 import *
11
- from repository .unavailability_repository import *
4
+ from domain import UserType
5
+ from repository .volunteer_unavailability_v2 import EventRepository
12
6
from services .jwk import requires_auth , is_user_or_has_role
13
7
from controllers .v2 .v2_blueprint import v2_api
14
8
20
14
21
15
22
16
class SpecificVolunteerUnavailabilityV2 (Resource ):
17
+ event_repository : EventRepository
18
+
19
+ def __init__ (self , event_repository : EventRepository = EventRepository ()):
20
+ self .event_repository = event_repository
23
21
24
22
@requires_auth
25
23
@is_user_or_has_role (None , UserType .ROOT_ADMIN )
26
24
def put (self , user_id , event_id ):
27
25
args = edit_parser .parse_args ()
28
- with session_scope () as session :
29
- success = edit_event (session , user_id , event_id , ** args )
30
- if success is True :
31
- return {"message" : "Updated successfully" }, 200
32
- elif success is False :
33
- return {"message" : "Event not found" }, 404
34
- else :
35
- return {"message" : "Unexpected Error Occurred" }, 400
26
+ success = self .event_repository .edit_event (user_id , event_id , ** args )
27
+ if success is True :
28
+ return {"message" : "Updated successfully" }, 200
29
+ elif success is False :
30
+ return {"message" : "Event not found" }, 404
31
+ else :
32
+ return {"message" : "Unexpected Error Occurred" }, 400
36
33
37
34
@requires_auth
38
35
@is_user_or_has_role (None , UserType .ROOT_ADMIN )
39
36
def delete (self , user_id , event_id ):
40
- with session_scope () as session :
41
- try :
42
- success = remove_event (session , user_id , event_id )
43
- if success :
44
- # If the event is successfully removed, return HTTP 200 OK.
45
- return {"message" : "Unavailability event removed successfully." }, 200
46
- else :
47
- # If the event does not exist or could not be removed, return HTTP 404 Not Found.
48
- return {"message" : "Unavailability event not found." }, 404
49
- except Exception as e :
50
- # HTTP 500 Internal Server Error
51
- return {"message" : "Internal server error" , "error" : str (e )}, 500
37
+ try :
38
+ success = self .event_repository .remove_event (user_id , event_id )
39
+ if success :
40
+ # If the event is successfully removed, return HTTP 200 OK.
41
+ return {"message" : "Unavailability event removed successfully." }, 200
42
+ else :
43
+ # If the event does not exist or could not be removed, return HTTP 404 Not Found.
44
+ return {"message" : "Unavailability event not found." }, 404
45
+ except Exception as e :
46
+ # HTTP 500 Internal Server Error
47
+ return {"message" : "Internal server error" , "error" : str (e )}, 500
52
48
53
49
54
50
class VolunteerUnavailabilityV2 (Resource ):
55
51
52
+ def __init__ (self ):
53
+ self .event_repository = EventRepository ()
54
+
56
55
@requires_auth
57
56
@marshal_with (volunteer_unavailability_time )
58
57
@is_user_or_has_role (None , UserType .ROOT_ADMIN )
59
58
def get (self , user_id ):
60
- with session_scope () as session :
61
- volunteer_unavailability_record = fetch_event (session , user_id )
62
- if volunteer_unavailability_record is not None :
63
- return volunteer_unavailability_record
64
- else :
65
- return jsonify ({'userID' : user_id , 'success' : False }), 400
59
+ volunteer_unavailability_record = self .event_repository .get_event (user_id )
60
+ if volunteer_unavailability_record is not None :
61
+ return volunteer_unavailability_record
62
+ else :
63
+ return {"message" : "No unavailability record found." }, 400
66
64
67
65
@requires_auth
68
66
@is_user_or_has_role (None , UserType .ROOT_ADMIN )
@@ -73,40 +71,33 @@ def post(self, user_id):
73
71
if args ['start' ] >= args ['end' ]:
74
72
return {"message" : "Start time must be earlier than end time" }, 400 # HTTP 400 Bad Request
75
73
76
- with session_scope () as session :
77
- # checks if new time frame overlaps with any existing in the database for specific userId
78
- overlapping_events = session .query (UnavailabilityTime ).filter (
79
- UnavailabilityTime .userId == user_id ,
80
- UnavailabilityTime .start < args ['end' ],
81
- UnavailabilityTime .end > args ['start' ],
82
- UnavailabilityTime .periodicity == args ['periodicity' ]
83
- ).all ()
84
- if overlapping_events :
85
- overlapping_details = []
86
- for event in overlapping_events :
87
- overlapping_details .append ({
88
- "eventId" : event .eventId })
89
- return {"message" : "Time frames overlap with existing events" ,
90
- "overlapping_events" : overlapping_details }, 400 # HTTP 400 Bad Request
91
-
92
- eventId = create_event (
93
- session ,
94
- user_id ,
95
- args ['title' ],
96
- args ['start' ],
97
- args ['end' ],
98
- args ['periodicity' ]
99
- )
100
- if eventId is not None :
101
- return {"eventId" : eventId }, 200 # HTTP 200 OK
102
- else :
103
- return {"description" : "Failed to create event" }, 400 # HTTP 400 Bad Request
74
+ overlapping_events = self .event_repository .check_overlapping_events (user_id , args ['start' ], args ['end' ], args ['periodicity' ])
75
+ if overlapping_events :
76
+ overlapping_details = []
77
+ for event in overlapping_events :
78
+ overlapping_details .append ({
79
+ "eventId" : event .eventId })
80
+ return {"message" : "Time frames overlap with existing events" ,
81
+ "overlapping_events" : overlapping_details }, 400 # HTTP 400 Bad Request
82
+
83
+ eventId = self .event_repository .create_event (
84
+ user_id ,
85
+ args ['title' ],
86
+ args ['start' ],
87
+ args ['end' ],
88
+ args ['periodicity' ]
89
+ )
90
+ if eventId is not None :
91
+ return {"eventId" : eventId }, 200 # HTTP 200 OK
92
+ else :
93
+ return {"description" : "Failed to create event" }, 400 # HTTP 400 Bad Request
104
94
except Exception as e :
105
95
return {"description" : "Internal server error" , "error" : str (e )}, 500 # HTTP 500 Internal Server Error
106
96
107
97
98
+
108
99
v2_api .add_resource (SpecificVolunteerUnavailabilityV2 , '/v2/volunteers/' ,
109
- '/v2/volunteers/<user_id>/unavailability/<event_id>' )
100
+ '/v2/volunteers/<user_id>/unavailability/<event_id>' )
110
101
111
102
v2_api .add_resource (VolunteerUnavailabilityV2 , '/v2/volunteers/' ,
112
- '/v2/volunteers/<user_id>/unavailability' )
103
+ '/v2/volunteers/<user_id>/unavailability' )
0 commit comments