@@ -74,7 +74,9 @@ async def get_events_for_this_year_month(
7474 description = "Create a new event" ,
7575 response_model = Event ,
7676 status_code = status .HTTP_201_CREATED ,
77- responses = {500 : {"description" : "failed to fetch new event" , "model" : DetailModel }},
77+ responses = {
78+ 500 : {"description" : "failed to fetch new event" , "model" : DetailModel },
79+ },
7880 operation_id = "create_event" ,
7981 # dependecies=[Depends()]
8082)
@@ -108,22 +110,53 @@ async def update_event(
108110 eid : int ,
109111 body : EventUpdate
110112):
111- event_info = await event .crud .get_event_by_eid (db_session , eid )
112-
113- if event_info is None :
113+ db_event = await event .crud .get_event_by_eid (db_session , eid )
114+ if db_event is None :
114115 raise HTTPException (
115116 status_code = status .HTTP_404_NOT_FOUND ,
116117 detail = "Event doesn't exist."
117118 )
119+
120+ final_start_time = body .start_time if body .start_time is not None else db_event .start_time
121+ final_end_time = body .end_time if body .end_time is not None else db_event .end_time
122+
123+ if final_start_time > final_end_time :
124+ raise HTTPException (
125+ status_code = status .HTTP_422_UNPROCESSABLE_ENTITY ,
126+ detail = "The event start time must be before the end time"
127+ )
118128
119- updated_event = body .model_dump (exclude_unset = True )
120- for key , value in updated_event .items ():
121- setattr (event_info , key , value )
129+ if not body .start_date and body .end_date :
130+ if not db_event .start_date :
131+ raise HTTPException (
132+ status_code = status .HTTP_422_UNPROCESSABLE_ENTITY ,
133+ detail = "The event start date and event end date must be initilized at the same time"
134+ )
135+ if db_event .start_date > body .end_date :
136+ raise HTTPException (
137+ status_code = status .HTTP_422_UNPROCESSABLE_ENTITY ,
138+ detail = "The event start date must be before the event end date"
139+ )
140+ if body .start_date and not body .end_date :
141+ if not db_event .end_date :
142+ raise HTTPException (
143+ status_code = status .HTTP_422_UNPROCESSABLE_ENTITY ,
144+ detail = "The event start date and event end date must be initilized at the same time"
145+ )
146+ if body .start_date > db_event .end_date :
147+ raise HTTPException (
148+ status_code = status .HTTP_422_UNPROCESSABLE_ENTITY ,
149+ detail = "The event start date must be before the event end date"
150+ )
151+
152+ updated_data = body .model_dump (exclude_unset = True )
153+ for key , value in updated_data .items ():
154+ setattr (db_event , key , value )
122155
123156 await db_session .commit ()
124- await db_session .refresh (event_info )
157+ await db_session .refresh (db_event )
125158
126- return event_info
159+ return db_event
127160
128161
129162
0 commit comments