@@ -80,7 +80,7 @@ def flight(self, flight: Flight):
80
80
self ._flight = flight
81
81
82
82
@staticmethod
83
- async def get_rocketpy_flight (flight : Flight ) -> RocketPyFlight :
83
+ def get_rocketpy_flight (flight : Flight ) -> RocketPyFlight :
84
84
"""
85
85
Get the rocketpy flight object.
86
86
@@ -106,20 +106,18 @@ async def create_flight(self) -> Union[FlightCreated, HTTPException]:
106
106
views.FlightCreated
107
107
"""
108
108
try :
109
- created_flight = await FlightRepository (
110
- flight = self .flight
111
- ).create_flight (
109
+ await FlightRepository .fetch_flight (self .flight ).create_flight (
112
110
motor_kind = self .motor_kind , rocket_option = self .rocket_option
113
111
)
114
112
except Exception as e :
115
113
exc_str = parse_error (e )
116
114
logger .error (f"controllers.flight.create_flight: { exc_str } " )
117
115
raise HTTPException (
118
116
status_code = status .HTTP_500_INTERNAL_SERVER_ERROR ,
119
- detail = "Failed to create flight: {exc_str}" ,
117
+ detail = f "Failed to create flight: { exc_str } " ,
120
118
) from e
121
119
else :
122
- return FlightCreated (flight_id = created_flight .flight_id )
120
+ return FlightCreated (flight_id = self . flight .flight_id )
123
121
finally :
124
122
logger .info (
125
123
f"Call to controllers.flight.create_flight completed for Flight { hash (self .flight )} "
@@ -146,15 +144,15 @@ async def get_flight_by_id(flight_id: str) -> Union[Flight, HTTPException]:
146
144
logger .error (f"controllers.flight.get_flight_by_id: { exc_str } " )
147
145
raise HTTPException (
148
146
status_code = status .HTTP_500_INTERNAL_SERVER_ERROR ,
149
- detail = "Failed to read flight: {exc_str}" ,
147
+ detail = f "Failed to read flight: { exc_str } " ,
150
148
) from e
151
149
else :
152
150
if read_flight :
153
151
return read_flight
154
152
raise HTTPException (
155
153
status_code = status .HTTP_404_NOT_FOUND ,
156
154
detail = "Flight not found." ,
157
- ) from e
155
+ )
158
156
finally :
159
157
logger .info (
160
158
f"Call to controllers.flight.get_flight_by_id completed for Flight { flight_id } "
@@ -179,7 +177,7 @@ async def get_rocketpy_flight_as_jsonpickle(
179
177
"""
180
178
try :
181
179
read_flight = await cls .get_flight_by_id (flight_id )
182
- rocketpy_flight = await cls .get_rocketpy_flight (read_flight )
180
+ rocketpy_flight = cls .get_rocketpy_flight (read_flight )
183
181
except HTTPException as e :
184
182
raise e from e
185
183
except Exception as e :
@@ -189,7 +187,7 @@ async def get_rocketpy_flight_as_jsonpickle(
189
187
)
190
188
raise HTTPException (
191
189
status_code = status .HTTP_500_INTERNAL_SERVER_ERROR ,
192
- detail = "Failed to read flight: {exc_str}" ,
190
+ detail = f "Failed to read flight: { exc_str } " ,
193
191
) from e
194
192
else :
195
193
return FlightPickle (
@@ -200,7 +198,7 @@ async def get_rocketpy_flight_as_jsonpickle(
200
198
f"Call to controllers.flight.get_rocketpy_flight_as_jsonpickle completed for Flight { flight_id } "
201
199
)
202
200
203
- async def update_flight (
201
+ async def update_flight_by_id (
204
202
self , flight_id : str
205
203
) -> Union [FlightUpdated , HTTPException ]:
206
204
"""
@@ -216,30 +214,28 @@ async def update_flight(
216
214
HTTP 404 Not Found: If the flight is not found in the database.
217
215
"""
218
216
try :
219
- await FlightController .get_flight_by_id (flight_id )
220
- updated_flight = await FlightRepository (
221
- flight = self .flight ,
222
- motor_kind = self .motor_kind ,
223
- rocket_option = self .rocket_option ,
224
- ).update_flight_by_id (flight_id = flight_id )
225
- except HTTPException as e :
226
- raise e from e
217
+ flight_repo = await FlightRepository .fetch_flight (
218
+ self .flight
219
+ ).create_flight (
220
+ motor_kind = self .motor_kind , rocket_option = self .rocket_option
221
+ )
222
+ await flight_repo .delete_flight_by_id (flight_id )
227
223
except Exception as e :
228
224
exc_str = parse_error (e )
229
225
logger .error (f"controllers.flight.update_flight: { exc_str } " )
230
226
raise HTTPException (
231
227
status_code = status .HTTP_500_INTERNAL_SERVER_ERROR ,
232
- detail = "Failed to update flight: {exc_str}" ,
228
+ detail = f "Failed to update flight: { exc_str } " ,
233
229
) from e
234
230
else :
235
- return FlightUpdated (new_flight_id = updated_flight .flight_id )
231
+ return FlightUpdated (new_flight_id = self . flight .flight_id )
236
232
finally :
237
233
logger .info (
238
234
f"Call to controllers.flight.update_flight completed for Flight { flight_id } "
239
235
)
240
236
241
237
@classmethod
242
- async def update_env (
238
+ async def update_env_by_flight_id (
243
239
cls , flight_id : str , env : Env
244
240
) -> Union [FlightUpdated , HTTPException ]:
245
241
"""
@@ -260,28 +256,32 @@ async def update_env(
260
256
new_flight = read_flight .dict ()
261
257
new_flight ["environment" ] = env
262
258
new_flight = Flight (** new_flight )
263
- updated_flight = await FlightRepository (
264
- flight = new_flight , flight_id = flight_id
265
- ).update_flight ()
259
+ flight_repo = await FlightRepository .fetch_flight (
260
+ new_flight
261
+ ).create_flight (
262
+ motor_kind = read_flight .rocket .motor .motor_kind ,
263
+ rocket_option = read_flight .rocket .rocket_option ,
264
+ )
265
+ await flight_repo .delete_flight_by_id (flight_id )
266
266
except HTTPException as e :
267
267
raise e from e
268
268
except Exception as e :
269
269
exc_str = parse_error (e )
270
270
logger .error (f"controllers.flight.update_env: { exc_str } " )
271
271
raise HTTPException (
272
272
status_code = status .HTTP_500_INTERNAL_SERVER_ERROR ,
273
- detail = "Failed to update environment: {exc_str}" ,
273
+ detail = f "Failed to update environment: { exc_str } " ,
274
274
) from e
275
275
else :
276
- return FlightUpdated (new_flight_id = updated_flight .flight_id )
276
+ return FlightUpdated (new_flight_id = new_flight .flight_id )
277
277
finally :
278
278
logger .info (
279
279
f"Call to controllers.flight.update_env completed for Flight { flight_id } ; Env { hash (env )} "
280
280
)
281
281
282
- @staticmethod
283
- async def update_rocket (
284
- flight_id : str , rocket : Rocket , rocket_option , motor_kind
282
+ @classmethod
283
+ async def update_rocket_by_flight_id (
284
+ cls , flight_id : str , rocket : Rocket , rocket_option , motor_kind
285
285
) -> Union [FlightUpdated , HTTPException ]:
286
286
"""
287
287
Update a models.Flight.rocket in the database.
@@ -297,34 +297,35 @@ async def update_rocket(
297
297
HTTP 404 Not Found: If the flight is not found in the database.
298
298
"""
299
299
try :
300
- read_flight = await FlightController .get_flight_by_id (flight_id )
300
+ read_flight = await cls .get_flight_by_id (flight_id )
301
301
updated_rocket = rocket .dict ()
302
302
updated_rocket ["rocket_option" ] = rocket_option
303
303
updated_rocket ["motor" ]["motor_kind" ] = motor_kind
304
304
new_flight = read_flight .dict ()
305
305
new_flight ["rocket" ] = updated_rocket
306
306
new_flight = Flight (** new_flight )
307
- new_flight_id = await FlightRepository (
308
- flight = new_flight , flight_id = flight_id
309
- ).update_flight ()
307
+ flight_repo = await FlightRepository .fetch_flight (
308
+ new_flight
309
+ ).create_flight (motor_kind = motor_kind , rocket_option = rocket_option )
310
+ await flight_repo .delete_flight_by_id (flight_id )
310
311
except HTTPException as e :
311
312
raise e from e
312
313
except Exception as e :
313
314
exc_str = parse_error (e )
314
315
logger .error (f"controllers.flight.update_rocket: { exc_str } " )
315
316
raise HTTPException (
316
317
status_code = status .HTTP_500_INTERNAL_SERVER_ERROR ,
317
- detail = "Failed to update rocket: {exc_str}" ,
318
+ detail = f "Failed to update rocket: { exc_str } " ,
318
319
) from e
319
320
else :
320
- return FlightUpdated (new_flight_id = new_flight_id )
321
+ return FlightUpdated (new_flight_id = new_flight . flight_id )
321
322
finally :
322
323
logger .info (
323
324
f"Call to controllers.flight.update_rocket completed for Flight { flight_id } ; Rocket { hash (rocket )} "
324
325
)
325
326
326
327
@staticmethod
327
- async def delete_flight (
328
+ async def delete_flight_by_id (
328
329
flight_id : str ,
329
330
) -> Union [FlightDeleted , HTTPException ]:
330
331
"""
@@ -341,14 +342,12 @@ async def delete_flight(
341
342
"""
342
343
try :
343
344
await FlightRepository ().delete_flight_by_id (flight_id )
344
- except HTTPException as e :
345
- raise e from e
346
345
except Exception as e :
347
346
exc_str = parse_error (e )
348
347
logger .error (f"controllers.flight.delete_flight: { exc_str } " )
349
348
raise HTTPException (
350
349
status_code = status .HTTP_500_INTERNAL_SERVER_ERROR ,
351
- detail = "Failed to delete flight: {exc_str}" ,
350
+ detail = f "Failed to delete flight: { exc_str } " ,
352
351
) from e
353
352
else :
354
353
return FlightDeleted (deleted_flight_id = flight_id )
@@ -635,7 +634,7 @@ async def simulate_flight(
635
634
logger .error (f"controllers.flight.simulate_flight: { exc_str } " )
636
635
raise HTTPException (
637
636
status_code = status .HTTP_500_INTERNAL_SERVER_ERROR ,
638
- detail = "Failed to simulate flight: {exc_str}" ,
637
+ detail = f "Failed to simulate flight: { exc_str } " ,
639
638
) from e
640
639
else :
641
640
return flight_summary
0 commit comments