Skip to content

Commit 803f0db

Browse files
general refactoring
1 parent ae9dbd2 commit 803f0db

16 files changed

+209
-303
lines changed

lib/controllers/environment.py

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def env(self, env: Env):
4444
self._env = env
4545

4646
@staticmethod
47-
async def get_rocketpy_env(env: Env) -> RocketPyEnvironment:
47+
def get_rocketpy_env(env: Env) -> RocketPyEnvironment:
4848
"""
4949
Get the rocketpy env object.
5050
@@ -70,9 +70,7 @@ async def create_env(self) -> Union[EnvCreated, HTTPException]:
7070
views.EnvCreated
7171
"""
7272
try:
73-
created_env = await EnvRepository(
74-
environment=self.env
75-
).create_env()
73+
await EnvRepository.fetch_env(self.env).create_env()
7674
except Exception as e:
7775
exc_str = parse_error(e)
7876
logger.error(f"controllers.environment.create_env: {exc_str}")
@@ -81,7 +79,7 @@ async def create_env(self) -> Union[EnvCreated, HTTPException]:
8179
detail=f"Failed to create environment: {exc_str}",
8280
) from e
8381
else:
84-
return EnvCreated(env_id=created_env.env_id)
82+
return EnvCreated(env_id=self.env.env_id)
8583
finally:
8684
logger.info(
8785
f"Call to controllers.environment.create_env completed for Env {hash(self.env)}"
@@ -141,7 +139,7 @@ async def get_rocketpy_env_as_jsonpickle(
141139
"""
142140
try:
143141
read_env = await cls.get_env_by_id(env_id)
144-
rocketpy_env = await cls.get_rocketpy_env(read_env)
142+
rocketpy_env = cls.get_rocketpy_env(read_env)
145143
except HTTPException as e:
146144
raise e from e
147145
except Exception as e:
@@ -162,7 +160,7 @@ async def get_rocketpy_env_as_jsonpickle(
162160
f"Call to controllers.environment.get_rocketpy_env_as_jsonpickle completed for Env {env_id}"
163161
)
164162

165-
async def update_env(
163+
async def update_env_by_id(
166164
self, env_id: str
167165
) -> Union[EnvUpdated, HTTPException]:
168166
"""
@@ -178,12 +176,8 @@ async def update_env(
178176
HTTP 404 Not Found: If the env is not found in the database.
179177
"""
180178
try:
181-
await EnvController.get_env_by_id(env_id)
182-
updated_env = await EnvRepository(
183-
environment=self.env
184-
).update_env_by_id(env_id)
185-
except HTTPException as e:
186-
raise e from e
179+
env_repo = await EnvRepository.fetch_env(self.env).create_env()
180+
await env_repo.delete_env_by_id(env_id)
187181
except Exception as e:
188182
exc_str = parse_error(e)
189183
logger.error(f"controllers.environment.update_env: {exc_str}")
@@ -192,14 +186,16 @@ async def update_env(
192186
detail=f"Failed to update environment: {exc_str}",
193187
) from e
194188
else:
195-
return EnvUpdated(new_env_id=updated_env.env_id)
189+
return EnvUpdated(new_env_id=self.env.env_id)
196190
finally:
197191
logger.info(
198192
f"Call to controllers.environment.update_env completed for Env {env_id}; Env {hash(self.env)}"
199193
)
200194

201195
@staticmethod
202-
async def delete_env(env_id: str) -> Union[EnvDeleted, HTTPException]:
196+
async def delete_env_by_id(
197+
env_id: str,
198+
) -> Union[EnvDeleted, HTTPException]:
203199
"""
204200
Delete a models.Env from the database.
205201
@@ -246,7 +242,7 @@ async def simulate_env(
246242
"""
247243
try:
248244
read_env = await cls.get_env_by_id(env_id)
249-
rocketpy_env = await cls.get_rocketpy_env(read_env)
245+
rocketpy_env = cls.get_rocketpy_env(read_env)
250246
env_simulation_numbers = EnvData.parse_obj(
251247
rocketpy_env.all_info_returned()
252248
)

lib/controllers/flight.py

Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def flight(self, flight: Flight):
8080
self._flight = flight
8181

8282
@staticmethod
83-
async def get_rocketpy_flight(flight: Flight) -> RocketPyFlight:
83+
def get_rocketpy_flight(flight: Flight) -> RocketPyFlight:
8484
"""
8585
Get the rocketpy flight object.
8686
@@ -106,20 +106,18 @@ async def create_flight(self) -> Union[FlightCreated, HTTPException]:
106106
views.FlightCreated
107107
"""
108108
try:
109-
created_flight = await FlightRepository(
110-
flight=self.flight
111-
).create_flight(
109+
await FlightRepository.fetch_flight(self.flight).create_flight(
112110
motor_kind=self.motor_kind, rocket_option=self.rocket_option
113111
)
114112
except Exception as e:
115113
exc_str = parse_error(e)
116114
logger.error(f"controllers.flight.create_flight: {exc_str}")
117115
raise HTTPException(
118116
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}",
120118
) from e
121119
else:
122-
return FlightCreated(flight_id=created_flight.flight_id)
120+
return FlightCreated(flight_id=self.flight.flight_id)
123121
finally:
124122
logger.info(
125123
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]:
146144
logger.error(f"controllers.flight.get_flight_by_id: {exc_str}")
147145
raise HTTPException(
148146
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}",
150148
) from e
151149
else:
152150
if read_flight:
153151
return read_flight
154152
raise HTTPException(
155153
status_code=status.HTTP_404_NOT_FOUND,
156154
detail="Flight not found.",
157-
) from e
155+
)
158156
finally:
159157
logger.info(
160158
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(
179177
"""
180178
try:
181179
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)
183181
except HTTPException as e:
184182
raise e from e
185183
except Exception as e:
@@ -189,7 +187,7 @@ async def get_rocketpy_flight_as_jsonpickle(
189187
)
190188
raise HTTPException(
191189
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}",
193191
) from e
194192
else:
195193
return FlightPickle(
@@ -200,7 +198,7 @@ async def get_rocketpy_flight_as_jsonpickle(
200198
f"Call to controllers.flight.get_rocketpy_flight_as_jsonpickle completed for Flight {flight_id}"
201199
)
202200

203-
async def update_flight(
201+
async def update_flight_by_id(
204202
self, flight_id: str
205203
) -> Union[FlightUpdated, HTTPException]:
206204
"""
@@ -216,30 +214,28 @@ async def update_flight(
216214
HTTP 404 Not Found: If the flight is not found in the database.
217215
"""
218216
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)
227223
except Exception as e:
228224
exc_str = parse_error(e)
229225
logger.error(f"controllers.flight.update_flight: {exc_str}")
230226
raise HTTPException(
231227
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}",
233229
) from e
234230
else:
235-
return FlightUpdated(new_flight_id=updated_flight.flight_id)
231+
return FlightUpdated(new_flight_id=self.flight.flight_id)
236232
finally:
237233
logger.info(
238234
f"Call to controllers.flight.update_flight completed for Flight {flight_id}"
239235
)
240236

241237
@classmethod
242-
async def update_env(
238+
async def update_env_by_flight_id(
243239
cls, flight_id: str, env: Env
244240
) -> Union[FlightUpdated, HTTPException]:
245241
"""
@@ -260,28 +256,32 @@ async def update_env(
260256
new_flight = read_flight.dict()
261257
new_flight["environment"] = env
262258
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)
266266
except HTTPException as e:
267267
raise e from e
268268
except Exception as e:
269269
exc_str = parse_error(e)
270270
logger.error(f"controllers.flight.update_env: {exc_str}")
271271
raise HTTPException(
272272
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}",
274274
) from e
275275
else:
276-
return FlightUpdated(new_flight_id=updated_flight.flight_id)
276+
return FlightUpdated(new_flight_id=new_flight.flight_id)
277277
finally:
278278
logger.info(
279279
f"Call to controllers.flight.update_env completed for Flight {flight_id}; Env {hash(env)}"
280280
)
281281

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
285285
) -> Union[FlightUpdated, HTTPException]:
286286
"""
287287
Update a models.Flight.rocket in the database.
@@ -297,34 +297,35 @@ async def update_rocket(
297297
HTTP 404 Not Found: If the flight is not found in the database.
298298
"""
299299
try:
300-
read_flight = await FlightController.get_flight_by_id(flight_id)
300+
read_flight = await cls.get_flight_by_id(flight_id)
301301
updated_rocket = rocket.dict()
302302
updated_rocket["rocket_option"] = rocket_option
303303
updated_rocket["motor"]["motor_kind"] = motor_kind
304304
new_flight = read_flight.dict()
305305
new_flight["rocket"] = updated_rocket
306306
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)
310311
except HTTPException as e:
311312
raise e from e
312313
except Exception as e:
313314
exc_str = parse_error(e)
314315
logger.error(f"controllers.flight.update_rocket: {exc_str}")
315316
raise HTTPException(
316317
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}",
318319
) from e
319320
else:
320-
return FlightUpdated(new_flight_id=new_flight_id)
321+
return FlightUpdated(new_flight_id=new_flight.flight_id)
321322
finally:
322323
logger.info(
323324
f"Call to controllers.flight.update_rocket completed for Flight {flight_id}; Rocket {hash(rocket)}"
324325
)
325326

326327
@staticmethod
327-
async def delete_flight(
328+
async def delete_flight_by_id(
328329
flight_id: str,
329330
) -> Union[FlightDeleted, HTTPException]:
330331
"""
@@ -341,14 +342,12 @@ async def delete_flight(
341342
"""
342343
try:
343344
await FlightRepository().delete_flight_by_id(flight_id)
344-
except HTTPException as e:
345-
raise e from e
346345
except Exception as e:
347346
exc_str = parse_error(e)
348347
logger.error(f"controllers.flight.delete_flight: {exc_str}")
349348
raise HTTPException(
350349
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}",
352351
) from e
353352
else:
354353
return FlightDeleted(deleted_flight_id=flight_id)
@@ -635,7 +634,7 @@ async def simulate_flight(
635634
logger.error(f"controllers.flight.simulate_flight: {exc_str}")
636635
raise HTTPException(
637636
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}",
639638
) from e
640639
else:
641640
return flight_summary

lib/controllers/motor.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ async def create_motor(self) -> Union[MotorCreated, HTTPException]:
118118
views.MotorCreated
119119
"""
120120
try:
121-
created_motor = MotorRepository(motor=self.motor).create_motor(
121+
await MotorRepository.fetch_motor(self.motor).create_motor(
122122
motor_kind=self.motor_kind
123123
)
124124
except Exception as e:
@@ -129,7 +129,7 @@ async def create_motor(self) -> Union[MotorCreated, HTTPException]:
129129
detail=f"Failed to create motor: {exc_str}",
130130
) from e
131131
else:
132-
return MotorCreated(motor_id=created_motor.motor_id)
132+
return MotorCreated(motor_id=self.motor.motor_id)
133133
finally:
134134
logger.info(
135135
f"Call to controllers.motor.create_motor completed for Motor {self.motor.motor_id}"
@@ -210,7 +210,7 @@ async def get_rocketpy_motor_as_jsonpickle(
210210
f"Call to controllers.motor.get_rocketpy_motor_as_jsonpickle completed for Motor {motor_id}"
211211
)
212212

213-
async def update_motor(
213+
async def update_motor_by_id(
214214
self, motor_id: str
215215
) -> Union[MotorUpdated, HTTPException]:
216216
"""
@@ -226,12 +226,10 @@ async def update_motor(
226226
HTTP 404 Not Found: If the motor is not found in the database.
227227
"""
228228
try:
229-
await MotorController.get_motor_by_id(motor_id)
230-
updated_motor = await MotorRepository(
231-
motor=self.motor
232-
).update_motor_by_id(motor_id=motor_id, motor_kind=self.motor_kind)
233-
except HTTPException as e:
234-
raise e from e
229+
motor_repo = await MotorRepository.fetch_motor(
230+
self.motor
231+
).create_motor(motor_kind=self.motor_kind)
232+
await motor_repo.delete_motor_by_id(motor_id)
235233
except Exception as e:
236234
exc_str = parse_error(e)
237235
logger.error(f"controllers.motor.update_motor: {exc_str}")
@@ -240,14 +238,14 @@ async def update_motor(
240238
detail=f"Failed to update motor: {exc_str}",
241239
) from e
242240
else:
243-
return MotorUpdated(new_motor_id=updated_motor.motor_id)
241+
return MotorUpdated(new_motor_id=self.motor.motor_id)
244242
finally:
245243
logger.info(
246244
f"Call to controllers.motor.update_motor completed for Motor {motor_id}"
247245
)
248246

249247
@staticmethod
250-
async def delete_motor(
248+
async def delete_motor_by_id(
251249
motor_id: str,
252250
) -> Union[MotorDeleted, HTTPException]:
253251
"""
@@ -296,7 +294,7 @@ async def simulate_motor(
296294
"""
297295
try:
298296
read_motor = await MotorRepository().get_motor_by_id(motor_id)
299-
motor = await cls.get_rocketpy_motor(read_motor)
297+
motor = cls.get_rocketpy_motor(read_motor)
300298

301299
motor_simulation_numbers = MotorData(
302300
total_burning_time="Total Burning Time: "

0 commit comments

Comments
 (0)